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

Create specific serializer for map values in per-entry serializer lookup #140

Merged
merged 1 commit into from
Feb 24, 2022
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 @@ -81,7 +81,8 @@ public void serialize(Encoder encoder, EncoderContext context, Argument<? extend
childEncoder.encodeNull();
} else {
@SuppressWarnings("unchecked") final Argument<V> valueGeneric = (Argument<V>) Argument.of(v.getClass());
final Serializer<? super V> valSerializer = context.findSerializer(valueGeneric);
final Serializer<? super V> valSerializer = context.findSerializer(valueGeneric)
.createSpecific(context, valueGeneric);
valSerializer.serialize(
childEncoder,
context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.micronaut.serde.support.serializers

import io.micronaut.core.type.Argument
import io.micronaut.serde.Encoder
import io.micronaut.serde.Serializer
import spock.lang.Specification

class CustomizedMapSerializerSpec extends Specification {
def 'create specific with generics'() {
given:
def encoder = Mock(Encoder)
encoder.encodeArray(_ as Argument) >> encoder
encoder.encodeObject(_ as Argument) >> encoder

def defaultSerializer = Mock(Serializer)
defaultSerializer.createSpecific(_ as Serializer.EncoderContext, _ as Argument) >> defaultSerializer

def context = Mock(Serializer.EncoderContext)
context.findSerializer(Argument.of(String)) >> defaultSerializer
context.findSerializer({Argument a -> List.isAssignableFrom(a.type)}) >> new IterableSerializer<>()
context.findSerializer({Argument a -> Map.isAssignableFrom(a.type)}) >> new CustomizedMapSerializer<>()

def serializer = new CustomizedMapSerializer()

def type = Argument.of(Map, String, String)
def specific = serializer.createSpecific(context, type)
when:
specific.serialize(encoder, context, type, [
val1: 'abc',
val2: '123',
val3: 'true'
])
then:
encoder.encodeObject(_ as Argument)
encoder.encodeKey("val1")
encoder.encodeString("abc")
encoder.encodeKey("val2")
encoder.encodeString("123")
encoder.encodeKey("val1")
encoder.encodeString("true")
encoder.finishStructure()
}

def 'create specific without generics'() {
given:
def encoder = Mock(Encoder)
encoder.encodeArray(_ as Argument) >> encoder
encoder.encodeObject(_ as Argument) >> encoder

def defaultSerializer = Mock(Serializer)
defaultSerializer.createSpecific(_ as Serializer.EncoderContext, _ as Argument) >> defaultSerializer

def context = Mock(Serializer.EncoderContext)
context.findSerializer(Argument.of(String)) >> defaultSerializer
context.findSerializer({Argument a -> List.isAssignableFrom(a.type)}) >> new IterableSerializer<>()
context.findSerializer({Argument a -> Map.isAssignableFrom(a.type)}) >> new CustomizedMapSerializer<>()

def serializer = new CustomizedMapSerializer()

def type = Argument.of(Map)
def specific = serializer.createSpecific(context, type)
when:
specific.serialize(encoder, context, type, [
keys: [
[
val1: 'abc',
val2: '123',
val3: 'true'
]
]
])
then:
encoder.encodeObject(_ as Argument)
encoder.encodeKey("keys")
encoder.encodeArray(_ as Argument)
encoder.encodeObject(_ as Argument)
encoder.encodeKey("val1")
encoder.encodeString("abc")
encoder.encodeKey("val2")
encoder.encodeString("123")
encoder.encodeKey("val1")
encoder.encodeString("true")
3 * encoder.finishStructure()
}
}