Is there a way to set #JsonProperty annotation dynamically like:
class A {
#JsonProperty("newB") //adding this dynamically
private String b;
}
or can I simply rename field of an instance? If so, suggest me an idea.
Also, in what way an ObjectMapper can be used with serialization?
Assume that your POJO class looks like this:
class PojoA {
private String b;
// getters, setters
}
Now, you have to create MixIn interface:
interface PojoAMixIn {
#JsonProperty("newB")
String getB();
}
Simple usage:
PojoA pojoA = new PojoA();
pojoA.setB("B value");
System.out.println("Without MixIn:");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pojoA));
System.out.println("With MixIn:");
ObjectMapper mapperWithMixIn = new ObjectMapper();
mapperWithMixIn.addMixInAnnotations(PojoA.class, PojoAMixIn.class);
System.out.println(mapperWithMixIn.writerWithDefaultPrettyPrinter().writeValueAsString(pojoA));
Above program prints:
Without MixIn:
{
"b" : "B value"
}
With MixIn:
{
"newB" : "B value"
}
this is a very late answer but, if it helps you or others, you should be able to change annotations at runtime. Check this link:
https://www.baeldung.com/java-reflection-change-annotation-params
Modifying annotations might be a bit messy and I prefer other options.
Mixin's are a good static option but if you need to change properties at runtime you can use a custom serializer (or deserializer). Then register your serializer with the ObjectMapper of your choosing (writing formats like json / xml are now provided for free via Jackson). Here are some additional examples:
custom serializer:
https://www.baeldung.com/jackson-custom-serialization
custom deserializer:
https://www.baeldung.com/jackson-deserialization
i.e.:
class A {
// #JsonProperty("newB") //adding this dynamically
String b;
}
class ASerializer extends StdSerializer<A> {
public ASerializer() {
this(null);
}
public ASerializer(Class<A> a) {
super(a);
}
#Override
public void serialize(A a, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (a == null) {
gen.writeNull();
} else {
gen.writeStartObject();
gen.writeStringField("newB", a.b);
gen.writeEndObject();
}
}
}
#Test
public void test() throws JsonProcessingException {
A a = new A();
a.b = "bbb";
String exp = "{\"newB\":\"bbb\"}";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(A.class, new ASerializer());
mapper.registerModule(module);
assertEquals(exp, mapper.writeValueAsString(a));
}
Related
Is there a way on Springboot that I can implement a custom serializer for a specific field on my request without doing annotations?
I prefer if we could create a bean or a override a configuration and serialize a string input (from json request) going to OffsetDateTime field on my request pojo.
I cannot annotate because my request classes are auto-generated..
You can register the serializer programatically in jackson. The class needing custom serialization:
public class SpecialObject {
private String field;
private String anotherField;
//getters, setters
}
The wrapper class, where it is a field:
public class WrapperObject {
private String text;
private SpecialObject specialObject;
//getters, setters
}
The serializer:
public class SpecialObjectSerializer extends StdSerializer<SpecialObject> {
public SpecialObjectSerializer() {
super(SpecialObject.class);
}
#Override
public void serialize(SpecialObject value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeStringField("fieldChanged", value.getField());
gen.writeStringField("anotherFieldChanged", value.getAnotherField());
gen.writeEndObject();
}
}
Nothing fancy, just changing field names when serializing.
Now you need to add your serializer in a Module and register that module in object mapper. You can do it like this:
public class NoAnnot {
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
//add serializer in module
module.addSerializer(SpecialObject.class, new SpecialObjectSerializer());
//register module
objectMapper.registerModule(module);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
SpecialObject specialObject = new SpecialObject();
specialObject.setField("foo");
specialObject.setAnotherField("bar");
WrapperObject wrapperObject = new WrapperObject();
wrapperObject.setText("bla");
wrapperObject.setSpecialObject(specialObject);
String json = objectMapper.writeValueAsString(wrapperObject);
System.out.println(json);
}
}
I use Jackson to serialise POJOs into CSV. Now we need to change the naming for certain fields to snake_case. This is easily done by #JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class).
For compatibility reasons we need some of the renamed fields also with their old name.
E.g.:
public class Pojo {
private int someField;
}
Default will serialise to "someField", SnakeCaseStrategy will serialise to "some_field".
How to get serialization with both?:
{
"someField" : "one",
"some_field" : "one"
}
My first try was a mixin:
public abstract class PojoFormat {
#JsonProperty("someField")
abstract String getSomeField();
}
but this effectively only undoes the naming strategy change.
So how to copy a field in serialization - preferable not by changing the Pojo (this copied fields should be removed when all clients can cope with it).
Little update:
in my real class there some nested class that use JsonUnwrapped and the doc stated that this is not working with custom serializer (didn't know that this makes a difference here).
Well, I have never seen this before, I would be very happy if someone here in this site knows how.
The easy way, in my opinion, is to use a Custom Serializer.
For example:
Using the #JsonSerialize annotation
Register a module
Dynamic Serializer with Reflection
#JsonSerialize annotation
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
#JsonSerializer(using=PojoSerializer.class)
class Pojo {
private String myValue;
// getters and setters
}
class PojoSerializer extends StdSerializer<Pojo> {
public PojoSerializer() {
super(Pojo.class);
}
#Override
public void serialize(Pojo value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeStringField("myValue", value.getMyValue());
gen.writeStringField("my_value", value.getMyValue());
gen.writeEndObject();
}
}
Module
static class Pojo {
private String myValue;
public String getMyValue() {
return myValue;
}
public Pojo setMyValue(String myValue) {
this.myValue = myValue;
return this;
}
}
static class PojoSerializer extends StdSerializer<Pojo> {
public PojoSerializer() {
super(Pojo.class);
}
#Override
public void serialize(Pojo value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeStringField("myValue", value.getMyValue());
gen.writeStringField("my_value", value.getMyValue());
gen.writeEndObject();
}
}
public static void main(String[] args) throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule("PojoModule");
module.addSerializer(Pojo.class, new PojoSerializer());
mapper.registerModule(module);
final Pojo pojo = new Pojo();
pojo.setMyValue("This is the value of my pojo");
System.out.println(mapper.writeValueAsString(pojo));
}
Reflection
I write some code for you, you might want to see to get new ideias.
This works as a generic way(just to not write several serializers).
// The serializer will be register in the ObjectMapper module.
static class Pojo {
private String myValue = "With snake and camel";
private String value = "Without snake case";
private String thirdValue = "snake & camel";
}
// using the annotation
#JsonSerialize(using = PojoSerializer.class)
static class Pojo2 {
private String pojoName = "Pojo 2";
private String pojo = "pojp";
}
static class PojoSerializer extends StdSerializer<Object> {
public PojoSerializer() {
super(Object.class);
}
#Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
final Field[] fields = value.getClass().getDeclaredFields();
for(final Field field : fields) {
final String name = field.getName();
final String fieldValue;
try {
// Do not use this!
fieldValue = (String)field.get(value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
byte firstUpperCase = -1;
for(byte index = 0; index < name.length(); index++) {
final char caractere = name.charAt(index);
// A ascii code is 66 decimal, and 90 is the Z in decimal
if(caractere > 'A' && caractere < 'Z') {
// found the first upper
firstUpperCase = index;
break;
}
}
// writes the normal field name
gen.writeStringField(name, fieldValue);
// if the name is in camel case, we will write in snake case too.
if(firstUpperCase != -1) {
final char lowerLetter = (char)((int) name.charAt(firstUpperCase) + 32);
final String left = name.substring(0, firstUpperCase);
final String right = String.format("%c%s",lowerLetter, name.substring(firstUpperCase + 1));
gen.writeStringField(String.format("%s_%s", left, right), fieldValue);
}
}
gen.writeEndObject();
}
}
You can try to use JsonAnyGetter annotation and define for every POJO extra mapping for backward compatibility.
Let's create a simple interface:
interface CompatibleToVer1 {
#JsonAnyGetter
Map<String, Object> getCompatibilityView();
}
and two classes which implement it:
#Data
#NoArgsConstructor
#AllArgsConstructor
#JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
class RootPojo implements CompatibleToVer1 {
private int rootId;
#JsonUnwrapped
private SomePojo pojo;
#Override
public Map<String, Object> getCompatibilityView() {
return Collections.singletonMap("rootId", rootId);
}
}
#Data
#NoArgsConstructor
#AllArgsConstructor
#JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
class SomePojo implements CompatibleToVer1 {
private int someField;
private String someName;
#Override
public Map<String, Object> getCompatibilityView() {
Map<String, Object> extra = new LinkedHashMap<>();
extra.put("someField", someField);
return extra;
}
}
As you can see, I defined extra columns for each POJO with custom names. Serialising to JSON is straightforward:
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
SomePojo pojo = new SomePojo(123, "Tom");
mapper.writeValue(System.out, new RootPojo(1, pojo));
Above code prints:
{
"root_id" : 1,
"some_field" : 123,
"some_name" : "Tom",
"someField" : 123,
"rootId" : 1
}
But for CSV we need to create extra configuration:
CsvMapper csvMapper = CsvMapper.builder().build();
CsvSchema pojoExtraScheme = CsvSchema.builder()
.addColumn("someField")
.build();
CsvSchema rootExtraScheme = CsvSchema.builder()
.addColumn("rootId")
.build();
CsvSchema compatibleSchema = CsvSchema.emptySchema()
.withHeader()
.withColumnsFrom(csvMapper.schemaFor(RootPojo.class))
.withColumnsFrom(rootExtraScheme)
.withColumnsFrom(csvMapper.schemaFor(SomePojo.class))
.withColumnsFrom(pojoExtraScheme);
SomePojo tom = new SomePojo(123, "Tom");
SomePojo jerry = new SomePojo(124, "Jerry");
List<RootPojo> pojos = Arrays.asList(new RootPojo(1, tom), new RootPojo(2, jerry));
ObjectWriter writer = csvMapper.writer(compatibleSchema);
System.out.println(writer.writeValueAsString(pojos));
Above code prints:
some_field,some_name,root_id,rootId,someField
123,Tom,1,1,123
124,Jerry,2,2,124
If you do not want to specify extra columns two times you can implement builder method based on our interface:
CsvSchema createSchemaFor(CompatibleToVer1 entity) {
CsvSchema.Builder builder = CsvSchema.builder();
entity.getCompatibilityView().keySet().forEach(builder::addColumn);
return builder.build();
}
and use as below:
CsvSchema compatibleSchema = CsvSchema.emptySchema()
.withHeader()
.withColumnsFrom(csvMapper.schemaFor(RootPojo.class))
.withColumnsFrom(createSchemaFor(new RootPojo()))
.withColumnsFrom(csvMapper.schemaFor(SomePojo.class))
.withColumnsFrom(createSchemaFor(new SomePojo()));
Using JsonAnyGetter with CSV is really tricky and could be problematic mixing it with other annotations, take a look at: Could please add JsonAnyGetter and JsonAnySetter annotations support?
I have the provider interface
interface IProvider<T> {
T locate();
}
and a class containing a field of type IProvider (can be another type for other fields).
class MyObject {
MyLocator<String> field;
}
I need to serialize instances of MyObject to JSON using Jackson 1.7. The output must be the same as if MyObject.field had been a String (i.e. no reference to ILocator).
I can't figure out how to build the custom serializer required to achieve this. Here is the structure I am trying to use for this task:
class MyLocatorSerializer extends SerializerBase<MyLocator<?>> {
public MyLocatorSerializer() {
super(MyLocator.class, false);
}
#Override
public void serialize(MyLocator<?> a_value, JsonGenerator a_jgen,
SerializerProvider a_provider) throws IOException, JsonGenerationException {
// Insert code here to serialize a_value.locate(), whatever its type
}
#Override
public JsonNode getSchema(SerializerProvider a_provider, Type a_typeHint)
throws JsonMappingException {
// What should I return here? I can't find documentation regarding the different schema types...
}
}
The custom serializer would be registered using
SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));
module.addSerializer(new MyLocatorSerializer());
objectMapper.registerModule(module);
Another answer using mix-in annotations following the comment from Staxman.
static class JacksonCustomModule extends SimpleModule {
public JacksonCustomModule() {
super("JacksonCustomModule", new Version(1, 0, 0, null));
}
#Override
public void setupModule(SetupContext context) {
context.setMixInAnnotations(IProvider.class, IProviderMixIn.class);
super.setupModule(context);
}
interface IProviderMixIn<T> {
#JsonValue
T locate();
}
}
Activate the module with:
objectMapper.registerModule(new JacksonCustomModule());
Apologies if I misunderstand the question, but would this be as simple as just using #JsonValue on 'Locate' method, instead of writing a custom serializer?
What #JsonValue does is take value of a property as is, and use it instead of creating a JSON Object: often this is used for serializing a POJO as a simple String or number, like so:
public class StringWrapper {
#JsonValue public String value;
}
so that for class like:
public class POJO {
public StringWrapper wrapped;
}
we would get serialization like:
{
"wrapper" : "string value of 'value'"
}
instead of what would otherwise be seen:
{
"wrapper" : {
"value" : "... string value ... "
}
}
Annotation can be used for any types of values obviously.
Following StaxMan's answer, I inspected the workings of #JsonValue and got the following serializer:
// Based on JsonValueSerializer
private static class ProviderSerializer extends SerializerBase<IProvider<?>> {
public ProviderSerializer() {
super(IProvider.class, false);
}
#Override
public void serialize(IProvider<?> value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
Object object = value.locate();
// and if we got null, can also just write it directly
if (object == null) {
provider.defaultSerializeNull(jgen);
return;
}
Class<?> c = object.getClass();
JsonSerializer<Object> ser = provider.findTypedValueSerializer(c, true, null);
// note: now we have bundled type serializer, so should NOT call with typed version
ser.serialize(object, jgen, provider);
}
#Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
throws JsonMappingException {
// is this right??
return JsonSchema.getDefaultSchemaNode();
}
}
After some tests, this does what I need. However, I don't fully really understand the purpose of the getSchema method, so maybe I'm doing something wrong...
I've got a bean defined as such :
public static class TestBean {
private String a;
private Long b;
public TestBean(String a, Long b) {
this.a = a;
this.b = b;
}
public String getA() {
return a;
}
public Long getB() {
return b;
}
}
It models some business and I do not get to instantiate it (using JPA). Some of my API let the user retrieve a view of this bean serialize as JSON using Jackson (through JAX-RS) and I would like to add a list of related links to this view.
The normal Jackson JSON serialization would be (for a = "aa" and b = 2L) :
{"a":"aa","b":2}
And I would like to have the links appear as
{"a":"aa","b":2,
"links":[{"rel":"rel","href":"href://"},{"rel":"rel2","href":"href://2"}]}
Possible work-around
I would rather not add a getLinks() method to my bean, it's specific to this view.
Simply using a composite object would yield a serialization like :
{"data":{"a":"aa","b":2},"links":[{"rel":"rel","href":"href://"}]}
Which I could live with but is not what I was looking for ultimately.
Current solution
I would like to avoid manipulating the JSON string or having to reload it into a Map to insert my extra values. For now the solution I've come up with seem awfully convoluted:
Current scary solution :
//a composite view object
public abstract class AddedLinksView<K> {
private final K resource;
private final Link[] links;
public AddedLinksView(K resource) {
this.resource = resource;
links = buildLinks(resource);
}
public abstract Link[] buildLinks(K resource);
public K getResource() {
return resource;
}
public Link[] getLinks() {
return links;
}
}
//a specific bean serializer
private static class RawBeanSerializer extends BeanSerializer {
public RawBeanSerializer(BeanSerializerBase ser) {
super(ser);
}
//this is like the standard serialize but without the start and end tags
public void rawSerialize(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException,
JsonGenerationException {
if (_propertyFilterId != null) {
serializeFieldsFiltered(bean, jgen, provider);
} else {
serializeFields(bean, jgen, provider);
}
}
}
#Test
public void usingModule() throws Exception {
// basic module metadata just includes name and version (both for troubleshooting; but name needs to be unique)
SimpleModule module = new SimpleModule("EnhancedDatesModule", new Version(0, 1, 0, "alpha"));
//adding a serializer for the composite view
module.addSerializer(new JsonSerializer<AddedLinksView>() {
#Override
public Class<AddedLinksView> handledType() {
return AddedLinksView.class;
}
#Override
public void serialize(AddedLinksView value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
//looking up the bean serializer that will be used for my resource
JsonSerializer<Object> ser = provider.findTypedValueSerializer(value.getResource().getClass(), true,
null);
if (ser instanceof BeanSerializerBase) {
//cloning it in a sub class that makes it possible to 'inline' the serialization
RawBeanSerializer openSer = new RawBeanSerializer((BeanSerializerBase) ser);
openSer.rawSerialize(value.getResource(), jgen, provider);
}
//adding my links
jgen.writeArrayFieldStart("links");
for (Link link : value.getLinks()) {
jgen.writeObject(link);
}
jgen.writeEndArray();
jgen.writeEndObject();
}
});
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
AddedLinksView<TestBean> view = new AddedLinksView<TestBean>(new TestBean("aa", 2L)) {
#Override
public Link[] buildLinks(TestBean resource) {
return new Link[] { new Link("rel", "href://"), new Link("rel2", "href://2") };
}
};
System.out.println("useModule json output: " + mapper.writeValueAsString(view));
}
Did I miss something obvious in Jackson to achieve this? Or am I completely off the mark in my requirements already?
There is no real way to externally inject things into POJOs to serialize: but you might be interested in checking out #JsonAnyGetter, which at least allows just adding contents of a java.util.Map as extra properties for a POJO.
Would this answer your question: http://wiki.fasterxml.com/JacksonFeatureUpdateValue
I am not sure you can avoid mapping. You may use Dozer to help.
This should help you: Tools for merging java beans
I have a Map<A,B> fieldOfC as a field of a class C. When I try to deserialize C with Jackson, an Exception is thrown because it can't find a Deserializer for Map's key A. So, I guess the solution is to extend StdJsonDeserializer and do it manually.
My problem is that I can't find an example on how to use the parser and the context of the method "deserialize" that I have to implement.
Can anyone write the code for this simple example so I can use it as a start to build my real deserializer?
public class A{
private String a1;
private Integer a2;
}
public class B{
private String b1;
}
public class C{
#JsonDeserialize(keyUsing=ADeserializer.class)
//also tried this: #JsonDeserialize(keyAs=A.class) without success
private Map<A,B> fieldOfC;
private String c1;
}
public class ADeserializer extends StdKeyDeserializer {
protected ADeserializer(Class<A> cls) {
super(cls);
}
protected Object _parse(String key, DeserializationContext ctxt) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(key, A.class);
}
}
Thanks in advance
EDIT: googling, I found a test of the same problem I have. This is exactly my problem
EDIT: changed extended class from StdDeserializer to StdKeyDeserializer as I read here in method findKeyDeserializer(org.codehaus.jackson.map.DeserializationConfig, org.codehaus.jackson.type.JavaType, org.codehaus.jackson.map.BeanProperty)
EDIT: After solving this issue I got this one that is related.
I am a complete newbie with Jackson, but the following works for me.
First I add a JsonCreator method to A:
public class A {
private String a1;
private Integer a2;
public String getA1() { return a1; }
public Integer getA2() { return a2; }
public void setA1(String a1) { this.a1 = a1; }
public void setA2(Integer a2) { this.a2 = a2; }
#JsonCreator
public static A fromJSON(String val) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
A a = mapper.readValue(val,A.class);
return a;
}
}
That alone solves the deserialization problem. The harder part for me was the correct serialization of the keys. What I did there was to define a key serializer that serializes named classes as there JSON serialization, like this:
public class KeySerializer extends SerializerBase<Object> {
private static final SerializerBase<Object> DEFAULT = new StdKeySerializer();
private Set<Class<?>> objectKeys_ = Collections.synchronizedSet(new HashSet<Class<?>>());
protected KeySerializer(Class<?>... objectKeys) {
super(Object.class);
for(Class<?> cl:objectKeys) {
objectKeys_.add(cl);
}
}
#Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
return DEFAULT.getSchema(provider, typeHint);
}
#Override
public void serialize(Object value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonGenerationException {
if (objectKeys_.contains(value.getClass())) {
ObjectMapper mapper = new ObjectMapper();
StringWriter writer = new StringWriter();
mapper.writeValue(writer, value);
jgen.writeFieldName(writer.toString());
} else {
DEFAULT.serialize(value, jgen, provider);
}
}
}
Then to prove it works, serializing and deserializing an instance of class C:
ObjectMapper mapper = new ObjectMapper();
StdSerializerProvider provider = new StdSerializerProvider();
provider.setKeySerializer(new KeySerializer(A.class));
mapper.setSerializerProvider(provider);
StringWriter out = new StringWriter();
mapper.writeValue(out, c);
String json = out.toString();
System.out.println("JSON= "+json);
C c2 = mapper.readValue(json, C.class);
System.out.print("C2= ");
StringWriter outC2 = new StringWriter();
mapper.writeValue(outC2, c2);
System.out.println(outC2.toString());
For me this produced the output:
JSON= {"c1":"goo","map":{"{\"a1\":\"1ccf\",\"a2\":7376}":{"b1":"5ox"},"{\"a1\":\"1cd2\",\"a2\":7379}":{"b1":"5p0"},"{\"a1\":\"1cd5\",\"a2\":7382}":{"b1":"5p3"},"{\"a1\":\"1cd8\",\"a2\":7385}":{"b1":"5p6"}}}
C2= {"c1":"goo","map":{"{\"a1\":\"1ccf\",\"a2\":7376}":{"b1":"5ox"},"{\"a1\":\"1cd2\",\"a2\":7379}":{"b1":"5p0"},"{\"a1\":\"1cd5\",\"a2\":7382}":{"b1":"5p3"},"{\"a1\":\"1cd8\",\"a2\":7385}":{"b1":"5p6"}}}
I feel there ought to have been a better way of doing saying how to serialize the key by using annotations, but I could not work it out.