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

Fix map serialization when key is not string #852

Merged
merged 2 commits into from
May 31, 2024
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
Expand Up @@ -170,7 +170,7 @@ private void encodeMapKey(EncoderContext context,
} else if (keyNode.isNull()) {
throw new SerdeException("Null key for a Map not allowed in JSON");
} else if (keyNode.isBoolean() || keyNode.isNumber()) {
encoder.encodeString(keyNode.coerceStringValue());
encoder.encodeKey(keyNode.coerceStringValue());
} else {
convertMapKeyToStringAndEncode(context, encoder, keyNode.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.micronaut.core.type.Argument
import io.micronaut.json.JsonMapper
import io.micronaut.serde.LimitingStream
import io.micronaut.serde.SerdeRegistry
import io.micronaut.serde.annotation.Serdeable
import io.micronaut.serde.support.ObjectWithMap
import io.micronaut.serde.support.util.JsonNodeDecoder
import io.micronaut.serde.support.util.JsonNodeEncoder
Expand All @@ -24,7 +23,7 @@ class ObjectWithMapSerializerSpec extends Specification {

def 'test serialize object with map'() {
given:
def obj = new ObjectWithMap("Object1", Map.of("item1", 1L))
def obj = new ObjectWithMap("Object1", Map.of("item1", 1L), Map.of(1, 1L))
def encoderContext = serdeRegistry.newEncoderContext(null)
def decoderContext = serdeRegistry.newDecoderContext(null)
def argument = Argument.of(ObjectWithMap)
Expand All @@ -36,13 +35,14 @@ class ObjectWithMapSerializerSpec extends Specification {
def jsonNode = encoder.getCompletedValue()
def str = jsonMapper.writeValueAsString(jsonNode)
then:
str == '{"name":"Object1","mapOfLongs":{"item1":1}}'
str == '{"name":"Object1","stringLongMap":{"item1":1},"integerLongMap":{"1":1}}'
when:
def decoder = JsonNodeDecoder.create(jsonNode, LimitingStream.DEFAULT_LIMITS)
def deserResult = deserializer.deserialize(decoder, decoderContext, argument)
then:
deserResult.name == obj.name
deserResult.mapOfLongs == obj.mapOfLongs
deserResult.stringLongMap == obj.stringLongMap
deserResult.integerLongMap == obj.integerLongMap
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
@Serdeable
public class ObjectWithMap {
private final String name;
private final Map<String, Long> mapOfLongs;
private final Map<String, Long> stringLongMap;
private final Map<Integer, Long> integerLongMap;

public ObjectWithMap(String name, Map<String, Long> mapOfLongs) {
public ObjectWithMap(String name, Map<String, Long> stringLongMap, Map<Integer, Long> integerLongMap) {
this.name = name;
this.mapOfLongs = mapOfLongs;
this.stringLongMap = stringLongMap;
this.integerLongMap = integerLongMap;
}

public String getName() {
return name;
}

public Map<String, Long> getMapOfLongs() {
return mapOfLongs;
public Map<String, Long> getStringLongMap() {
return stringLongMap;
}

public Map<Integer, Long> getIntegerLongMap() {
return integerLongMap;
}
}
Loading