Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #283: Deserializing Map with enum keys results in runtime string keys #522

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -67,7 +67,7 @@ Class<?> getComponentClass() {
}

@Override
public void appendResult(Object result) {
public void appendResult(Object result, Unmarshaller context) {
appendCaptor(convertNullToOptionalEmpty(componentClass, result));
}

Expand All @@ -80,7 +80,7 @@ private <X> void appendCaptor(X value) {
protected void deserializeNext(JsonParser parser, Unmarshaller context) {
final JsonbDeserializer<?> deserializer = newUnmarshallerItemBuilder(context.getJsonbContext()).withType(componentClass)
.withCustomization(componentClassModel == null ? null : componentClassModel.getClassCustomization()).build();
appendResult(deserializer.deserialize(parser, context, componentClass));
appendResult(deserializer.deserialize(parser, context, componentClass), context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -104,7 +104,7 @@ protected void deserializeInternal(JsonbParser parser, Unmarshaller context) {
case KEY_NAME:
break;
case VALUE_NULL:
appendResult(null);
appendResult(null, context);
break;
case END_OBJECT:
case END_ARRAY:
Expand Down Expand Up @@ -190,8 +190,9 @@ protected Object convertNullToOptionalEmpty(Type propertyType, Object value) {
* or other embedded objects use methods provided.
*
* @param result An instance result of an item.
* @param context Current unmarshalling context.
*/
public abstract void appendResult(Object result);
public abstract void appendResult(Object result, Unmarshaller context);

/**
* Returns parser context.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -47,7 +47,7 @@ protected void deserializeNext(JsonParser parser, Unmarshaller context) {
}

@Override
public void appendResult(Object result) {
public void appendResult(Object result, Unmarshaller context) {
throw new UnsupportedOperationException("Inner json structures are deserialized by JsonParser.");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -102,7 +102,7 @@ public T getInstance(Unmarshaller unmarshaller) {
}

@Override
public void appendResult(Object result) {
public void appendResult(Object result, Unmarshaller context) {
appendCaptor(convertNullToOptionalEmpty(collectionValueType, result));
}

Expand All @@ -114,7 +114,7 @@ private <T> void appendCaptor(T object) {
@Override
protected void deserializeNext(JsonParser parser, Unmarshaller context) {
final JsonbDeserializer<?> deserializer = newCollectionOrMapItem(collectionValueType, context.getJsonbContext());
appendResult(deserializer.deserialize(parser, context, collectionValueType));
appendResult(deserializer.deserialize(parser, context, collectionValueType), context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -12,6 +12,7 @@

package org.eclipse.yasson.internal.serializer;

import java.io.StringReader;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
Expand All @@ -33,15 +34,22 @@

/**
* Item implementation for {@link java.util.Map} fields.
* According to JSON specification object can have only string keys, given that maps could only be parsed
* from JSON objects, implementation is bound to String type.
* According to JSON specification object can have only string keys.
* Nevertheless the implementation lets the key be a basic object that was
* serialized into a string representation. Therefore the key is also parsed to
* convert it into its parametrized type.
*
* @param <T> map type
*/
public class MapDeserializer<T extends Map<?, ?>> extends AbstractContainerDeserializer<T> implements EmbeddedItem {

/**
* Type of value in the map. (Keys must always be Strings, because of JSON spec)
* Type of the key in the map.
*/
private final Type mapKeyRuntimeType;

/**
* Type of value in the map.
*/
private final Type mapValueRuntimeType;

Expand All @@ -54,6 +62,9 @@ public class MapDeserializer<T extends Map<?, ?>> extends AbstractContainerDeser
*/
protected MapDeserializer(DeserializerBuilder builder) {
super(builder);
mapKeyRuntimeType = getRuntimeType() instanceof ParameterizedType
? ReflectionUtils.resolveType(this, ((ParameterizedType) getRuntimeType()).getActualTypeArguments()[0])
: Object.class;
mapValueRuntimeType = getRuntimeType() instanceof ParameterizedType
? ReflectionUtils.resolveType(this, ((ParameterizedType) getRuntimeType()).getActualTypeArguments()[1])
: Object.class;
Expand Down Expand Up @@ -93,19 +104,23 @@ public T getInstance(Unmarshaller unmarshaller) {
}

@Override
public void appendResult(Object result) {
appendCaptor(getParserContext().getLastKeyName(), convertNullToOptionalEmpty(mapValueRuntimeType, result));
public void appendResult(Object result, Unmarshaller context) {
// try to deserialize the string key into its type, JaxbException if not possible
final Object key = context.deserialize(mapKeyRuntimeType, new JsonbRiParser(
context.getJsonbContext().getJsonProvider().createParser(
new StringReader("\"" + getParserContext().getLastKeyName() + "\""))));
appendCaptor(key, convertNullToOptionalEmpty(mapValueRuntimeType, result));
}

@SuppressWarnings("unchecked")
private <V> void appendCaptor(String key, V value) {
((Map<String, V>) getInstance(null)).put(key, value);
private <K, V> void appendCaptor(K key, V value) {
((Map<K, V>) getInstance(null)).put(key, value);
}

@Override
protected void deserializeNext(JsonParser parser, Unmarshaller context) {
final JsonbDeserializer<?> deserializer = newCollectionOrMapItem(mapValueRuntimeType, context.getJsonbContext());
appendResult(deserializer.deserialize(parser, context, mapValueRuntimeType));
appendResult(deserializer.deserialize(parser, context, mapValueRuntimeType), context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -137,9 +137,10 @@ private T createInstance(Class<T> rawType, JsonbCreator creator) {
* Set populated instance of current object to its unfinished wrapper values map.
*
* @param result An instance result of an item.
* @param context Current unmarshalling context.
*/
@Override
public void appendResult(Object result) {
public void appendResult(Object result, Unmarshaller context) {
final PropertyModel model = getModel();
//missing property for null values
if (model == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -46,7 +46,7 @@ protected UserDeserializerDeserializer(DeserializerBuilder builder, Deserializer
}

@Override
public void appendResult(Object result) {
public void appendResult(Object result, Unmarshaller context) {
//ignore internal deserialize() call in custom deserializer
}

Expand Down
Loading