Serialize, deserialize using jackson - java

I was trying convert to my object to and from json but the default serializer, deserializer by jackson doesn't work.
How can I make this work? I understand I might need to write a custom serializer, deserializer. How can I do that?
Is ther some annotation by adding which the code would work?
Here is the object:
#JsonDeserialize(keyUsing = mypairDeserializer.class)
#JsonSerialize(keyUsing = mypairSerializer.class)
HashMap<Set < Mypair > , List < Mypair > > obj;
public class ConditionSerializer extends JsonSerializer<Collection<mypair>> {
#Override
public void serialize(final Collection<mypair> conditionSet, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("Pair");
jsonGenerator.writeStartArray();
for(final Condition condition: conditionSet) {
jsonGenerator.writeString(mypair.toString());
}
jsonGenerator.writeEndArray();
jsonGenerator.writeEndObject();
}
}
public class mypairDeserializer extends KeyDeserializer {
ObjectMapper mapper = new ObjectMapper();
#Override
public Collection<mypair> deserializeKey(final String key, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
// return new mypair(key);
return mapper.readValue(key, Collection.class);
}
}

Hi again from last post,
So, this is an example of what you can do :
Note that since I don't know what is your object Mypair, I did this example with a User class :
public class User {
private int id;
private String name;
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
// getters & setters
}
The class containing your complex object :
public class YourClass {
#JsonSerialize(using = ComplexObjectSerializer.class)
private Map<Set<User>, List<User>> object;
public YourClass(Map<Set<User>, List<User>> object) {
this.object = object;
}
public Map<Set<User>, List<User>> getObject() {
return object;
}
public void setObject(Map<Set<User>, List<User>> object) {
this.object = object;
}
}
The custom serializer :
public class ComplexObjectSerializer extends StdSerializer<Map<Set<User>, List<User>>> {
public ComplexObjectSerializer() {
this(null);
}
public ComplexObjectSerializer(Class<Map<Set<User>, List<User>>> t) {
super(t);
}
private static final long serialVersionUID = 1L;
#Override
public void serialize(Map<Set<User>, List<User>> complexObject,
JsonGenerator jsonGen, SerializerProvider arg2) throws IOException {
// Suppose you want the following json:
/**
* [ { "set":[], "list":[] } ]
*/
jsonGen.writeStartArray(); // [
for (Entry<Set<User>, List<User>> entry : complexObject.entrySet()) {
jsonGen.writeStartObject(); // {
jsonGen.writeObjectField("set", entry.getKey()); // It will call the default serializer for a Set<User>, ie : [ {"id": 0, "name":"string"} ]
jsonGen.writeObjectField("list", entry.getValue()); // It will call the default serializer for a List<User>, ie the same thing as the Set above
jsonGen.writeEndObject(); // }
}
jsonGen.writeEndArray(); // ]
}
}
Main :
Map<Set<User>, List<User>> complexObject = new HashMap<Set<User>, List<User>>();
// Add some data in the map ...
YourClass yourClass = new YourClass(complexObject);
// Serialize your object
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(yourClass); // It will call your custom serializer
System.out.println(json);
Output :
{
"object": [
{
"set": [
{
"id": 5,
"name": "userName5"
},
{
"id": 6,
"name": "userName6"
}
],
"list": [
{
"id": 2,
"name": "userName2"
}
]
},
{
"set": [
{
"id": 4,
"name": "userName4"
},
{
"id": 3,
"name": "userName3"
}
],
"list": [
{
"id": 0,
"name": "userName0"
},
{
"id": 1,
"name": "userName1"
}
]
}
]
}

Related

Jackson deserialize JSON into pojo with map property

Can somebody help me, how I can deserialize the following JSON, which I can not change?
I am using Jackson for serialization.
{
"columns": [
{
"header": "Heading1",
},
{
"header": "Heading2",
}
],
"rows": [
"id": 1,
"Heading1": {
"value": "Value1"
},
"Heading2": {
"value": "Value2"
}
]
}
Columns can have unknown number of headers and their value eg. "Header1" is used in the rows array.
So far I have the following structure:
public class QueryResult {
private ColumnConfig[] columns;
private QueryResultRow[] rows;
}
public class ColumnConfig {
private String header;
}
public class QueryResultRow {
private int id;
private Map<String, CellValue> values;
}
public class CellValue{
private String value;
}
The problem is that the Map is empty when I deserialize into QueryResult;
I read about TypeReference but I do not know how I can specify a TypeReference<HashMap<String,CellValue>> for the property values in QueryResultRow.
Edit:
My ObjectMapper code is the following:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String queryResultString = loadQuery(queryPath);
QueryResult result = mapper.readValue(queryResultString, QueryResult.class);
The content of queryResultString is the JSON above.
First problem is your JSON is invalid. I assume it should be something like this,
{
"columns": [
{
"header": "Heading1"
},
{
"header": "Heading2"
}
],
"rows": [
{
"id": 1,
"Heading1": {
"value": "Value1"
},
"Heading2": {
"value": "Value2"
}
}
]
}
Then answer is quite straightforward. You need to change your QueryResultRow as follows,
class QueryResultRow {
private int id;
private Map<String, CellValue> values = new HashMap<>();
#JsonAnySetter
public void addValues(String k, CellValue v) {
values.put(k, v);
}
}
Then I think you should good to go.
Here is a complete working example,
public class Main {
public static void main(String[] args) throws IOException {
String s = "{\"columns\":[{\"header\":\"Heading1\"},{\"header\":\"Heading2\"}],\"rows\":[{\"id\":1,\"Heading1\":{\"value\":\"Value1\"},\"Heading2\":{\"value\":\"Value2\"}}]}";
ObjectMapper om = new ObjectMapper();
QueryResult queryResult = om.readValue(s, QueryResult.class);
System.out.println(queryResult);
}
}
#Getter
#Setter
#ToString
class QueryResult {
private ColumnConfig[] columns;
private QueryResultRow[] rows;
}
#Getter
#Setter
#ToString
class ColumnConfig {
private String header;
}
#Getter
#Setter
#ToString
class QueryResultRow {
private int id;
private Map<String, CellValue> values = new HashMap<>();
#JsonAnySetter
public void addValues(String k, CellValue v) {
values.put(k, v);
}
}
#Getter
#Setter
#ToString
class CellValue{
private String value;
}

How to handle varying data by one POJO

I'm trying to create a single POJO for the varying JSON data. Is there is a way to implement this by a single class only? and without writing Serializer and Deserializer?
ResultTwo Example:
{ "results": ["24","0","18","34","27"] }
ResultOne Example:
{
"results": [
{
"value": "2|2|5"
},
{
"value": "2|3|4",
"multiplier": 25
},
{
"value": "2|3|5"
},
{
"value": "2|3|4",
"multiplier": 50
},
{
"value": "1|1|4"
},
{
"value": "3|6|6",
"multiplier": 30
}
]
}
What I've tried:
BaseResult
public class BaseResult { }
ResultOne
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultOne extends BaseResult {
// {"value":"2|6|6","multiplier":25}
// or
// {"value":"4|4|4"}
private String value;
private Integer multiplier;
}
ResultTwo
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultTwo extends BaseResult {
private String result;
}
ResultSerializer
public class ResultSerializer extends StdSerializer<BaseResult> {
public ResultSerializer(Class<BaseResult> t) {
super(t);
}
#Override
public void serialize(
BaseResult value, JsonGenerator gen, SerializerProvider arg2)
throws IOException {
if(value instanceof ResultTwo){
ResultTwo result = (ResultTwo)value;
gen.writeString(result.getResult());
}else if(value instanceof ResultOne){
ResultOne resultOne = (ResultOne)value;
gen.writeObject(resultOne);
}else{
throw new ClassCastException("BaseResult value doesn't have any known type: " + value);
}
}
}
ResultDeserializer
public class ResultDeserializer
extends StdDeserializer<BaseResult> {
public ResultDeserializer(Class<?> vc) {
super(vc);
}
#Override
public BaseResult deserialize(
JsonParser jsonparser, DeserializationContext context)
throws IOException {
String content = jsonparser.getText();
JsonNode node = jsonparser.getCodec().readTree(jsonparser);
if(node instanceof ObjectNode){
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.treeToValue(node, ResultOne.class);
}else{
ResultTwo resultTwo = new ResultTwo();
resultTwo.setResult(content);
return resultTwo;
}
}
}
On parent class I've annotated this:
#JsonSerialize(contentUsing = ResultSerializer.class)
#JsonDeserialize(contentUsing = ResultDeserializer.class)
private List<BaseResult> results = null;
#Kulsin -
In spring controller you can use Map<String, Object> as request body, which can entertain your both the requests-
#RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public void testMethod(#RequestBody Map<String, Object> request) {
//access data using request reference.
}

How to parse nested JSON object in java

Here is the sample JSON string. I want to parse this nested JSON object even though nested object have the same name. Some time we may have multiple levels of the nested objects. I tired with Jackson nested objects parsing but that did not work for me. After parsing the object, i want to convert that into a different format.Please help me in parsing this JSON. Thanks in advance.
{
"operator": "and",
"predicates": [
{
"operator": "and",
"predicates": [
{
"columnName": "userName",
"datatype": "string",
"input": "text",
"operand": "equal",
"value": "xxxx"
},
{
"columnName": "Age",
"datatype": "number",
"input": "number",
"operand": "greater_or_equal",
"value": "21"
}
]
},
{
"operator": "and",
"predicates": [
{
"columnName": "userName",
"datatype": "string",
"input": "text",
"operand": "not_equal",
"value": "nnn"
},
{
"columnName": "Birthday",
"datatype": "date",
"input": "date_picker",
"operand": "in",
"value": "2020-07-23,2020-07-24"
}
]
}
]
}
below is the code in java
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Predicates {
private String columnName;
private String datatype;
private String input;
private String operator;
private String value;
private String operand;
/**
*
*/
private List<Predicates> predicates;
#JsonProperty("predicates")
private void unpackNested(Map<String,Object> predicates) {
this.columnName = (String)predicates.get("columnName");
this.datatype = (String)predicates.get("datatype");
this.input = (String)predicates.get("input");
this.operator = (String)predicates.get("operator");
this.value = (String)predicates.get("value");
this.operand = (String)predicates.get("operand");
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getDatatype() {
return datatype;
}
public void setDatatype(String datatype) {
this.datatype = datatype;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getOperand() {
return operand;
}
public void setOperand(String operand) {
this.operand = operand;
}
public List<Predicates> getPredicates() {
return predicates;
}
public void setPredicates(List<Predicates> predicates) {
this.predicates = predicates;
}
}
Parsing
ObjectMapper mapper = new ObjectMapper();
Predicates pr = mapper.readValue(json, Predicates.class);
I don't know what you are trying to achieve with your unpackNested method. Jackson can already bind properties from your JSON to your objects and handles recursive properties just fine.
I simply removed your unpackNested method and ran your code on your provided input:
ObjectMapper mapper = new ObjectMapper();
Predicates pr = mapper.readValue(json, Predicates.class);
The object pr contains the full json including the nested child predicates. (I ran the code with jackson databind 2.11.2).
If your properties are not auto-detected, annotate your getters with #JsonProperty:
class Predicate {
// ..snip..
#JsonProperty("input")
public String getInput() {
return input;
}
#JsonProperty("predicates")
public List<Predicates> getPredicates() {
return predicates;
}
}
But apart from that, no extra steps need to be taken. Jackson can already unpack nested objects, just remove your (weird) unpackNested method and let Jackson do its job.

How to deserialize a Map<?, ?> with Jackson?

I'm trying to serialize/deserialize a Map<?, ?> with arbitrary object as keys with Jackson version 2.8. The JSON counterpart should be an array of couples, i.e. given
public class Foo {
public String foo;
public Foo(String foo) {
this.foo = foo;
}
}
public class Bar {
public String bar;
public Bar(String bar) {
this.bar = bar;
}
}
then
Map<Foo, Bar> map;
map.put(new Foo("foo1"), new Bar("bar1"));
map.put(new Foo("foo2"), new Bar("bar2"));
should be represented by this JSON
[
[ { "foo": "foo1" }, { "bar": "bar1" } ],
[ { "foo": "foo2" }, { "bar": "bar2" } ]
]
So I did the serializer part as
public class MapToArraySerializer extends JsonSerializer<Map<?, ?>> {
#Override
public void serialize(Map<?, ?> value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartArray();
for (Map.Entry<?, ?> entry : value.entrySet()) {
gen.writeStartArray();
gen.writeObject(entry.getKey());
gen.writeObject(entry.getValue());
gen.writeEndArray();
}
gen.writeEndArray();
}
}
but I have no idea how to write a JsonDeserializer to do the inverse job. Any suggestions?
Note: I need the [ [ "key1", "value1" ], [ "key2", "value2" ] ] notation to be able to consume that JSON in JavaScript a new Map( ... ) and JSON.stringify(map) would produce that notation too (see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map).
To clarify, such a map would be a field of other classes, e.g.
public class Baz {
#JsonSerialize(using = MapToArraySerializer.class)
#JsonDeserialize(using = ArrayToMapDeserializer.class, keyAs = Foo.class, contentAs = Bar.class)
Map<Foo, Bar> map;
}
and ArrayToMapDeserializer extends JsonDeserializer<Map<?, ?>> is where I'm asking for help.
I came up with this solution:
public class ArrayToMapDeserializer extends JsonDeserializer<SortedMap<Object, Object>>
implements ContextualDeserializer {
private Class<?> keyAs;
private Class<?> contentAs;
#Override
public Map<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return this.deserialize(p, ctxt, new HashMap<>());
}
#Override
public Map<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt,
Map<Object, Object> intoValue) throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
ObjectCodec codec = p.getCodec();
if (node.isArray()) {
node.forEach(entry -> {
try {
JsonNode keyNode = entry.get(0);
JsonNode valueNode = entry.get(1);
intoValue.put(keyNode.traverse(codec).readValueAs(this.keyAs),
valueNode.traverse(codec).readValueAs(this.contentAs));
} catch (NullPointerException | IOException e) {
// skip entry
}
});
}
return intoValue;
}
#Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
JsonDeserialize jsonDeserialize = property.getAnnotation(JsonDeserialize.class);
this.keyAs = jsonDeserialize.keyAs();
this.contentAs = jsonDeserialize.contentAs();
return this;
}
}
which can be used like this:
public class Baz {
#JsonSerialize(using = MapToArraySerializer.class)
#JsonDeserialize(using = ArrayToMapDeserializer.class,
keyAs = Foo.class, contentAs = Bar.class)
Map<Foo, Bar> map;
}
Here is the deserialize:
#Override
public Map<?, ?> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
Map map = new LinkedHashMap();
ObjectCodec oc = p.getCodec();
JsonNode anode = oc.readTree(p);
for (int i = 0; i < anode.size(); i++) {
JsonNode node = anode.get(i);
map.put(node.get(0), node.get(1));
}
return map;
}
I added a few test cases, with a new Oson implementation, to the original solution, in which I used oson to do the conversion, but with a different convension: map to json: {key1: value1, key2: value2, ...}, so the json output becomes:
{
{
"foo": "foo1"
}: {
"bar": "bar1"
},
{
"foo": "foo2"
}: {
"bar": "bar2"
}
}
You can check out the source code!

Jackson deserialization with unknown dynamic properties

I have a JSON string like:
"shipping_profiles": {
"563": {
"name": "name",
"value": "value"
},
"564": {
"name": "name",
"value": "value"
},
"565": {
"name": "name",
"value": "value"
},
"566": {
"name": "name",
"value": "value"
}
}
Now I am parsing it with Jackson 2.0.
I am trying to get a List<shipping_profiles> from the JSON string.
Is it possible?
Your shipping_profiles property doesn't look like array. It represent object with dynamic properties, so we should treat it like an object. If we do not know anything about properties we can use #JsonAnySetter annotation. Algorithm could looks like below:
Deserialize JSON into JSON-model classes.
Convert dynamic objects (maps) into app's POJO classes using ObjectMapper
Use app's POJO whenever you want.
Please see my example implementation. I hope, it help you solve your problem. Input JSON:
{
"shipping_profiles":{
"563":{
"name":"name563",
"value":"value563"
},
"564":{
"name":"name564",
"value":"value564"
},
"565":{
"name":"name565",
"value":"value565"
},
"566":{
"name":"name566",
"value":"value566"
}
}
}
Example program:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonProgram {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
File source = new File("X:/test.json");
Entity entity = mapper.readValue(source, Entity.class);
ShippingProfiles shippingProfiles = entity.getShippingProfiles();
List<Map<String, String>> profileMaps = shippingProfiles.getProfiles();
List<Profile> profiles = new ArrayList<Profile>(profileMaps.size());
for (Map<String, String> item : profileMaps) {
profiles.add(mapper.convertValue(item, Profile.class));
}
System.out.println(profiles);
}
}
class Entity {
#JsonProperty("shipping_profiles")
private ShippingProfiles shippingProfiles;
public ShippingProfiles getShippingProfiles() {
return shippingProfiles;
}
public void setShippingProfiles(ShippingProfiles shippingProfiles) {
this.shippingProfiles = shippingProfiles;
}
}
class ShippingProfiles {
private List<Map<String, String>> profiles = new ArrayList<Map<String, String>>();
#JsonAnySetter
public void setDynamicProperty(String name, Map<String, String> map) {
profiles.add(map);
}
public List<Map<String, String>> getProfiles() {
return profiles;
}
public void setProfiles(List<Map<String, String>> profiles) {
this.profiles = profiles;
}
}
class Profile {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
#Override
public String toString() {
return "Profile [name=" + name + ", value=" + value + "]";
}
}
Above app prints:
[Profile [name=name563, value=value563], Profile [name=name564, value=value564], Profile [name=name565, value=value565], Profile [name=name566, value=value566]]
I got my json with dynamic property parsed with the way #michalziober provide.
"commandClasses": {
"32": {
"name": "Basic",
"data": {
"name": "devices.1.instances.1.commandClasses.32.data",
"value": null,
"type": "NoneType"
},
"38": {
"name": "SwitchMultilevel",
"data": {
"name": "devices.1.instances.1.commandClasses.38.data",
"value": null,
"type": "NoneType"
},
"43": {
"name": "SceneActivation",
"data": {
"name": "devices.1.instances.1.commandClasses.43.data",
"value": null,
"type": "NoneType"
}
With this json I also need to save that dynamic property, so I add another List for storing it.
public class CommandClasses {
private List<String> nameList = new ArrayList<String>();
private List<CommandClass> commmandClasses = new ArrayList<CommandClass>();
private Logger logger = Logger.getInstance(CommandClasses.class);
#JsonAnySetter
public void setDynamicCommandClass(String name, CommandClass cc) {
logger.d("# adding new CC : " + name);
nameList.add(name);
commmandClasses.add(cc);
}
public List<CommandClass> getCommmandClasses() {
return commmandClasses;
}
public void setCommmandClasses(List<CommandClass> commmandClasses) {
this.commmandClasses = commmandClasses;
}
}
Now I can also access the field as id to send out request later.

Categories