Can not deserialize instance of java.util.ArrayList out of VALUE_STRING

This is the solution for my old question: I implemented my own ContextResolver in order to enable the DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY feature. package org.lig.hadas.services.mapper; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; @Produces(MediaType.APPLICATION_JSON) @Provider public class ObjectMapperProvider implements ContextResolver<ObjectMapper> { ObjectMapper mapper; public ObjectMapperProvider(){ mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); } @Override public … Read more

Decode JSON string in Java with json-simple library

This is the best and easiest code: public class test { public static void main(String str[]) { String jsonString = “{\”stat\”: { \”sdr\”: \”aa:bb:cc:dd:ee:ff\”, \”rcv\”: \”aa:bb:cc:dd:ee:ff\”, \”time\”: \”UTC in millis\”, \”type\”: 1, \”subt\”: 1, \”argv\”: [{\”type\”: 1, \”val\”:\”stackoverflow\”}]}}”; JSONObject jsonObject = new JSONObject(jsonString); JSONObject newJSON = jsonObject.getJSONObject(“stat”); System.out.println(newJSON.toString()); jsonObject = new JSONObject(newJSON.toString()); System.out.println(jsonObject.getString(“rcv”)); System.out.println(jsonObject.getJSONArray(“argv”)); } … Read more

json deserialize from legacy property names

This can be done with a custom IContractResolver created by extending DefaultContractResolver: [System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field, AllowMultiple = false, Inherited = true)] public class LegacyDataMemberNamesAttribute : Attribute { public LegacyDataMemberNamesAttribute() : this(new string[0]) { } public LegacyDataMemberNamesAttribute(params string[] names) { this.Names = names; } public string [] Names { get; set; } } public class LegacyPropertyResolver … Read more

Modify existing object with new partial JSON data using Json.NET

You want JsonSerializer.Populate() or its static wrapper method JsonConvert.PopulateObject(): Populates the JSON values onto the target object. For instance, here it is updating an instance of your Calendar class: public static class TestPopulate { public static void Test() { var calendar = new Calendar { Id = 42, CoffeeProvider = “Espresso2000”, Meetings = new[] { … Read more

DateTime column type becomes String type after deserializing DataTable

The basic problem here is that Json.NET’s DataTableConverter infers each DataColumn.DataType by looking at token values present in the first row only. It works this way because it streams the JSON for the table in rather than loading the entirety into an intermediate JToken hierarchy. While streaming gives better performance with reduced memory use, it … Read more

Is Jackson’s @JsonSubTypes still necessary for polymorphic deserialization?

There are two ways to achieve polymorphism in serialization and deserialization with Jackson. They are defined in Section 1. Usage in the link you posted. Your code @JsonTypeInfo( use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = “@class”) is an example of the second approach. The first thing to note is that All instances of annotated … Read more

What is deserialize and serialize in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization … Read more