0

when trying to deserialize this JSON the playSequenceDateRanges List is empty

JSON:

{
    "first_name": "bob",
    "last_name": "smith",
    "play_sequence_date_ranges": [
        "1 - 07/30/2018 - AL",
        "2 - 07/30/2018 - AZ"
    ],
    "addresses": [
        {
            "line_1": "Street 1",
            "city": "City"
        }
    ],
   "id": 113
}

Java class:

public class Person implements Comparable<Person > {
    @JsonbProperty(value = "play_sequence_date_ranges")
    private List<String> playSequenceDateRanges = new ArrayList<>();

    @JsonbProperty(value = "first_name")
    private String firstName;

    @JsonbProperty(value = "last_name")
    private String lastName;

    private List<Address> addresses = new ArrayList<>();

    private Integer id;
    ...
}

I've tried to add a deserializer and adapter annotations (@JsonbTypeDeserializer and @JsonbTypeAdapter respectively) to playSequenceDateRanges to create a List of Strings from a JSONArray but no joy; the code isn't even being hit on debug.

Deserializer:

public class JsonArrayToStringListDeserializer implements JsonbDeserializer<List<String>> {
    @Override
    public List<String> deserialize(final JsonParser parser, final DeserializationContext ctx, final Type rtType) {
        List<String> stringList = new ArrayList<>();
        JsonArray jsonArray = parser.getArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            stringList.add(jsonArray.getString(i));
        }
        return stringList;
    }
}

Adapter

public class JsonArrayStringListAdapter implements JsonbAdapter<List<String>, JsonArray> {
    @Override
    public JsonArray adaptToJson(List<String> obj) throws Exception {
        return null;
    }

    @Override
    public List<String> adaptFromJson(JsonArray obj) throws Exception {
        Jsonb jsonb = JsonbBuilder.create();
        List<String> stringList = jsonb.fromJson((InputStream) obj, new ArrayList<String>(){}.getClass().getGenericSuperclass());
        return stringList;
    }
}

I'm using yasson 1.08.

user1421324
  • 139
  • 3
  • 9

1 Answers1

0

So this turned out to be some pain in the hole. I had to write an javax.json.bind.adapter.JsonbAdapter for the class, adding the annotation to the class member only did nothing.

Yasson's implementation of json-b strikes me as being buggy as I've run into issues that I've never had with Jackson.

user1421324
  • 139
  • 3
  • 9