Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a PropertyNameConverter which allows a custom mapping between java field names and YAML property names to be configured #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

agibagib
Copy link

Sometimes there is a difference between property names as they appear in YAML, and how the Java fields are named. This commit resolves that by introducing the concept of a PropertyNameConverter which allows the transformation between a Field and the YAML property to be defined.

The PropertyNameConverter can be defined using YamlConfig.setPropertyNameConverter()

There is a default implementation DefaultPropertyNameConverter which retains the existing functionality if nothing has been configured.

One notable difference is within YamlConfig.setPropertyElementType and setPropertyDefaultType. Here the propertyName parameter should be the YAML version of the name (the result of the conversion), not the field name.

Below is a short example which demonstrates mapping YAML properties containing spaces, onto camel case field names. eg. full name becomes fullName.

package com.esotericsoftware.yamlbeans;

import java.io.StringWriter;
import java.lang.reflect.Field;

public class PropertyNameConverterExample {

    private static final PropertyNameConverter CAMELCASE_TO_SPACES_CONVERTER = new PropertyNameConverter() {
        public String convertFieldToPropertyName(Field field) {
            return field.getName().replaceAll("[A-Z]", " $0").trim().toLowerCase();
        }
    };

    public static void main(String[] args) throws YamlException {
        TestObject o = new TestObject();
        o.setFullName("Bob");
        o.setPhoneNumber("124-1234-324");

        StringWriter sw = new StringWriter();
        YamlWriter writer = new YamlWriter(sw);
        writer.getConfig().setPropertyNameConverter(CAMELCASE_TO_SPACES_CONVERTER);
        writer.write(o);
        writer.close();

        System.out.println(sw);

        YamlReader reader = new YamlReader(sw.toString());
        reader.getConfig().setPropertyNameConverter(CAMELCASE_TO_SPACES_CONVERTER);
        TestObject o2 = reader.read(TestObject.class);
        System.out.println("o2.getFullName() = " + o2.getFullName());
        System.out.println("o2.getPhoneNumber() = " + o2.getPhoneNumber());

    }

    public static class TestObject {
        private String fullName;
        private String phoneNumber;

        public String getFullName() {
            return fullName;
        }

        public void setFullName(String fullName) {
            this.fullName = fullName;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
    }
}

Output:

!com.esotericsoftware.yamlbeans.PropertyNameConverterExample$TestObject
full name: Bob
phone number: 124-1234-324

o2.getFullName() = Bob
o2.getPhoneNumber() = 124-1234-324

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant