Jackson Custom Deserializer

时间:2013-12-19 10:52:06

标签: android json serialization jackson

我正在为对象中的一个属性使用自定义反序列化器。

public class Leg {

    @JsonProperty("carrier_code")
    private String carrierCode;
    @JsonProperty("carrier_name")
    private String carrierName;
    @JsonProperty("mode_of_transport")
    private String modeOfTransport;
    private String provider;
    @JsonProperty("leg_id")
    private int legId;
    @JsonDeserialize(using = PriceContainerDeserializer.class)
    @JsonProperty("price_container")
    private PriceContainer priceContainer;
    private Schedule schedule;

}

我遇到的问题是,除了时间表之外,一切都被解析了。如果我删除了解析调度的自定义反序列化器。我该如何解决这个问题?

这是自定义反序列化器

public class PriceContainerDeserializer extends JsonDeserializer<PriceContainer> {

    @Override
    public PriceContainer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        PriceContainer priceContainer = new PriceContainer();
        jsonParser.nextValue();
        priceContainer.setCurrency(jsonParser.getText());
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        priceContainer.setAmount(jsonParser.getDoubleValue());
        return priceContainer;
    }
}

JSON示例 -

{
                        "carrier_code": "car",
                        "carrier_name": "Car2go",
                        "leg_id": "1",
                        "mode_of_transport": "car_sharing",
                        "pre_legs": [
                            {
                                "mode_of_transport": "walk",
                                "schedule": {
                                    "a": {
                                        "date": 1387152000,
                                        "lat": 52.529985,
                                        "lng": 13.403,
                                        "name": "Torstra\u00dfe 109",
                                        "time": 1387192515,
                                        "time_delta": 0
                                    },
                                    "b": {
                                        "date": 1387152000,
                                        "lat": 52.52761,
                                        "lng": 13.3997,
                                        "name": "Auguststra\u00dfe 52, 10119 Berlin",
                                        "time": 1387192774,
                                        "time_delta": 0
                                    },
                                    "distance": 346,
                                    "duration": 259
                                }
                            }
                        ],
                        "price_container": {
                            "currency": "EUR",
                            "is_bookable": false,
                            "prices": [
                                {
                                    "amount": 377,
                                    "currency": "EUR",
                                    "price_name": "Car2go",
                                    "price_type": 0,
                                    "tarif_id": ""
                                }
                            ],
                            "reservation_price": 0,
                            "selected_price_index": 0
                        },
                        "provider": "car2go",
                        "schedule": {
                            "a": {
                                "date": 1387152000,
                                "lat": 52.52761,
                                "lng": 13.3997,
                                "name": "Auguststra\u00dfe 52, 10119 Berlin",
                                "time": 1387192774,
                                "time_delta": 0
                            },
                            "b": {
                                "date": 1387152000,
                                "lat": 52.514658,
                                "lng": 13.465346,
                                "name": "Samariterstr.",
                                "time": 1387193314,
                                "time_delta": 0
                            },
                            "distance": 4828.0199999999995,
                            "duration": 540
                        },
                        "sub_legs": [
                            {
                                "carrier_code": "car",
                                "carrier_name": "Car2go",
                                "properties": {
                                    "engine_type": "CE",
                                    "exterior_state": "UNACCEPTABLE",
                                    "fuel_level": 48,
                                    "interior_state": "UNACCEPTABLE",
                                    "license_plate_number": "B-GO2798"
                                },
                                "schedule": {
                                    "a": {
                                        "date": 1387152000,
                                        "lat": 52.52761,
                                        "lng": 13.3997,
                                        "name": "Auguststra\u00dfe 52, 10119 Berlin",
                                        "time": 1387192774,
                                        "time_delta": 0
                                    },
                                    "b": {
                                        "date": 1387152000,
                                        "lat": 52.514658,
                                        "lng": 13.465346,
                                        "name": "Samariterstr.",
                                        "time": 1387193314,
                                        "time_delta": 0
                                    },
                                    "distance": 4828.0199999999995,
                                    "duration": 540
                                },
                                "sub_mode_of_transport": {
                                    "id": [
                                        10,
                                        "car2go"
                                    ],
                                    "mode_of_transport": "car_sharing",
                                    "name": "car2go"
                                },
                                "vehicle_name": "B-GO2798"
                            }
                        ]
                    }

修改

所以我的解串器现在看起来像这样

public class PriceContainerDeserializer extends JsonDeserializer<PriceContainer> {

    @Override
    public PriceContainer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        PriceContainer priceContainer = new PriceContainer();
        jsonParser.nextValue();
        priceContainer.setCurrency(jsonParser.getText());
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        priceContainer.setAmount(jsonParser.getDoubleValue());
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        jsonParser.nextValue();
        return priceContainer;
    }
}

这是最好的方法吗?

0 个答案:

没有答案