gson

Customizing JSON serialization in Play

☆樱花仙子☆ 提交于 2019-12-18 05:23:50
问题 I'm using renderJSON(Object) to return some objects as JSON values, and it's working fine except for one field. Is there an easy way to add in that one field without having to manually create the whole json template? 回答1: Play uses GSON to build the JSON string. If your one field is a specific object type, then you can easily do this by providing a customised serialisation for that type. See the documentation here http://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and

Gson deserializing nested objects with InstanceCreator

流过昼夜 提交于 2019-12-18 04:37:12
问题 I have a class named PageItem , which has a constructor that takes a Context as parameter: PageItem(Context context) { super(context); this.context = context; } PageItem has these properties: private int id; private String Title; private String Description; public Newsprovider newsprovider; public Topic topic; Newsprovider and Topic are other classes of my application and have the following constructors: Newsprovider (Context context) { super(context); this.context = context; } Topic (Context

How to pass gson serialised object to Intent in android?

戏子无情 提交于 2019-12-18 04:03:29
问题 i am trying to pass the gson serialised object to intent by using the below code intent.putExtra("com.example", vo); // vo is the gson serialised object. but it is throwing the run time exception, Please help. java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.d.a) at android.os.Parcel.writeSerializable(Parcel.java:1285) at android.os.Parcel.writeValue(Parcel.java:1233) at android.os.Parcel.writeMapInternal(Parcel.java:591) at

How to serialize and deserialize Java 8's java.time types with Gson? [closed]

∥☆過路亽.° 提交于 2019-12-18 03:04:33
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I'm using GSON to serialise some object graphs to JSON. These objects graphs use the new Java 8 java.time entities ( ZonedDateTime , OffsetDateTime , LocalTime etc). I've found a library for Joda Time serialisers here - is there an equivalent library for the JDK java.time classes? (This person had no luck with

springfox(swagger2) does not work with GsonHttpMessageConverterConfig

眉间皱痕 提交于 2019-12-18 01:21:23
问题 What I am trying to build is a spring-boot (v1.2.3) application and expose my Rest API with SpringFox(swagger2) v2.0.0 my Swagger Spring config @EnableSwagger2 @Configuration public class SwaggerConfig { @Bean public Docket myApi() { return new Docket(DocumentationType.SWAGGER_2) .genericModelSubstitutes(DeferredResult.class) .useDefaultResponseMessages(false) .forCodeGeneration(false) .pathMapping("/my-prj"); } } I need to use gson to convert my pojo's to json, and I do it this way:

Mapping Json Array to Java Models

不羁岁月 提交于 2019-12-17 22:59:19
问题 I have a complex json as in here I am trying to map this in my model class 'ChromeJsonModel' like : Type collectionType = new TypeToken<List<ChromeJsonModel>>(){}.getType(); List<ChromeJsonModel> jsonModelList = (List<ChromeJsonModel>) new Gson().fromJson( jsonPrettyPrintString , collectionType); Bu t I am receiving following error. Expected BEGIN_ARRAY but was BEGIN_OBJECT Any reason why and where am I going wrong? 回答1: You have very complex JSON payload where the same property could have

Multiple GSON @SerializedName per field?

旧城冷巷雨未停 提交于 2019-12-17 21:54:56
问题 Is there any way in Gson to map multiple JSON fields to a single Java object member variable? Let's say I have a Java class... public class MyClass { String id; String name; } I want to use this single class with two different services. However, these two services differ in how they return their data... { "id": 2341, "person": "Bob" } ... and ... { "id": 5382, "user": "Mary" } ... respectively. Is there any way to map both the "person" and "user" fields in the JSON string to the name field in

How to use Gson to serialize objects in android?

孤街浪徒 提交于 2019-12-17 20:36:57
问题 I want to send 2 objects to Java server from Android client using socket (as I am developing a Remote PC). AndroidClient.java public class MainActivity extends Activity{ Socket client; ObjectOutputStream oos; OutputStream os; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SendObj so=new SendObj(); so.execute(); } class SendObj extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground

How parse json array with multiple objects by gson?

倖福魔咒の 提交于 2019-12-17 19:43:47
问题 How can I parse json using gson? I have a json array with multiple object types, and I don't know, what kind of object I need to create to save this structure. I cannot change the json data format (I don't control the server). Can I use gson or other library parse this json array, how should I do? This is the json code block: [ { "type": 1, "object": { "title1": "title1", "title2": "title2" } }, { "type": 2, "object": [ "string", "string", "string" ] }, { "type": 3, "object": [ { "url": "url"

How do you get GSON to omit null or empty objects and empty arrays and lists?

假如想象 提交于 2019-12-17 19:33:29
问题 I am using Gson and I am in a situation in which I have to shrink the size of certain Json strings. I would like to do so by getting it to not put null objects, only empty values, and empty lists and arrays into the Json string. Is there a straightforward way to do that? Let me clarify a bit: I want everything that says: emptyProp:{} or emptyArray:[] to be skipped. I want any object that only contains properties that are empty to be skipped. 回答1: Null values are excluded by default as long as