Gson to json conversion with two DateFormat

I was facing the same issue. Here is my solution via custom deserialization: new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()); private static final String[] DATE_FORMATS = new String[] { “MMM dd, yyyy HH:mm:ss”, “MMM dd, yyyy” }; private class DateDeserializer implements JsonDeserializer<Date> { @Override public Date deserialize(JsonElement jsonElement, Type typeOF, JsonDeserializationContext context) throws JsonParseException { for (String format … Read more

How to handle a NumberFormatException with Gson in deserialization a JSON response

Here is an example that I made for Long type. This is a better option: public class LongTypeAdapter extends TypeAdapter<Long> { @Override public Long read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } String stringValue = reader.nextString(); try { Long value = Long.valueOf(stringValue); return value; } catch (NumberFormatException e) { … Read more

Retrofit GSON serialize Date from json string into java.util.date

Gson gson = new GsonBuilder() .setDateFormat(“yyyy-MM-dd’T’HH:mm:ss”) .create(); RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(API_BASE_URL) .setConverter(new GsonConverter.create(gson)) .build(); Or the Kotlin equivalent: val gson = GsonBuilder().setDateFormat(“yyyy-MM-dd’T’HH:mm:ss”).create() RestAdapter restAdapter = Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build() .create(T::class.java) You can set your customized Gson parser to retrofit. More here: Retrofit Website Look at Ondreju’s response to see how to implement this … Read more

Karaf / Maven – Unable to resolve: missing requirement osgi.wiring.package

I believe you have two options here. If you have Import-Package: com.google.gson;version=”[2.3,3)” in your MANIFEST.MF, this means that you want some package to be imported from a deployed bundle, not from an embedded jar. In this case, you should first deploy gson-2.3.1.jar bundle (copy this file to the deploy folder), and then deploy your bundle. … Read more

How to serialize a class with an interface?

Here is a generic solution that works for all cases where only interface is known statically. Create serialiser/deserialiser: final class InterfaceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> { public JsonElement serialize(T object, Type interfaceType, JsonSerializationContext context) { final JsonObject wrapper = new JsonObject(); wrapper.addProperty(“type”, object.getClass().getName()); wrapper.add(“data”, context.serialize(object)); return wrapper; } public T deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) … Read more

Best approach to parse huge (extra large) JSON file

You don’t need to switch to Jackson. Gson 2.1 introduced a new TypeAdapter interface that permits mixed tree and streaming serialization and deserialization. The API is efficient and flexible. See Gson’s Streaming doc for an example of combining tree and binding modes. This is strictly better than mixed streaming and tree modes; with binding you … Read more

GSON does not deserialize reference to outer class

As Gson documentation says: Gson can serialize static nested classes quite easily. Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization. You can address this problem … Read more

GSON is not being imported into the maven project

Try: <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> I have edited my comment and added the scope. The default scope is compile, meaning that the dependency is not present at runtime. For this, you use the provided scope. More about scopes in maven dependencies on Apache’s Introduction to Maven Dependencies. Hope this resolves your issue. P.S.: … Read more

Using Gson and Abstract Classes

I finally solved it! // GSON GsonBuilder gsonBilder = new GsonBuilder(); gsonBilder.registerTypeAdapter(Conteudo.class, new InterfaceAdapter<Conteudo>()); gsonBilder.setPrettyPrinting(); Gson gson =gsonBilder.create(); String str2send = gson.toJson(message); Mensagem msg_recv = gson.fromJson(str2send,Mensagem.class); Note that: “registerTypeAdapter(AbstractClass.class, new InterfaceAdapter());” by AbstractClass.class i mean the class that you are implementing in my case it was Conteúdo that could be ConteudoTweet or ConteudoUserSystem and so … Read more