How do I implement TypeAdapterFactory in Gson?

When you register a regular type adapter (GsonBuilder.registerTypeAdapter), it only generates a type adapter for THAT specific class. For example: public abstract class Animal { abstract void speak(); } public class Dog extends Animal { private final String speech = “woof”; public void speak() { System.out.println(speech); } } // in some gson related method gsonBuilder.registerTypeAdapter(Animal.class, … 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

Jsonpath with Jackson or Gson

The Jayway JsonPath library has support for reading values using a JSON path. For example: String json = “…”; Map<String, Object> book = JsonPath.read(json, “$.store.book[0]”); System.out.println(book); // prints {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} Double price = JsonPath.read(json, “$.store.bicycle.price”); System.out.println(price); // prints 19.95 You can also map JSON objects directly to classes, like … Read more

How to convert a String to JsonObject using gson library

You can convert it to a JavaBean if you want using: Gson gson = new GsonBuilder().setPrettyPrinting().create(); gson.fromJson(jsonString, JavaBean.class) To use JsonObject, which is more flexible, use the following: String json = “{\”Success\”:true,\”Message\”:\”Invalid access token.\”}”; JsonParser jsonParser = new JsonParser(); JsonObject jo = (JsonObject)jsonParser.parse(json); Assert.assertNotNull(jo); Assert.assertTrue(jo.get(“Success”).getAsString()); Which is equivalent to the following: JsonElement jelem = gson.fromJson(json, … Read more

Using GSON with proguard enabled

Variable names will be obfuscated with proguard, leaving you with something like private String a; Instead of private String descripcionCategoria; You can add proguard rules so some classes don’t get obfuscated. I got away with it using these: -keepattributes Signature # POJOs used with GSON # The variable names are JSON key values and should … 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

google gson LinkedTreeMap class cast to myclass

Serializing and Deserializing Generic Types When you call toJson(obj), Gson calls obj.getClass() to get information on the fields to serialize. Similarly, you can typically pass MyClass.class object in the fromJson(json, MyClass.class) method. This works fine if the object is a non-generic type. However, if the object is of a generic type, then the Generic type … Read more

Unable to create converter for my class in Android Retrofit library

If anyone ever comes across this in the future because you are trying to define your own custom converter factory and are getting this error, it can also be caused by having multiple variables in a class with a misspelled or the same serialized name. IE: public class foo { @SerializedName(“name”) String firstName; @SerializedName(“name”) String … Read more

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