gson

Force GSON to use specific constructor

眉间皱痕 提交于 2019-12-17 16:33:44
问题 public class UserAction { private final UUID uuid; private String userId; /* more fields, setters and getters here */ public UserAction(){ this.uuid = UUID.fromString(new com.eaio.uuid.UUID().toString()); } public UserAction(UUID uuid){ this.uuid = uuid; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final UserAction other = (UserAction) obj; if (this.uuid != other.uuid && (this.uuid == null || !this.uuid

Gson, JSON and the subtleties of LinkedTreeMap

一世执手 提交于 2019-12-17 16:07:46
问题 I've recently started playing around with JSON strings, and was told that Google's own library, Gson , is the new and hip way of dealing with these. The way I've understood it, is that a JSON string is essentially a map. Where each variable points to a value in the string. For example: String jsonInput2 = "{\"created_at\":\"Sat Feb 08 15:37:37 +0000 2014\",\"id\":432176397474623489\"} Thus far, all is well. Information such as when this JSON string was created, can be assigned to a variable

Java GSON: Getting the list of all keys under a JSONObject

旧巷老猫 提交于 2019-12-17 15:44:49
问题 I have got GSON as a JSON parser in Java, but the keys aren't always the same. For example. I have the following JSON: { "The Object I already know": { "key1":"value1", "key2":"value2", "AnotherObject": { "anotherKey1":"anotherValue1", "anotherKey2":"anotherValue2" } } I have already got the JSONObject "The Object I already know". Now I need to get all of the JSONElements for this Object, this would be "Key1", "Key2" and "AnotherObject". Thanks in advance. EDIT: The Output should be a String

class A declares multiple JSON fields

心已入冬 提交于 2019-12-17 15:32:57
问题 i have a class A which has some private fields and the same class extends another class B which also has some private fields which are in class A. public class A extends B { private BigDecimal netAmountTcy; private BigDecimal netAmountPcy; private BigDecimal priceTo; private String segment; private BigDecimal taxAmountTcy; private BigDecimal taxAmountPcy; private BigDecimal tradeFeesTcy; private BigDecimal tradeFeesPcy; // getter and setter for the above fields } and class B has got some

Why Kotlin data classes can have nulls in non-nullable fields with Gson?

两盒软妹~` 提交于 2019-12-17 12:20:28
问题 In Kotlin you can create a data class : data class CountriesResponse( val count: Int, val countries: List<Country>, val error: String) Then you can use it to parse a JSON, for instance, "{n: 10}". In this case you will have an object val countries: CountriesResponse , received from Retrofit , Fuel or Gson , that contains these values: count = 0, countries = null, error = null . In Kotlin + Gson - How to get an emptyList when null for data class you can see another example. When you later try

Gson - deserialization to specific object type based on field value

萝らか妹 提交于 2019-12-17 10:46:19
问题 I want to deserialize json objects to specific types of objects (using Gson library) based on type field value, eg.: [ { "type": "type1", "id": "131481204101", "url": "http://something.com", "name": "BLAH BLAH", "icon": "SOME_STRING", "price": "FREE", "backgroundUrl": "SOME_STRING" }, { .... } ] So type field will have different (but known) values. Based on that value I need to deserialize that json object to appropriate model object, eg.: Type1Model, Type2Model etc. I know I can easily do

How does Gson TypeToken work?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 10:37:39
问题 I understand that in Java contrary to, for example, C# generics are compile-time feature and is removed via type erasure. So, how does Gson's TypeToken really work? How does it get the generic type of an object? 回答1: From §4.6 of the JLS (emphasis mine): Type erasure is a mapping from types (possibly including parameterized types and type variables) to types (that are never parameterized types or type variables). We write |T| for the erasure of type T. The erasure mapping is defined as

RestTemplate入门

醉酒当歌 提交于 2019-12-17 10:22:59
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> RestTemplate入门  本篇主要讲解RestTemplate的基本使用,它是Spring提供的用来访问Rest服务的客户端,RestTmplate提供了很多便捷的方法,可以大大提供开发效率,本篇只涉及基本使用,内部原理后续再展开  1.RestTemplate简述  RestTemplate是Spring提供的用于发送HTTP请求的客户端工具,它遵循Restful原则,RestTemplate默认依赖JDK的Http连接工具HttpUrlConnection,你也可以替换不同的源,比如OkHttp、Apache HttpComponents 等等。。  2.HttpMessageConverter  在说RestTemplate之前,先介绍HttpMessageConverter,RestTemplate默认使用的转化HttpMessageConverter去将Http消息转换成POJO 或者 POJO转化成Http消息。在创建RestTemplate的时候会默认添加一组HttpMessageConveter的实现。  HttpMessageConverter源码: public interface HttpMessageConverter<T> { //指示此转换器是否可以读取给定的类。

How to parse JSON array without any object in Retrofit?

我只是一个虾纸丫 提交于 2019-12-17 09:56:25
问题 I am working with Retrofit and GSON. I have a JSON response as a JSON array but I don't know how to parse it by using a model class. My response is as follows: [ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight" ] 回答1: Just call in the callback a list of Strings and it should do the job... new Callback<List<String>>(). 来源: https://stackoverflow.com/questions/28628513/how-to-parse-json-array-without-any-object-in-retrofit

GSON: how to prevent StackOverflowError while keeping circular references?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 09:46:01
问题 I have a structure with circular references. And for debug purposes, I want to dump it. Basically as any format, but I chose JSON. Since it can be any class, I chose GSON which doesn't needs JAXB annotations. But GSON hits the circular references and recurses until StackOverflowError . How can I limit GSON to ignore certain class members? Both @XmlTransient and @JsonIgnore are not obeyed. ignore certain object graph paths? E.g. I could instruct GSON not to serialize release.customFields