Swift’s JSONDecoder with multiple date formats in a JSON string?

Please try decoder configurated similarly to this: lazy var decoder: JSONDecoder = { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in let container = try decoder.singleValueContainer() let dateStr = try container.decode(String.self) // possible date strings: “2016-05-01”, “2016-07-04T17:37:21.119229Z”, “2018-05-20T15:00:00Z” let len = dateStr.count var date: Date? = nil if len == 10 { … Read more

Using Gson in Kotlin to parse JSON array

You need to change parameter in your fromJson() function call like following: val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList() You need to pass Array<WeatherObject>::class.java for class type and then convert result into List. No need to change registerTypeAdapter() function call. Check following code: fun getWeatherObjectFromJson(jsonStr: String): List<WeatherObject> { var stringReader: StringReader = StringReader(jsonStr) var jsonReader: … Read more

Deserializing JSON when sometimes array and sometimes object

A very detailed explanation on how to handle this case is available at “Using a Custom JsonConverter to fix bad JSON results”. To summarize, you can extend the default JSON.NET converter doing Annotate the property with the issue [JsonConverter(typeof(SingleValueArrayConverter<OrderItem>))] public List<OrderItem> items; Extend the converter to return a list of your desired type even for … 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

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

Jackson – @JsonTypeInfo property is being mapped as null?

Finally, I’ve found the solution in the API Doc Note on visibility of type identifier: by default, deserialization (use during reading of JSON) of type identifier is completely handled by Jackson, and is not passed to deserializers. However, if so desired, it is possible to define property visible = true in which case property will … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)