There is an abstract class Product and another class SomeProduct which extends Product.
Product:
#JsonTypeInfo
(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type"
)
#JsonSubTypes
({
#JsonSubTypes.Type(value = SomeProduct.class, name = Product.PRODUCT_TYPE_SOME)
})
public abstract class Product
{
static final String PRODUCT_TYPE_SOME = "some_product";
}
SomeProduct:
public class SomeProduct extends Product
{
#JsonProperty("status")
#JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
private int status;
public int getStatus()
{
return status;
}
public void setStatus(int status)
{
this.status = status;
}
}
There will be more classes (different products) which will extend Product.
When I serialize it using ObjectMapper,
ObjectMapper mapper = new ObjectMapper();
Product p = new SomeProduct();
String json = mapper.writeValueAsString(p);
this is the output:
{"type":"some_product"}
Now, when I try to deserialize it back,
Product x = mapper.convertValue(json, Product.class);
this exception is thrown:
java.lang.IllegalArgumentException: Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'type' that is to contain type id (for class com.shubham.model.Product)
at [Source: N/A; line: -1, column: -1]
What am I doing wrong here ? I've looked on SO and found a question where defaultImpl was used in JsonTypeInfo. But I can't deserialize the json back to a "Default Impl" since the JSON will always be valid for a specific implementation.
Using Jackson 2.4.3
You are wrongly using mapper.convertValue(json, Product.class);. You should use:
mapper.readValue(json, Product.class);
convertValue use:
Convenience method for doing two-step conversion from given value, into
instance of given value type, if (but only if!) conversion is needed.
If given value is already of requested type, value is returned as is.
Related
I have to send request to a third party service where the request JSON looks similar to below. There will be another api which has the same exact JSON format as the response. So I need to build object model that will be able to successfully serialize to/ deserialize from this JSON format.
Sample json
{
"name": "Johnny",
"vehicles": [{
"vehicleType": "car",
"vehicleInfo": {
"maxSeatCount": 2,
"coupe": true
}
}, {
"vehicleType": "truck",
"vehicleInfo": {
"towingCapacity": "3000lb",
"bedLength": "6ft"
}
}]
}
Here are the POJOs I created to meet the above model.
PersonInfo.java
public class PersonInfo {
private String name;
private List<Vehicle> vehicles;
}
Vehicle.java
public class Vehicle {
private String vehicleType;
private VehicleInfo vehicleInfo;
}
VehicleInfo.java
#JsonTypeInfo(use = Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "vehicleType")
#JsonSubTypes({
#Type(value = CarInfo.class, name="car"),
#Type(value = TruckInfo.class, name="truck")
})
public abstract class VehicleInfo {
}
CarInfo.java
public class CarInfo extends VehicleInfo{
private int maxSeatCount;
private boolean coupe;
}
TruckInfo.java
public class TruckInfo extends VehicleInfo{
private String towingCapacity;
private String bedLength;
}
I'm running into two problems with this model. During serialization, the JSON generated has the attribute vehicleType inside vehicleInfo object as well. It should not be.
JSON generated using above model.
{
"name" : "Johnny",
"vehicles" : [ {
"vehicleType" : "car",
"vehicleInfo" : {
"vehicleType" : "car", // this shouldn't be here
"maxSeatCount" : 2,
"coupe" : false
}
}, {
"vehicleType" : "truck",
"vehicleInfo" : {
"vehicleType" : "truck", // this shouldn't be here
"towingCapacity" : "3000lb",
"bedLength" : "6ft"
}
} ]
}
Second issue is that during deserialization, Jackson is complaining that it doesn't see the vehicleType attribute in vehicleInfo type.
Exception in thread "main"
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not
resolve subtype of [simple type, class
com.kodakandla.file.VehicleInfo]: missing type id property
'vehicleType' (for POJO property 'vehicleInfo') at [Source:
(String)"{"name":"Johnny","vehicles":[{"vehicleType":"car","vehicleInfo":{"maxSeatCount":2,"coupe":true}},{"vehicleType":"truck","vehicleInfo":{"towingCapacity":"3000lb","bedLength":"6ft"}}]}";
line: 1, column: 95] (through reference chain:
com.kodakandla.file.PersonInfo["vehicles"]->java.util.ArrayList[0]->com.kodakandla.file.Vehicle["vehicleInfo"])
at
com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
at
com.fasterxml.jackson.databind.DeserializationContext.missingTypeIdException(DeserializationContext.java:2083)
at
com.fasterxml.jackson.databind.DeserializationContext.handleMissingTypeId(DeserializationContext.java:1596)
What changes do I need to make for this to work?
I figured out what was wrong with my POJOs. Thought I would post the answer just in case if anyone else runs into similar issue.
I have the #JsonTypeInfo annotation in the wrong place. Instead of setting it at the class level in the VehicleInfo class, I had to set it at the field level in the Vehicle class.
public class Vehicle {
private String vehicleType;
#JsonTypeInfo(use = Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "vehicleType")
#JsonSubTypes({
#Type(value = CarInfo.class, name="car"),
#Type(value = TruckInfo.class, name="truck")
})
private VehicleInfo vehicleInfo;
}
Now the serialization and deserialization are working as expected.
I have the answer to my own question, so I post both the answer and solution, as explicitly encouraged by Jeff Atwood. My question was originally for Kotlin, but while trying to find a solution, I also tried Java, so I provide the question and solution in both Java and Kotlin.)
Question in Kotlin
Given this deserializable Product class:
data class Product(val name: String, val prices: List<Int>)
and this json string that lacks the prices field:
{"name": "Computer"}
how can I deserialize the json string to a Product object using Jackson?
What I have tried in Kotlin
I tried this:
data class Product(val name: String, val prices: List<Int>)
// Missing "prices" field
val json = """{"name": "Computer"}"""
// "prices" field included works fine
// val json = """{"name": "Computer", "prices": [1,2,3]}"""
val mapper = ObjectMapper().registerKotlinModule()
val product = mapper.readValue<Product>(json)
println(product)
but it results in this exception:
com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of
[simple type, class MainKt$main$Product] value failed for JSON property prices due to
missing (therefore NULL) value for creator parameter prices which is a non-nullable type
at [Source: (String)"{"name": "Computer"}"; line: 1, column: 20]
(through reference chain: MainKt$main$Product["prices"])
When using Java
For Java the Product class would be:
class Product {
private String name;
private List<Integer> prices;
public Product(String name, List<Integer> prices) {
this.name = name;
this.prices = prices;
}
#Override
public String toString() {
return "Product{name='" + name + "\", prices=" + prices + '}';
}
}
with this Jackson code:
String json = "{\"name\": \"Computer\"}";
// String json = "{\"name\": \"Computer\", \"prices\": [1,2,3]}";
ObjectMapper mapper = new ObjectMapper();
// ParameterNamesModule is needed for non-arg constructor when not using Jackson annotations
mapper.registerModule(new ParameterNamesModule());
Product product = mapper.readValue(json, Product.class);
// Shows "prices=null", while "prices=[]" is required
System.out.println(product);
But this sets prices to null instead of an empty list.
Solution in Kotlin
This solution is for Jackson 2.11 and higher. It uses the jackson-module-kotlin Maven artifact.
val kotlinModule = KotlinModule.Builder()
.configure(KotlinFeature.NullToEmptyCollection, true)
.build()
val mapper = ObjectMapper().registerModule(kotlinModule)
val product = mapper.readValue(json, Product::class.java)
println(product)
So the solution uses KotlinFeature.NullToEmptyCollection, which has the following documentation:
Default: false. Whether to deserialize null values for collection
properties as empty collections.
There is also a map version: KotlinFeature.NullToEmptyMap.
For version 2.9 and 2.10 you can use the nullToEmptyCollection default parameter of the KotlinModule constructor.
Solution in Java using annotations
Annotated Product class:
class Product {
private String name;
private List<Integer> prices;
public Product(#JsonProperty("name") String name,
#JsonProperty("prices")
#JsonSetter(nulls = Nulls.AS_EMPTY) List<Integer> prices
) {
this.name = name;
this.prices = prices;
}
#Override
public String toString() {
return "Product{name='" + name + "\', prices=" + prices + '}';
}
}
Jackson code:
String json = "{\"name\": \"Computer\"}";
ObjectMapper mapper = new ObjectMapper();
Product product = mapper.readValue(json, Product.class);
System.out.println(product); // Product{name='Computer', prices=[]}
The key part in this solution is #JsonSetter(nulls = Nulls.AS_EMPTY), which sets the missing or null json field to an empty list in Java.
The number of verbose annotations, such as #JsonProperty("prices") can be reduced by using the jackson-module-parameter-names Maven artifact. Then only #JsonSetter(nulls = Nulls.AS_EMPTY) is needed.
Solution in Java without annotations
This solution requires the jackson-module-parameter-names Maven artifact. When using this module/artifact, don't forget to add the -parameters compiler argument.
Product class Jackson without annotations:
class Product {
private String name;
private List<Integer> prices;
public Product(String name, List<Integer> prices) {
this.name = name;
this.prices = prices;
}
#Override
public String toString() {
return "Product{name='" + name + "\", prices=" + prices + '}';
}
}
Jackson code:
String json = "{\"name\": \"Computer\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ParameterNamesModule());
mapper.setDefaultSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
Product product = mapper.readValue(json, Product.class);
System.out.println(product);
The ParameterNamesModule model is required to allow Jackson to reflect the Product constructor parameters by name, so that #JsonProperty("prices") isn't required anymore.
And JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY) is used to convert missing or null json fields to a list.
Your Product class need to implement Serializable class. It can make consistency data.
class Product implements Serializable {
..............
}
I'm using MongoDb for unstructured documents. When I do the aggregations, I'm getting final output as unstructured objects. I post some sample data for the easiness. Actual objects have many fields.
Eg :
[
{ _id : "1", type: "VIDEO", videoUrl : "youtube.com/java"},
{ _id : "2", type: "DOCUMENT", documentUrl : "someurl.com/spring-boot-pdf"},
{ _id : "3", type: "ASSESSMENT", marks : 78}
]
The respective class for the types of above objects are
#Data
public class Video{
private String _id;
private String type;
private String videoUrl;
}
#Data
public class Document{
private String _id;
private String type;
private String documentUrl;
}
#Data
public class Assessment{
private String _id;
private String type;
private Integer marks;
}
Since I can't specify the converter class, I get all objects as list of Object.class which is a general type for all.
List<Object> list = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
It's working, but this is not readable and not maintainable for backend and front-end developers (eg : swagger ui). So I came up with a solution, that put all fields as a class.
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
class MyConvetor{
private String _id;
private String type;
private String videoUrl;
private String documentUrl;
private Integer marks;
}
Here Jackson helps to ignore all null fields
Now I can use MyConverter as Type
List<MyConverter> list = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), MyConverter.class).getMappedResults();
But I feel this is not a good practice when we implementing a standard application. I'd like to know, is there any way to avoid the general type class (e.g. extending any abstract class)? Or is this the only way I can do?
I don't think so (or I don't know) if MongoDB in Java provides this kind of dynamic conversion by some field (it would require specify what field and what classes). But you can do it by hand.
First, you need to define your types (enum values or some map) for matching string to class. You can create abstract parent class (eg. TypedObject) for easier usage and binding all target classes (Video, Document, Assessment) .
Next you have to read and map values from Mongo to anything because you want to read all data in code. Object is good but I recommend Map<String, Object> (your Object actually is that Map - you can check it by invoking list.get(0).toString()). You can also map to String or DBObject or some JSON object - you have to read "type" field by hand and get all data from object.
At the end you can convert "bag of data" (Map<String, Object> in my example) to target class.
Now you can use converted objects by target classes. For proving these are actually target classes I print objects with toString all fields.
Example implementation
Classes:
#Data
public abstract class TypedObject {
private String _id;
private String type;
}
#Data
#ToString(callSuper = true)
public class Video extends TypedObject {
private String videoUrl;
}
#Data
#ToString(callSuper = true)
public class Document extends TypedObject {
private String documentUrl;
}
#Data
#ToString(callSuper = true)
public class Assessment extends TypedObject {
private Integer marks;
}
Enum for mapping string types to classes:
#RequiredArgsConstructor
public enum Type {
VIDEO("VIDEO", Video.class),
DOCUMENT("DOCUMENT", Document.class),
ASSESSMENT("ASSESSMENT", Assessment.class);
private final String typeName;
private final Class<? extends TypedObject> clazz;
public static Class<? extends TypedObject> getClazz(String typeName) {
return Arrays.stream(values())
.filter(type -> type.typeName.equals(typeName))
.findFirst()
.map(type -> type.clazz)
.orElseThrow(IllegalArgumentException::new);
}
}
Method for converting "bag of data" from JSON to your target class:
private static TypedObject toClazz(Map<String, Object> objectMap, ObjectMapper objectMapper) {
Class<? extends TypedObject> type = Type.getClazz(objectMap.get("type").toString());
return objectMapper.convertValue(objectMap, type);
}
Read JSON to "bags of data" and use of the above:
String json = "[\n" +
" { _id : \"1\", type: \"VIDEO\", videoUrl : \"youtube.com/java\"},\n" +
" { _id : \"2\", type: \"DOCUMENT\", documentUrl : \"someurl.com/spring-boot-pdf\"},\n" +
" { _id : \"3\", type: \"ASSESSMENT\", marks : 78}\n" +
"]";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
List<Map<String, Object>> readObjects = objectMapper.readValue(json, new TypeReference<>() {});
for (Map<String, Object> readObject : readObjects) {
TypedObject convertedObject = toClazz(readObject, objectMapper);
System.out.println(convertedObject);
}
Remarks:
In example I use Jackson ObjectMapper for reading JSON. This makes the example and testing simpler. I think you can replace it with mongoTemplate.aggregate(). But anyway I need ObjectMapper in toClazz method for converting "bags of data".
I use Map<String, Object> instead of just Object. It is more complicated: List<Map<String, Object>> readObjects = objectMapper.readValue(json, new TypeReference<>() {});. If you want, you can do something like this: List<Object> readObjects2 = (List<Object>) objectMapper.readValue(json, new TypeReference<List<Object>>() {});
Result:
Video(super=TypedObject(_id=1, type=VIDEO), videoUrl=youtube.com/java)
Document(super=TypedObject(_id=2, type=DOCUMENT), documentUrl=someurl.com/spring-boot-pdf)
Assessment(super=TypedObject(_id=3, type=ASSESSMENT), marks=78)
Of course you can cast TypedObject to target class you need (I recommend checking instance of before casting) and use:
Video video = (Video) toClazz(readObjects.get(0), objectMapper);
System.out.println(video.getVideoUrl());
I assumed you read whole collection once and you get all types mixed up in one list (as in example in your question). But you can try find documents in MongoDB by field "type" and get data separately for each of type. With this you can easily convert to each type separately.
I am trying to deserialize JSON into a custom POJO that I am not able to modify. That POJO has annotations from a different custom internal serialization framework that I'm not able to use. How can I create a custom deserializer that will respect these annotations?
Here is an example POJO:
public class ExampleClass {
#Property(name = "id")
public String id;
#Property(name = "time_windows")
#NotNull
public List<TimeWindow> timeWindows = new ArrayList<>();
public static class TimeWindow {
#Property(name = "start")
public Long start;
#Property(name = "end")
public Long end;
}
}
So in this case, the deserializer would look for fields in the JSON that correspond to the Property annotations, and use the value in that annotation to decide what field to grab. If a property doesn't have the Property annotation, it should be ignored.
I have been going through the Jackson docs but haven't been able to find exactly what I need. Is this a place where an AnnotationIntrospector would be useful? Or possibly a ContextualDeserializer?
Any pointers in the right direction would be greatly appreciated!
Update: I tried implementing the advice in the comments, but without success.
Here is my initial implementation of the introspector:
class CustomAnnotationInspector : JacksonAnnotationIntrospector () {
override fun hasIgnoreMarker(m: AnnotatedMember?): Boolean {
val property = m?.getAnnotation(Property::class.java)
return property == null
}
override fun findNameForDeserialization(a: Annotated?): PropertyName {
val property = a?.getAnnotation(Property::class.java)
return if (property == null) {
super.findNameForDeserialization(a)
} else {
PropertyName(property.name)
}
}
}
And here is where I actually use it:
// Create an empty instance of the request object.
val paramInstance = nonPathParams?.type?.getDeclaredConstructor()?.newInstance()
// Create new object mapper that will write values from
// JSON into the empty object.
val mapper = ObjectMapper()
// Tells the mapper to respect custom annotations.
mapper.setAnnotationIntrospector(CustomAnnotationInspector())
// Write the contents of the request body into the StringWriter
// (this is required for the mapper.writeValue method
val sw = StringWriter()
sw.write(context.bodyAsString)
// Deserialize the contents of the StringWriter
// into the empty POJO.
mapper.writeValue(sw, paramInstance)
Unfortunately it seems that findNameForDeserialization is never called, and none of the JSON values are written into paramInstance. Can anybody spot where I'm going wrong?
Thank you!
Update 2: I changed the code slightly, I'm now able to identify the property names but Jackson is failing to create an instance of the object.
Here's my new code:
val mapper = ObjectMapper()
// Tells the mapper to respect CoreNg annotations.
val introspector = CustomAnnotationInspector()
mapper.setAnnotationIntrospector(introspector)
val paramInstance = mapper.readValue(context.bodyAsString,nonPathParams?.type)
My breakpoints in the custom annotation introspector are getting hit. But I'm getting the following exception:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `app.employee.api.employee.BOUpsertEmployeeRequest` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
Here is the POJO I'm trying to deserialize:
public class BOUpsertEmployeeRequest {
public BOUpsertEmployeeRequest () { }
#NotNull
#Property(name = "xref_code")
public String xrefCode;
#Property(name = "first_name")
public String firstName;
#Property(name = "last_name")
public String lastName;
#Property(name = "email_address")
public String emailAddress;
#Property(name = "phone")
public String phone;
#Property(name = "address")
public List<String> address;
#Property(name = "employment_status")
public String employmentStatus;
#Property(name = "pay_type")
public String payType;
#Property(name = "position")
public String position;
#Property(name = "skills")
public List<String> skills;
#Property(name = "gender")
public String gender;
}
As far as I can tell it has a default constructor. Anybody have any idea what the problem is?
Thank you!
Method hasIgnoreMarker is called not only for fields, but also for the constructor, including the virtual one:
Method called to check whether given property is marked to be ignored. This is used to determine whether to ignore properties, on per-property basis, usually combining annotations from multiple accessors (getters, setters, fields, constructor parameters).
In this case you should ignore only fields, that are not marked properly:
static class CustomAnnotationIntrospector extends JacksonAnnotationIntrospector {
#Override
public PropertyName findNameForDeserialization(Annotated a) {
Property property = a.getAnnotation(Property.class);
if (property == null) {
return PropertyName.USE_DEFAULT;
} else {
return PropertyName.construct(property.name());
}
}
#Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
return m instanceof AnnotatedField
&& m.getAnnotation(Property.class) == null;
}
}
Example:
class Pojo {
// #Property(name = "id")
Integer id;
// #Property(name = "number")
Integer number;
#Property(name = "assure")
Boolean assure;
#Property(name = "person")
Map<String, String> person;
}
String json =
"{\"id\" : 1, \"number\" : 12345, \"assure\" : true," +
" \"person\" : {\"name\" : \"John\", \"age\" : 23}}";
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new CustomAnnotationIntrospector());
Pojo pojo = mapper.readValue(json, Pojo.class);
System.out.println(pojo);
Pojo{id=null, number=null, assure=true, person={name=John, age=23}}
Note: Custom Property annotation should have RetentionPolicy.RUNTIME (same as JsonProperty annotation):
#Retention(RetentionPolicy.RUNTIME)
public #interface Property {
String name();
}
I will suggest a different approach:
In the runtime, with the bytecode instrumentation library Byte Buddy and its Java agent, re-annotate the fields with the proper Jackson Annotations. Simply implement the logic via reflection. See the following example:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.dynamic.DynamicType.Builder;
import net.bytebuddy.dynamic.DynamicType.Builder.FieldDefinition.Valuable;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import net.bytebuddy.matcher.ElementMatchers;
#Target(ElementType.FIELD)
#Retention(RetentionPolicy.RUNTIME)
#interface MyJsonIgnore {
}
#Target(ElementType.FIELD)
#Retention(RetentionPolicy.RUNTIME)
#interface MyJsonProperty {
String name();
}
public class Sample {
public static void main(String[] args) throws JsonProcessingException {
ByteBuddyAgent.install();
ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent();
ByteBuddy byteBuddy = new ByteBuddy();
AnnotationDescription jsonIgnoreDesc =
AnnotationDescription.Builder.ofType(JsonIgnore.class).build();
Builder<Person> personBuilder = byteBuddy.redefine(Person.class);
for (Field declaredField : Person.class.getDeclaredFields()) {
Valuable<Person> field = personBuilder.field(ElementMatchers.named(declaredField.getName()));
MyJsonProperty myJsonProperty = declaredField.getAnnotation(MyJsonProperty.class);
if (myJsonProperty != null) {
AnnotationDescription jsonPropertyDesc =
AnnotationDescription.Builder.ofType(JsonProperty.class)
.define("value", myJsonProperty.name())
.build();
personBuilder = field.annotateField(jsonPropertyDesc);
}
MyJsonIgnore myJsonIgnore = declaredField.getAnnotation(MyJsonIgnore.class);
if (myJsonIgnore != null) {
personBuilder = field.annotateField(jsonIgnoreDesc);
}
}
personBuilder.make().load(Sample.class.getClassLoader(), classReloadingStrategy);
Person person = new Person("Utku", "Ozdemir", "Berlin");
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(person);
System.out.println(jsonString);
}
}
class Person {
#MyJsonProperty(name = "FIRST")
private String firstName;
#MyJsonProperty(name = "LAST")
private String lastName;
#MyJsonIgnore private String city;
public Person(String firstName, String lastName, String city) {
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
}
}
In the example above, I
create MyJsonProperty and MyJsonIgnore annotations and a Person class for the demonstration purpose
instrument the current Java process with the Byte buddy agent
create a bytebuddy builder to redefine the Person class
loop over the fields of the Person class and check for these annotations
add an additional annotation to each of those fields (on the builder), Jackson's JsonProperty (with the correct field name mapping) and JsonIgnore.
after being done with the fields, make the new class bytecode and load it to the current classloader using the byte buddy agent's class reloading mechanism
write a person object to the stdout.
It prints, as expected:
{"FIRST":"Utku","LAST":"Ozdemir"}
(the field city is ignored)
This solution might feel like an overkill, but on the other side, it is pretty generic solution - with a few changes in the logic, you could handle all the 3rd party classes (which you are not able to modify) instead of handling them case by case.
I have an Entity class below with two String fields: name and description. The description field is to contain a raw JSON value e.g. { "abc": 123 }
#Getter
#Setter
public class Entity {
private String name;
#JsonRawValue
private String descriptionJson;
}
I've got simple test code below using Jackson to serialize and deserialize:
Entity ent = new Entity();
ent.setName("MyName");
ent.setDescriptionJson("{ \"abc\": 123 }");
// Convert Object to JSON string
String json = mapper.writeValueAsString(ent);
// Convert JSON string back to object
Entity ent2 = mapper.readValue(json, Entity.class);
When converting Object -> JSON the description string is nested because the #JsonRawValue is set:
{"name":"MyName","descriptionJson":{ "abc": 123 }}
However, when I call the Jackson mapper.readValue function to read the JSON string back into an entity object I get the exception:
com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
at [Source: (String)"{"name":"MyName","descriptionJson":{ "abc": 123 }}"; line: 1, column: 36] (through reference chain: com.test.Entity["descriptionJson"])
Given that the #JsonRawValue annotation exists, how would you recommend marshalling the created JSON string back into to Entity object? Is there another annotation I'm missing?
Thanks
#JsonRawValue is intended for serialization-side only, but in this problem you can do like this:
#Getter
#Setter
public class Entity {
private String name;
#JsonRawValue
private String descriptionJson;
#JsonProperty(value = "descriptionJson")
public void setDescriptionJsonRaw(JsonNode node) {
this.descriptionJson = node.toString();
}
}
This problem is repeated with
How can I include raw JSON in an object using Jackson?.
For one of my requirements I used field type as Map to store Json as it is. This way I was able to read the nested JSOn as Map and when I serialize object to JSON, it came up correctly. Below is the example.
Entity.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Entity {
public int id=0;
public String itemName="";
public Map<String,String> owner=new HashMap<>();
}
Temp.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Temp {
public static void main(String[] args){
ObjectMapper objectMapper= new ObjectMapper();
try {
Entity entity
=objectMapper.readValue(Temp.class.getResource("sample.json"), Entity.class);
System.out.println(entity);
String json=objectMapper.writeValueAsString(entity);
System.out.println(json);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sample.json
{
"id": 1,
"itemName": "theItem",
"owner": {
"id": 2,
"name": "theUser"
}
}
You can use ObjectMapper from Jackson 2 as follows:
ObjectMapper mapper = new ObjectMapper();
String jsonStr = "sample json string"; // populate this as required
MyClass obj = mapper.readValue(jsonStr,MyClass.class)
try escaping the curly braces in the description json's value.
#JsonRawValue is intended only sor serializatio from docs:
Marker annotation that indicates that the annotated method or field should be serialized by including literal String value of the property as is, without quoting of characters.
To solve your problem you can try
public class Entity {
#Getter
#Setter
private String name;
private String descriptionJson;
#JsonRawValue
public String getDescriptionJson() {
return descriptionJson;
}
public void setJson(JsonNode node) {
this.descriptionJson = node.toString();
}
}