eclipse-ee4j / parsson

Parsson Project
Other
10 stars 21 forks source link

MapUtil.handle does not support Array objects #90

Closed haducloc closed 1 year ago

haducloc commented 1 year ago

I am using Json-B that implemented by Eclipse Yasson. I want to serialize my custom Map that there is a value is an instance of array of String. I got this error:

Caused by: java.lang.IllegalArgumentException: Type class [Ljava.lang.String; is not supported. at org.eclipse.parsson.MapUtil.handle(MapUtil.java:75) at org.eclipse.parsson.JsonObjectBuilderImpl.populate(JsonObjectBuilderImpl.java:200) at org.eclipse.parsson.JsonObjectBuilderImpl.(JsonObjectBuilderImpl.java:66) at org.eclipse.parsson.JsonProviderImpl.createObjectBuilder(JsonProviderImpl.java:187) at jakarta.json.Json.createObjectBuilder(Json.java:303) at com.appslandia.common.jose.JsonbMapAdapter.adaptToJson(JsonbMapAdapter.java:56) at com.appslandia.common.jose.JsonbMapAdapter.adaptToJson(JsonbMapAdapter.java:39) at org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer.serialize(AdaptedObjectSerializer.java:62)

I check on parsson.MapUtil.handle and I found out this method does NOT support array objects?

My suggession fix:

static JsonValue handle(Object value, JsonContext jsonContext) {
    if (value == null) {
        return JsonValue.NULL;

    // ... 
    } else if (value instanceof Array) {
        Collection<?> collection = toCollection(value);
        JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(collection, jsonContext);
        return jsonArrayBuilder.build();
    }

    throw new IllegalArgumentException(String.format("Type %s is not supported.", value.getClass()));
}

    static Collection<?> toCollection(Object arr) {
        int len = Array.getLength(arr);
        List<Object> list = new ArrayList<>(len);

        for (int i = 0; i < len; i++) {
            list.add(Array.get(arr, i));
        }
        return list;
    }

If I use list of String instead array of String, my custom map was serialized fine.

Please notes that, If I use Gson or Jackson, my custom map was serialized fine.