gson

Gson parse to POJO with custom key

谁都会走 提交于 2021-02-19 05:38:06
问题 I have json data like this: meds: [ { MedsID: 8063, updated_at: "2015-11-04T06:59:55", treatment_date: "2015-11-04T00:00:00", name: "name" } ], scores: [ { ScoreID: 75820, updated_at: "2015-11-04T06:59:55" dialysis_flow: 233, } ], dias: [ { DiasID: 75820, updated_at: "2015-11-04T06:59:55" name: "K", } ] And here is my Entities: public class BaseData{ public long id; } public class Med extends BaseData{ public String name; public String updated_at; public String treatment_date; } public class

GSON - Deserialize case insensitive field name (no definite case pattern)

半世苍凉 提交于 2021-02-17 21:39:58
问题 I want to build a flexible api, I have no definite case sensitivity the user can pass, so GSON must be able to deserialize this in case sensitive. {"firstName":"Juan"} {"firstname":"Juan"} {"Firstname":"Juan"} ... How can I deserialize these field into my Foo's firstName? public class Foo { private String firstName; //..getters } I tried to use FieldNamingPolicy, but it did not work. new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .create();

Retrofit 2: No virtual method newJsonReader(Ljava/io/Reader;) NoSuchMethodException

坚强是说给别人听的谎言 提交于 2021-02-16 20:23:41
问题 I am searching for this error whole internet, but yet, only one stackoverflow entry with no answer or comment. I am trying to use Retrofit 2. It is my first time using it. Here are my dependencies: compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.google.code.gson:gson:2.6.2' I exclued any OkHttp libraries as Retrofit already uses it. This is my request interface: public interface LoginService { @POST(HTTPService.AUTHENTICATIO

Gson doesn't serialize fields defined in subclasses

◇◆丶佛笑我妖孽 提交于 2021-02-16 16:27:50
问题 For some unknown reason if I have: class A{ int stars; public int getStars(){ return stars; } public void setStarts(int stars){ this.stars = stars; } } class B extends A{ int sunshines; [getter and setter for sunshines] } class C{ List<A> classes; [get and set for classes] } if I serialize an Object of type C I have only the fields of A in the serialized objects in the field classes (while I would expect to have the fields of B if the object is a B). How to do that? 回答1: I've tried your

Gson doesn't serialize fields defined in subclasses

有些话、适合烂在心里 提交于 2021-02-16 16:26:22
问题 For some unknown reason if I have: class A{ int stars; public int getStars(){ return stars; } public void setStarts(int stars){ this.stars = stars; } } class B extends A{ int sunshines; [getter and setter for sunshines] } class C{ List<A> classes; [get and set for classes] } if I serialize an Object of type C I have only the fields of A in the serialized objects in the field classes (while I would expect to have the fields of B if the object is a B). How to do that? 回答1: I've tried your

How to read JSON in list of generic

会有一股神秘感。 提交于 2021-02-11 17:51:26
问题 I have a helper class calling a REST web service. This helper class receives the information from the web service within a class defined as a generic "R". I can easily use GSON to read my JSON in a POJO, but I can't figure out how to give it a List and read it properly. So far I have tried to extract the type of R in a variable and pass it to the List without success. I have tried using Jackson with its TypeFactory, also without success, it won't take R as its parameter. What I want to do is

How to read JSON in list of generic

北城以北 提交于 2021-02-11 17:51:07
问题 I have a helper class calling a REST web service. This helper class receives the information from the web service within a class defined as a generic "R". I can easily use GSON to read my JSON in a POJO, but I can't figure out how to give it a List and read it properly. So far I have tried to extract the type of R in a variable and pass it to the List without success. I have tried using Jackson with its TypeFactory, also without success, it won't take R as its parameter. What I want to do is

Compare two json which has same nested structure and same keys but values can be different?

两盒软妹~` 提交于 2021-02-11 15:01:29
问题 For example : Json A => { "customer": { "age": 29, "fullName": "Emily Jenkins", "year":1988, "address":{ "lineOne":"lineOne", "lineTwo":"lineTwo" } }, "handset":{ "phone":"0123456789", "colour":"black" } } Json B => { "customer": { "fullName": "Sammy J", "age": 31, "year":1985, "address":{ "lineTwo":"lineTwo", "lineOne":"lineOne" } }, "handset":{ "colour":"red", "phone":"0123456788" } } I want compare these two json and it should return true as keys are matching and both json structure is

How config gson in Spring boot?

强颜欢笑 提交于 2021-02-11 05:59:03
问题 Spring Boot 2 In application.yml http: converters: preferred-json-mapper: gson Now I write class with custom settings for Gson : public class GsonUtil { public static GsonBuilder gsonbuilder = new GsonBuilder(); public static Gson gson; public static JsonParser parser = new JsonParser(); static { // @Exclude -> to exclude specific field when serialize/deserilaize gsonbuilder.addSerializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes

Gson desearilize list and class, how does erasure work here?

大兔子大兔子 提交于 2021-02-11 00:22:14
问题 I intend to write a generic method to convert a json list into it's specific list with class. This is the generic json parser: public class JsonParserUtils { private static Gson gson = new Gson(); public static <T> String toJson(T object) { if (object == null) { return null; } return gson.toJson(object); } public static <T> T fromJson(String json, Class<T> className) { if (StringUtils.isEmpty(json)) { return null; } T object = gson.fromJson(json, className); return object; } public static <T>