Killesk
Killesk

Reputation: 3204

GSON.fromJson() issue with hashmap

I'm using GSON 2.5.

I have 2 Strings:

String reservationRoomNightGood = "[{\"reservationRoomNightParams\":{\"date\":\"2016-06-05\",\"totalPrice\":600,\"currencyCode\":\"EUR\",\"crsRatePlanId\":0,\"rateplanId\":0},\"roomNightExtra\":[]},{\"reservationRoomNightParams\":{\"date\":\"2016-06-06\",\"totalPrice\":400,\"currencyCode\":\"EUR\",\"crsRatePlanId\":0,\"rateplanId\":0},\"roomNightExtra\":[]}]";

String reservationRoomNightBad = "[{\"reservationRoomNightParams\":{\"date\":\"2016-06-05\",\"totalPrice\":700,\"currencyCode\":\"EUR\",\"chRatePlanCodes\":{\"date\":\"2016-06-05\",\"totalPrice\":600,\"currencyCode\":\"EUR\",\"crsRatePlanId\":0,\"rateplanId\":0},\"crsRatePlanId\":12969,\"configuredCommission\":0,\"configuredCommissionType\":\"P\",\"rateplanId\":0},\"roomNightExtra\":[]}]";

and I have this class to populate the strings into:

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;


public class ReservationRoomNight implements Serializable{

    private static final long serialVersionUID = -3697795946626268528L; 

    transient private static Logger logger = Logger.getLogger(Reservation.class);


    private List<ReservationExtraCost> roomNightExtra;

    private Map<String, String> reservationRoomNightParams;


    private Map<String, String> chRatePlanCodes = new HashMap<String, String>();


    private long crsRatePlanId;

    private Float configuredCommission;

    private String configuredCommissionType;

    public ReservationRoomNight(String nightDate, Float totalPrice, String currencyCode) {
        reservationRoomNightParams = new HashMap<String, String>();
        chRatePlanCodes = new HashMap<String, String>();
    }   
    public List<ReservationExtraCost> getRoomNightExtra() {
        return roomNightExtra;
    }
    public void setRoomNightExtra(List<ReservationExtraCost> roomNightExtra) {
        this.roomNightExtra = roomNightExtra;
    }

    public Map<String, String> getChRatePlanCodes() {
        return chRatePlanCodes;
    }

    public void setChRatePlanCodes(Map<String, String> chRatePlanCodes) {
        this.chRatePlanCodes = chRatePlanCodes;
    }

    public String getChannelCodes(String key) {
        return chRatePlanCodes.get(key);
    }

    public long getCRSRatePlanId() {
        return crsRatePlanId;
    }




}

The reservationRoomNightGood String is extracted no problem, but I can't figure out why reservationRoomNightBad is failing.

Type listType = new TypeToken<ArrayList<ReservationRoomNight>>() {}.getType();
        List<ReservationRoomNight> goodReservationRoomNight = new Gson().fromJson(reservationRoomNightGood, listType);
        List<ReservationRoomNight> boodReservationRoomNight = new Gson().fromJson(reservationRoomNightBad, listType);

If I take out the chRatePlanCodes hashmap from the reservationRoomNightBad string, I have no problem. But, there is a problem when i leave that part of the string in. For example, try to run it with this string (reservationRoomNightBad - hashmap)

String reservationRoomNightBad = "[{\"reservationRoomNightParams\":{\"date\":\"2016-06-05\",\"totalPrice\":700,\"currencyCode\":\"EUR\",\"crsRatePlanId\":12969,\"configuredCommission\":0,\"configuredCommissionType\":\"P\",\"rateplanId\":0},\"roomNightExtra\":[]}]";

Any ideas?

Upvotes: 1

Views: 113

Answers (1)

thinkling
thinkling

Reputation: 59

your code :

private Map<String, String> reservationRoomNightParams;

only accept key String && value String, but you gave an object to this property:

"chRatePlanCodes":{
                "date":"2016-06-05",
                "totalPrice":600,
                "currencyCode":"EUR",
                "crsRatePlanId":0,
                "rateplanId":0
            }

fix this using:

private Map<String, Object> reservationRoomNightParams;

Upvotes: 1

Related Questions