Parsing JSON array into java.util.List with Gson

Definitely the easiest way to do that is using Gson’s default parsing function fromJson(). There is an implementation of this function suitable for when you need to deserialize into any ParameterizedType (e.g., any List), which is fromJson(JsonElement json, Type typeOfT). In your case, you just need to get the Type of a List<String> and then … Read more

What is the basic purpose of @SerializedName annotation in Android using Gson

Java class example, public class Person { @SerializedName(“name”) private String personName; @SerializedName(“bd”) private String birthDate; } This class has two fields that represent the person name and birth date of a person. These fields are annotated with the @SerializedName annotation. The parameter (value) of this annotation is the name to be used when serialising and … Read more

Multiple GSON @SerializedName per field?

In October 2015, Gson version 2.4 (changelog) added the ability to use alternate/multiple names for @SerializedName when deserializing. No more custom TypeAdapter needed! Usage: java @SerializedName(value=”name”, alternate={“person”, “user”}) kotlin @SerializedName(value=”name”, alternate= [“person”, “user”]) https://www.javadoc.io/doc/com.google.code.gson/gson/2.6.2/com/google/gson/annotations/SerializedName.html

Kotlin Data Class from Json using GSON

Data class: data class Topic( @SerializedName(“id”) val id: Long, @SerializedName(“name”) val name: String, @SerializedName(“image”) val image: String, @SerializedName(“description”) val description: String ) to JSON: val gson = Gson() val json = gson.toJson(topic) from JSON: val json = getJson() val topic = gson.fromJson(json, Topic::class.java)

How to use TypeToken + generics with Gson in Kotlin

Create this inline fun: inline fun <reified T> Gson.fromJson(json: String) = fromJson<T>(json, object: TypeToken<T>() {}.type) and then you can call it in this way: val turns = Gson().fromJson<Turns>(pref.turns) // or val turns: Turns = Gson().fromJson(pref.turns) Previous Alternatives: ALTERNATIVE 1: val turnsType = object : TypeToken<List<Turns>>() {}.type val turns = Gson().fromJson<List<Turns>>(pref.turns, turnsType) You have to put … Read more

Gson ignoring map entries with value=null

See Gson User Guide – Null Object Support: The default behaviour that is implemented in Gson is that null object fields are ignored. This allows for a more compact output format; however, the client must define a default value for these fields as the JSON format is converted back into its Java form. Here’s how … Read more

Using Enums while parsing JSON with GSON

I want to expand a bit NAZIK/user2724653 answer (for my case). Here is a Java code: public class Item { @SerializedName(“status”) private Status currentState = null; // other fields, getters, setters, constructor and other code… public enum Status { @SerializedName(“0”) BUY, @SerializedName(“1”) DOWNLOAD, @SerializedName(“2”) DOWNLOADING, @SerializedName(“3”) OPEN } } in the json file you have … Read more

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

This is a well-known issue and based on this answer you could add setLenient: Gson gson = new GsonBuilder() .setLenient() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); Now, if you add this to your retrofit, it gives you another error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 … Read more

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