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?