0

I have this project I'm working on and I can't seem to get Gson to behave properly. Here are the screenshots/files I will be referring to: https://www.dropbox.com/sh/4qi2skij9knea7n/AACwkRV9x43lZeOoTZeCS3NPa?dl=0

I have my model, Trip:

public class Trip {

@Expose
@SerializedName("Id")
private String mId;

@Expose
@SerializedName("Name")
private String mName;

@Expose
@SerializedName("Date")
private String mShipmentDateString;

@Expose
@SerializedName("State")
private int mState;

@Expose
@SerializedName("Orders")
private List<Order> mOrders;


//Getters and setters
}

And that's it, nothing more. I have created a separate project to try and understand this problem better, what I do in Main Activity is:

String jsonElement = JsonHelper.getString();
final TripResponse trips = new Gson().fromJson(jsonElement, 
TripResponse.class);

TripRequest tripRequest = new TripRequest();
tripRequest.setTrips(trips.getTripList());

Gson gson = new GsonBuilder().serializeNulls().create();
String jsonWithBuilder = gson.toJson(tripRequest);


Gson gsonB2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
String object = gsonB2.toJson(tripRequest);

This is what I get, as you can see even though the dateString exists and has a value it doesn't get serialized: (look at image1 in the folder).

The funny thing is: if I swap the SerializedName between dateString and name, suddenly it serializes the date and not the name: (as you can see in image2 on dropbox).

Input and output (output starts at line 118) are in the file "INPUTANDOUTPUT" in the dropbox folder.

What am I missing?

  • Your shared images are not loading to my side – Piyush Aug 09 '17 at 10:03
  • I don't know why, they are shared and public in the dropbox folder I have linked.. – Sara Seward Aug 09 '17 at 10:42
  • Check [this](https://stackoverflow.com/questions/4802887/gson-how-to-exclude-specific-fields-from-serialization-without-annotations) @Sara Seward – Piyush Aug 09 '17 at 11:02
  • I don't have the same problem: they want to EXCLUDE fields, whereas I want to force INCLUDE them because Gson doesn't seem to "see" them. – Sara Seward Aug 09 '17 at 11:14
  • Also can you log this value `jsonWithBuilder` and `object`? Also change this from `Gson gsonB2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();` to `Gson gsonB2 = new GsonBuilder().serializeNulls().create();` – Piyush Aug 09 '17 at 11:35
  • I send them to you at piyush.gupta569@gmail.com I have "logged" them: they are in the file inputandoutput.txt : they are always equal to each other. You told me to change gsonB2 to .serializeNulls(): if you read the code, I created 3 GSON objects: one is the simplest/default one, the other one is the complex builder with .excludeFieldsWithoutExposeAnnotation().seriali‌​zeNulls() (called gsonB2), the other one is called gson and is the simple one with GsonBuilder().serializeNulls().create(); Can you help me? – Sara Seward Aug 09 '17 at 12:42
  • Yes. I have doubt that if may be your Name key contains null value or empty and also you want to include that value (serialize) which also null or empty. So over come with it i have found this [article](https://futurestud.io/tutorials/gson-builder-force-serialization-of-null-values).But I am little confuse with your code too. I have used Gson many times but i have only used with simple one. @Sara Seward. You got it my point ?? – Piyush Aug 09 '17 at 12:45
  • What is `TripRequest` stands for ? If your `trips` objects contains response data with Id , Name and date then you should serialize `trips` object. – Piyush Aug 09 '17 at 12:57
  • The person writing the APIs wants an object called "trips" containing the list of the trips, so I made TripRequest. I am serializing TripRequest and so serializing all the trips inside it.. I have already tried serializeNulls...it doesn't work. I'm as confused as you unfortunately because it seems (to me) that everything is ok... – Sara Seward Aug 09 '17 at 13:07
  • Have you tried with `Gson gson = new GsonBuilder().create();` only ? – Piyush Aug 09 '17 at 13:10
  • I tried now, doesn't change anything :/ – Sara Seward Aug 09 '17 at 13:28

1 Answers1

0

I think it's because your are using two differentes @annotation for the same purpose :

@Expose // serialize the variable as mId
private String mId 

@SerializedName("Id") // serialize the variable as Id
private String mId;

You can try to use just @SerializedName

Hope this helps

Sorry for my english.

Cochi
  • 2,199
  • 2
  • 12
  • 15
  • Can you share the excepted json format of your Trip object please? – Cochi Aug 09 '17 at 11:30
  • I already have, it's in the file inputoutput.txt in the shared folder. – Sara Seward Aug 09 '17 at 12:38
  • Just to be sure, the date value doesn't appear after the orders list in your result object ? ^^ The Json doesn't care about the order defined in your code, in your json file the Date value is after the Order list, that's why i ask. – Cochi Aug 10 '17 at 10:20