Creating GSON Object

JsonObject innerObject = new JsonObject(); innerObject.addProperty(“name”, “john”); JsonObject jsonObject = new JsonObject(); jsonObject.add(“publisher”, innerObject); http://www.javadoc.io/doc/com.google.code.gson/gson Just an FYI: Gson is really made for converting Java objects to/from JSON. If this is the main way you’re using Gson, I think you’re missing the point.

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

JsonParser is deprecated

Based on the javadoc for Gson 2.8.6 No need to instantiate this class, use the static methods instead. and following are the alternatives to be used. // jsonString is of type java.lang.String JsonObject jsonObject = JsonParser.parseString​(jsonString).getAsJsonObject(); // reader is of type java.io.Reader JsonObject jsonObject = JsonParser.parseReader​(reader).getAsJsonObject(); // jsonReader is of type com.google.gson.stream.JsonReader JsonObject jsonObject = … Read more

Could not serialize object cause of HibernateProxy

You can do without manually unproxying everything by using a custom TypeAdapter. Something along these lines: /** * This TypeAdapter unproxies Hibernate proxied objects, and serializes them * through the registered (or default) TypeAdapter of the base class. */ public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> { public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { @Override … Read more

JAVA – 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

retrofit convertor factory can not access GsonConverterFactory

Try to use same version for retrofit and converter-gson – 2.0.0-beta2. You are using beta2 for retrofit and beta1 for converter. implementation ‘com.squareup.retrofit:retrofit:2.0.0-beta2’ implementation ‘com.squareup.retrofit:converter-gson:2.0.0-beta2’ Important note! Retrofit change its package name since 2.0.0-beta3 version. Now you should use com.squareup.retrofit2. Here is example: implementation ‘com.squareup.retrofit2:retrofit:2.2.0’ implementation ‘com.squareup.retrofit2:converter-gson:2.2.0’

how to parse JSON file with GSON

You have to fetch the whole data in the list and then do the iteration as it is a file and will become inefficient otherwise. private static final Type REVIEW_TYPE = new TypeToken<List<Review>>() { }.getType(); Gson gson = new Gson(); JsonReader reader = new JsonReader(new FileReader(filename)); List<Review> data = gson.fromJson(reader, REVIEW_TYPE); // contains the whole … Read more

Converting Object to JSON and JSON to Object in PHP, (library like Gson for Java)

This should do the trick! // convert object => json $json = json_encode($myObject); // convert json => object $obj = json_decode($json); Here’s an example $foo = new StdClass(); $foo->hello = “world”; $foo->bar = “baz”; $json = json_encode($foo); echo $json; //=> {“hello”:”world”,”bar”:”baz”} print_r(json_decode($json)); // stdClass Object // ( // [hello] => world // [bar] => baz … Read more

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