@@ -676,6 +676,7 @@ package custom;
676
676
677
677
import com.fasterxml.jackson.annotation.JsonCreator;
678
678
import com.fasterxml.jackson.annotation.JsonProperty;
679
+ import io.micronaut.context.annotation.Bean;
679
680
import io.micronaut.core.annotation.NonNull;
680
681
import io.micronaut.core.type.Argument;
681
682
import io.micronaut.serde.annotation.Serdeable;
@@ -697,7 +698,66 @@ record CustomValue(
697
698
}
698
699
}
699
700
700
- @Singleton
701
+ @Bean(typed = CustomSerde.class)
702
+ class CustomSerde implements Deserializer<Integer> {
703
+ @Override
704
+ public Integer getDefaultValue(DecoderContext ignoredContext, Argument<? super Integer> ignoredType) {
705
+ return -2;
706
+ }
707
+
708
+ @Override
709
+ public Integer deserialize(Decoder decoder, DecoderContext context, Argument<? super Integer> type) throws IOException {
710
+ return decoder.decodeInt();
711
+ }
712
+
713
+ @Override
714
+ public Integer deserializeNullable(Decoder decoder, DecoderContext context, Argument<? super Integer> type) throws IOException {
715
+ if (decoder.decodeNull()) {
716
+ return -1;
717
+ }
718
+ return decoder.decodeInt();
719
+ }
720
+ }
721
+ ''' )
722
+
723
+ expect :
724
+ jsonMapper. readValue(' {"value":1}' , typeUnderTest). value() == 1
725
+ jsonMapper. readValue(' {"value":null}' , typeUnderTest). value() == -1
726
+ jsonMapper. readValue(' {}' , typeUnderTest). value() == -2
727
+
728
+ cleanup :
729
+ context. close()
730
+ }
731
+ void " test custom deserializer 2" () {
732
+
733
+ given :
734
+ def context = buildContext(' custom.CustomValue' ,'''
735
+ package custom;
736
+
737
+ import com.fasterxml.jackson.annotation.JsonCreator;
738
+ import com.fasterxml.jackson.annotation.JsonProperty;
739
+ import io.micronaut.context.annotation.Bean;
740
+ import io.micronaut.core.annotation.NonNull;
741
+ import io.micronaut.core.type.Argument;
742
+ import io.micronaut.serde.annotation.Serdeable;
743
+ import io.micronaut.serde.annotation.SerdeImport;
744
+ import io.micronaut.serde.Decoder;
745
+ import io.micronaut.serde.Deserializer;
746
+ import jakarta.inject.Singleton;
747
+ import java.io.IOException;
748
+
749
+ @Serdeable
750
+ record CustomValue(
751
+ @Serdeable.Deserializable(using = CustomSerde.class)
752
+ @NonNull
753
+ Integer value
754
+ ) {
755
+ @JsonCreator
756
+ public CustomValue {
757
+ }
758
+ }
759
+
760
+ @Bean(typed = CustomSerde.class)
701
761
class CustomSerde implements Deserializer<Integer> {
702
762
@Override
703
763
public Integer getDefaultValue(DecoderContext ignoredContext, Argument<? super Integer> ignoredType) {
@@ -727,4 +787,99 @@ class CustomSerde implements Deserializer<Integer> {
727
787
cleanup :
728
788
context. close()
729
789
}
790
+
791
+ void " test custom serializer" () {
792
+ given :
793
+ def context = buildContext(' custom.CustomValue' ,'''
794
+ package custom;
795
+
796
+ import com.fasterxml.jackson.annotation.JsonCreator;
797
+ import com.fasterxml.jackson.annotation.JsonProperty;
798
+ import io.micronaut.context.annotation.Bean;
799
+ import io.micronaut.core.annotation.NonNull;
800
+ import io.micronaut.core.type.Argument;
801
+ import io.micronaut.serde.Encoder;
802
+ import io.micronaut.serde.Serializer;
803
+ import io.micronaut.serde.annotation.Serdeable;
804
+ import io.micronaut.serde.annotation.SerdeImport;
805
+ import io.micronaut.serde.Decoder;
806
+ import jakarta.inject.Singleton;
807
+ import java.io.IOException;
808
+
809
+ @Serdeable
810
+ record CustomValue(
811
+ @Serdeable.Serializable(using = CustomSerde.class)
812
+ @JsonProperty("value")
813
+ @NonNull
814
+ Integer value
815
+ ) {
816
+ @JsonCreator
817
+ public CustomValue {
818
+ }
819
+ }
820
+
821
+ @Bean(typed = CustomSerde.class)
822
+ class CustomSerde implements Serializer<Integer> {
823
+ @Override
824
+ public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Integer> type, Integer value) throws IOException {
825
+ encoder.encodeInt(123);
826
+ }
827
+ }
828
+ ''' )
829
+
830
+ expect :
831
+ jsonMapper. writeValueAsString(typeUnderTest. type. newInstance(1 )) == """ {"value":123}"""
832
+ jsonMapper. writeValueAsString(typeUnderTest. type. newInstance(2 )) == """ {"value":123}"""
833
+ jsonMapper. writeValueAsString(typeUnderTest. type. newInstance(3 )) == """ {"value":123}"""
834
+
835
+ cleanup :
836
+ context. close()
837
+ }
838
+
839
+ void " test custom serializer 2" () {
840
+ given :
841
+ def context = buildContext(' custom.CustomValue' ,'''
842
+ package custom;
843
+
844
+ import com.fasterxml.jackson.annotation.JsonCreator;
845
+ import com.fasterxml.jackson.annotation.JsonProperty;
846
+ import io.micronaut.context.annotation.Bean;
847
+ import io.micronaut.core.annotation.NonNull;
848
+ import io.micronaut.core.type.Argument;
849
+ import io.micronaut.serde.Encoder;
850
+ import io.micronaut.serde.Serializer;
851
+ import io.micronaut.serde.annotation.Serdeable;
852
+ import io.micronaut.serde.annotation.SerdeImport;
853
+ import io.micronaut.serde.Decoder;
854
+ import jakarta.inject.Singleton;
855
+ import java.io.IOException;
856
+
857
+ @Serdeable
858
+ record CustomValue(
859
+ @Serdeable.Serializable(using = CustomSerde.class)
860
+ @NonNull
861
+ Integer value
862
+ ) {
863
+ @JsonCreator
864
+ public CustomValue {
865
+ }
866
+ }
867
+
868
+ @Bean(typed = CustomSerde.class)
869
+ class CustomSerde implements Serializer<Integer> {
870
+ @Override
871
+ public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Integer> type, Integer value) throws IOException {
872
+ encoder.encodeInt(123);
873
+ }
874
+ }
875
+ ''' )
876
+
877
+ expect :
878
+ jsonMapper. writeValueAsString(typeUnderTest. type. newInstance(1 )) == """ {"value":123}"""
879
+ jsonMapper. writeValueAsString(typeUnderTest. type. newInstance(2 )) == """ {"value":123}"""
880
+ jsonMapper. writeValueAsString(typeUnderTest. type. newInstance(3 )) == """ {"value":123}"""
881
+
882
+ cleanup :
883
+ context. close()
884
+ }
730
885
}
0 commit comments