0

I am trying to parse a JSON structure like below

{
   "service":{
      "id":"7ba68ee3-73fc-4d0e-9819-6f3f23b97ab3",
      "state":"new",
      "operatingState":"Unknown",
      "serviceCharacteristic":[
         {
            "name":"privateId",
            "value":[
               {
                  "number":"505013486018300"
               }
            ]
         },
         {
            "name":"publicId",
            "value":[
               {
                  "serviceNumber":"34344141",
                  "accessRestriction": "true"
               }
            ]
         },
         {
            "name":"serviceProviderId",
            "value":"0002"
         },
         {
            "name":"type_w",
            "value":[
               {
                  "number":"505013486018300"
               }
            ]
         },
         {
            "name":"type_f",
            "value":[
               {
                  "number":"2323123",
                  "enabled": "true"
               }
            ]
         }
      ],
      "supportingService":[
         {
            "id":"b28f4a28-4226-4976-9373-85c0bc13b440",
            "state":"active",
            "operatingState":"Unknown",
            "serviceDate":"2019-12-20T12:05:58.000Z",
            "serviceCharacteristic":[
               {
                  "name":"profile",
                  "value":"default"
               },
               {
                  "name":"type_s",
                  "value":[
                     "bar-a",
                     "bar-b"
                  ]
               },
               {
                  "name":"type_a",
                  "value":"false"
               },
               {
                  "name":"type_b",
                  "value":"cliBlk"
               },
               {
                  "name":"type_c",
                  "value":"true"
               },
               {
                  "name":"type_d",
                  "value":"false"
               },
               {
                  "name":"call_me",
                  "value":"true"
               },
               {
                  "name":"defaultCallBackNotification",
                  "value":"false"
               }
            ]
         }
      ]
   }
}

and after searching/reading followed the solution provided (https://stackoverflow.com/a/12459070/2082536) and wrote a custom JSON Deserializer (StdDeserializer). I can get the JSON node (value) in the Deserializer but then facing issues in mapping and returning correct Java objects.

Reason for custom Deserializer is due to the different data types associated with property (value). Client is sending three different types

  1. List of Objects
  2. List of String
  3. String

So far, I have done as below

1. Created abstract class value

public abstract class Value {
    public Value() {
    }
}

2. Created a class to hold list of objects

@JsonIgnoreProperties(ignoreUnknown = true)
public class ListOfValueObjects extends Value implements Serializable
{

    public List<ValueObject> value;

    public ListOfValueObjects() {
        super();
    }

    public static class ValueObject implements Serializable
    {
        @JsonProperty("enabled")
        public String enabled;
        @JsonProperty("accessRestriction")
        public String accessRestriction;
        @JsonProperty("serviceNumber")
        public String serviceNumber;
        @JsonProperty("number")
        public String number;

        public ValueObject() {
        }

    }

}

3. Created class for list of string

    public class ValueList extends Value implements Serializable
    {
        public List<String> value;

        public ValueList() {
            super();
        }

    }
3. Created class for string value

    public class ValueString extends Value implements Serializable
    {
        public String value;

        public ValueString() {
            super();
        }

    }

4. The top class looks like 

    public class service implements Serializable
    {
        // other properties

        public List<ServiceCharacteristic> serviceCharacteristic = null;
        @JsonProperty("supportingService")
        @Valid
        public List<SupportingService> supportingService = null;
    }

    public static class ServiceCharacteristic implements Serializable
    {

        @JsonProperty("name")
        public String name;
        @Valid
        @JsonDeserialize(using = ValueDeserializer.class)
        public Value value;
    }

5. Dserializer look like ( referncing the answer given in here -- https://stackoverflow.com/a/12459070/2082536 )


    public class ValueDeserializer extends StdDeserializer<Value> 
    {

        public ValueDeserializer() {
            super(Value.class);
        }

        @Override
        public Value deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException 
        {
            ObjectCodec codec = jsonParser.getCodec();

            // this should be the value node ???
            JsonNode node = codec.readTree(jsonParser);

            // base on node type switch to correct class 

            // and using object mapper deserialize to class

        }
    }

Jackson version: 2-10.1

rawat
  • 165
  • 1
  • 2
  • 15
  • 1
    I didn't find your question very clear. Could you be more explicit about what's yur input and expected output ? – Dici Dec 21 '19 at 09:05
  • Sorry about that, I want to know how to de-serialize the JSON data having a quirk property (in the above example its 'value'). – rawat Dec 21 '19 at 09:24

0 Answers0