diff --git a/serde-support/src/main/java/io/micronaut/serde/support/deserializers/collect/EnumMapDeserializer.java b/serde-support/src/main/java/io/micronaut/serde/support/deserializers/collect/EnumMapDeserializer.java index f388d70c4..0413f532f 100644 --- a/serde-support/src/main/java/io/micronaut/serde/support/deserializers/collect/EnumMapDeserializer.java +++ b/serde-support/src/main/java/io/micronaut/serde/support/deserializers/collect/EnumMapDeserializer.java @@ -52,19 +52,19 @@ public Deserializer> createSpecific(DecoderContext context, Argume return (decoder, decoderContext, mapType) -> { final EnumMap map = new EnumMap<>(enumType.getType()); final RemainingLimits remainingLimits = decoderContext.getSerdeConfiguration().map(LimitingStream::limitsFromConfiguration).orElse(LimitingStream.DEFAULT_LIMITS); - final Decoder objectDecoder = decoder.decodeObject(mapType); - String key = objectDecoder.decodeKey(); - while (key != null) { - JsonNodeDecoder keyDecoder = JsonNodeDecoder.create(JsonNode.createStringNode(key), remainingLimits); - E k = enumDeser.deserialize(keyDecoder, decoderContext, enumType); - if (valueDeser == null) { - map.put(k, (V) objectDecoder.decodeArbitrary()); - } else { - map.put(k, valueDeser.deserializeNullable(objectDecoder, decoderContext, valueType)); + try (final Decoder objectDecoder = decoder.decodeObject(mapType)) { + String key = objectDecoder.decodeKey(); + while (key != null) { + JsonNodeDecoder keyDecoder = JsonNodeDecoder.create(JsonNode.createStringNode(key), remainingLimits); + E k = enumDeser.deserialize(keyDecoder, decoderContext, enumType); + if (valueDeser == null) { + map.put(k, (V) objectDecoder.decodeArbitrary()); + } else { + map.put(k, valueDeser.deserializeNullable(objectDecoder, decoderContext, valueType)); + } + key = objectDecoder.decodeKey(); } - key = objectDecoder.decodeKey(); } - objectDecoder.finishStructure(); return map; }; }