YML based serializer for Java. Based on JMS serializer https://jmsyst.com/libs/serializer/master/reference/yml_reference
To create a Serializer instance:
Cache cache = new Cache() {
@Override
public String get(String key) { return null; }
@Override
public void set(String key, String content) {}
@Override
public void remove(String key) {}
};
ConfigurationProvider configurationProvider = new ConfigurationProvider() {
@Override
public boolean getBoolean(String key, boolean defaultValue) { return true; }
@Override
public String getString(String key, String defaultValue) { return ""; }
};
Environment environment = () -> false;
Serializer serializer = SerializerBuilder.build()
.withCache(cache)
.withConfigurationProvider(configurationProvider)
.withEnvironment(environment)
.get();
And to add a logger instance:
Logger logger = new Logger() {
@Override
public Level getLevel() { return Level.ERROR; }
@Override
public void setLevel(Level level) { }
@Override
public void error(String message) { }
};
serializer.setLogger(logger);
Class Foo:
package foo.bar;
class Foo {
private String bar = "foobar";
public String getBar() {
return bar;
}
public Long getCalc() {
return 2 + 2;
}
}
Yml file (named foo.bar.Foo.yml
):
foo.bar.Foo:
virtual_properties:
getCalc:
serialized_name: calc
groups: [barGroup]
properties:
bar:
groups: [fooGroup]
Usage:
serializer.serialize(fooInstance, "fooGroup", "barGroup");
// it will return
// {"bar": "foobar", "calc": 4}