gson

Excluding certain fields from Serialization based on value in GSON

ⅰ亾dé卋堺 提交于 2019-12-18 11:35:10
问题 I am using GSON for my serialization purposes, I am not finding a way of excluding certain fields from serialization based on ExclusionStrategy class provided by Gson based on the value of field, as it only supports top level class or field attributes based exclusions. The field attributes do not include the value of that field. So what should I do? 回答1: The way to achieve this is by creating custom serializer for the class in question. After allowing Gson to create a JSON object in default

serialize and deserialize enum with Gson [duplicate]

寵の児 提交于 2019-12-18 10:15:12
问题 This question already has answers here : Using Enums while parsing JSON with GSON (7 answers) Closed 4 years ago . How can i serialize and deserialize a simple enum like this with gson 2.2.4 ? public enum Color { RED, BLUE, YELLOW; } 回答1: According to Gson API documentation, Gson provides default serialization/deserialization of Enum , so basically it should be serialized and deserialized using the standard toJson and fromJson methods, as with any other type. 回答2: You can try this. import com

How to parse dynamic json in android with retrofit 2 using annotations

為{幸葍}努か 提交于 2019-12-18 09:34:13
问题 I have a JSON structure which I want to parse using retrofit 2 (@Expose). Below I have mentioned the JSON. Need help to parse it using dynamic annotations. { "status": 1, "message": "success", "data" : [ { "type": 1, "heading": "", "description": "", "created_on": 141123213, "author_id": 123, "author_name": "some name", "author_pic": "some_pic", "read_time": "3.1 min", "post_pic_url": "", "post_web_url": "", "isLiked": false, "isSaved": false, "totalLikes": 12 }, { "type": 2, "author_id": 123

Json response parser for Array or Object

六月ゝ 毕业季﹏ 提交于 2019-12-18 09:09:29
问题 I am writing a library to consume a Json API and I am facing a design problem when using Gson as the parsing library. One of the endpoints returns an array of objects if everything goes well like so: [ { "name": "John", "age" : 21 }, { "name": "Sarah", "age" : 32 }, ] However, the error schema for all the endpoints in the API is an json object instead of an array. { "errors": [ { "code": 1001, "message": "Something blew up" } ] } The problem arises when modeling this in POJOs. Because the

Gson deserialization - Trying to parse a JSON to an Object

独自空忆成欢 提交于 2019-12-18 08:57:09
问题 I am trying to parse a JSON into an Object. There are two classes: User and Profile. User got an instance of Profile. So now there is one JSON to build an User Object. Inside this JSON are the attributes for the User and Profile listed and as you can see, Profile and User got both a HashMap called List. However i'd like to create the User and the Profile out of this Json, but i got this Exception: //EDIT: I removed the Map<String, String> links from Profile and User. So now I do not get any

GSON: How to move fields to parent object

你离开我真会死。 提交于 2019-12-18 08:54:07
问题 I'm using Google GSON to transform my Java object to JSON. Currently I'm having the following structure: "Step": { "start_name": "Start", "end_name": "End", "data": { "duration": { "value": 292, "text": "4 min." }, "distance": { "value": 1009.0, "text": "1 km" }, "location": { "lat": 59.0000, "lng": 9.0000, "alt": 0.0 } } } Currently a Duration object is inside a Data object. I would like to skip the Data object and move the Duration object to the Step object, like this: "Step": { "start_name

GSON: JSON deserialization to variable type (List/String)

爱⌒轻易说出口 提交于 2019-12-18 07:15:11
问题 At this point it's already an old question and I've probably read every related topic on SO. But to the point. I need some advice or correction maybe? For some reason we have generatable Jsons of 2 types: {"data": {"id": "value"}} and {"data":[{"id": "value"}]} Object and Array. There are also other params but they doesn't matter here. "id" is differ for every request. Sometimes it's userId, portfolioId etc. So I get "id" and pass it to related var. For a long time I was working with the

GSON: JSON deserialization to variable type (List/String)

空扰寡人 提交于 2019-12-18 07:15:10
问题 At this point it's already an old question and I've probably read every related topic on SO. But to the point. I need some advice or correction maybe? For some reason we have generatable Jsons of 2 types: {"data": {"id": "value"}} and {"data":[{"id": "value"}]} Object and Array. There are also other params but they doesn't matter here. "id" is differ for every request. Sometimes it's userId, portfolioId etc. So I get "id" and pass it to related var. For a long time I was working with the

Retrofit encoding special characters

試著忘記壹切 提交于 2019-12-18 05:58:32
问题 I am using retrofit with gson instead of android since its faster and more secure. The problem is that retrofit is encoding special characters like = and ? , and the url I'm using cannot decode these characters. This is my code: api class: public interface placeApi { @GET("/{id}") public void getFeed(@Path("id") TypedString id, Callback<PlaceModel> response); } Main class: String url = "http://api.beirut.com/BeirutProfile.php?"; String next = "profileid=111"; //Creating adapter for retrofit

Retrofit encoding special characters

為{幸葍}努か 提交于 2019-12-18 05:58:12
问题 I am using retrofit with gson instead of android since its faster and more secure. The problem is that retrofit is encoding special characters like = and ? , and the url I'm using cannot decode these characters. This is my code: api class: public interface placeApi { @GET("/{id}") public void getFeed(@Path("id") TypedString id, Callback<PlaceModel> response); } Main class: String url = "http://api.beirut.com/BeirutProfile.php?"; String next = "profileid=111"; //Creating adapter for retrofit