|
17 | 17 |
|
18 | 18 | import com.fasterxml.jackson.core.JsonGenerator; |
19 | 19 | import com.fasterxml.jackson.core.JsonEncoding; |
| 20 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 21 | +import com.fasterxml.jackson.databind.JsonSerializer; |
20 | 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.SerializerProvider; |
21 | 24 | import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 25 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
22 | 26 | import org.junit.Test; |
23 | 27 | import org.msgpack.core.ExtensionTypeHeader; |
24 | 28 | import org.msgpack.core.MessagePack; |
@@ -732,4 +736,152 @@ public void testComplexTypeKeyWithV06Format() |
732 | 736 | assertThat(unpacker.unpackString(), is("foo")); |
733 | 737 | assertThat(unpacker.unpackInt(), is(42)); |
734 | 738 | } |
| 739 | + |
| 740 | + // Test serializers that store a string as a number |
| 741 | + |
| 742 | + public static class IntegerSerializerStoringAsString |
| 743 | + extends JsonSerializer<Integer> |
| 744 | + { |
| 745 | + @Override |
| 746 | + public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) |
| 747 | + throws IOException, JsonProcessingException |
| 748 | + { |
| 749 | + gen.writeNumber(String.valueOf(value)); |
| 750 | + } |
| 751 | + } |
| 752 | + |
| 753 | + @Test |
| 754 | + public void serializeStringAsInteger() |
| 755 | + throws IOException |
| 756 | + { |
| 757 | + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 758 | + objectMapper.registerModule( |
| 759 | + new SimpleModule().addSerializer(Integer.class, new IntegerSerializerStoringAsString())); |
| 760 | + |
| 761 | + assertThat( |
| 762 | + MessagePack.newDefaultUnpacker(objectMapper.writeValueAsBytes(Integer.MAX_VALUE)).unpackInt(), |
| 763 | + is(Integer.MAX_VALUE)); |
| 764 | + } |
| 765 | + |
| 766 | + public static class LongSerializerStoringAsString |
| 767 | + extends JsonSerializer<Long> |
| 768 | + { |
| 769 | + @Override |
| 770 | + public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) |
| 771 | + throws IOException, JsonProcessingException |
| 772 | + { |
| 773 | + gen.writeNumber(String.valueOf(value)); |
| 774 | + } |
| 775 | + } |
| 776 | + |
| 777 | + @Test |
| 778 | + public void serializeStringAsLong() |
| 779 | + throws IOException |
| 780 | + { |
| 781 | + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 782 | + objectMapper.registerModule( |
| 783 | + new SimpleModule().addSerializer(Long.class, new LongSerializerStoringAsString())); |
| 784 | + |
| 785 | + assertThat( |
| 786 | + MessagePack.newDefaultUnpacker(objectMapper.writeValueAsBytes(Long.MIN_VALUE)).unpackLong(), |
| 787 | + is(Long.MIN_VALUE)); |
| 788 | + } |
| 789 | + |
| 790 | + public static class FloatSerializerStoringAsString |
| 791 | + extends JsonSerializer<Float> |
| 792 | + { |
| 793 | + @Override |
| 794 | + public void serialize(Float value, JsonGenerator gen, SerializerProvider serializers) |
| 795 | + throws IOException, JsonProcessingException |
| 796 | + { |
| 797 | + gen.writeNumber(String.valueOf(value)); |
| 798 | + } |
| 799 | + } |
| 800 | + |
| 801 | + @Test |
| 802 | + public void serializeStringAsFloat() |
| 803 | + throws IOException |
| 804 | + { |
| 805 | + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 806 | + objectMapper.registerModule( |
| 807 | + new SimpleModule().addSerializer(Float.class, new FloatSerializerStoringAsString())); |
| 808 | + |
| 809 | + assertThat( |
| 810 | + MessagePack.newDefaultUnpacker(objectMapper.writeValueAsBytes(Float.MAX_VALUE)).unpackFloat(), |
| 811 | + is(Float.MAX_VALUE)); |
| 812 | + } |
| 813 | + |
| 814 | + public static class DoubleSerializerStoringAsString |
| 815 | + extends JsonSerializer<Double> |
| 816 | + { |
| 817 | + @Override |
| 818 | + public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) |
| 819 | + throws IOException, JsonProcessingException |
| 820 | + { |
| 821 | + gen.writeNumber(String.valueOf(value)); |
| 822 | + } |
| 823 | + } |
| 824 | + |
| 825 | + @Test |
| 826 | + public void serializeStringAsDouble() |
| 827 | + throws IOException |
| 828 | + { |
| 829 | + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 830 | + objectMapper.registerModule( |
| 831 | + new SimpleModule().addSerializer(Double.class, new DoubleSerializerStoringAsString())); |
| 832 | + |
| 833 | + assertThat( |
| 834 | + MessagePack.newDefaultUnpacker(objectMapper.writeValueAsBytes(Double.MIN_VALUE)).unpackDouble(), |
| 835 | + is(Double.MIN_VALUE)); |
| 836 | + } |
| 837 | + |
| 838 | + public static class BigDecimalSerializerStoringAsString |
| 839 | + extends JsonSerializer<BigDecimal> |
| 840 | + { |
| 841 | + @Override |
| 842 | + public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) |
| 843 | + throws IOException, JsonProcessingException |
| 844 | + { |
| 845 | + gen.writeNumber(String.valueOf(value)); |
| 846 | + } |
| 847 | + } |
| 848 | + |
| 849 | + @Test |
| 850 | + public void serializeStringAsBigDecimal() |
| 851 | + throws IOException |
| 852 | + { |
| 853 | + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 854 | + objectMapper.registerModule( |
| 855 | + new SimpleModule().addSerializer(BigDecimal.class, new BigDecimalSerializerStoringAsString())); |
| 856 | + |
| 857 | + BigDecimal bd = BigDecimal.valueOf(Long.MAX_VALUE).add(BigDecimal.ONE); |
| 858 | + assertThat( |
| 859 | + MessagePack.newDefaultUnpacker(objectMapper.writeValueAsBytes(bd)).unpackDouble(), |
| 860 | + is(bd.doubleValue())); |
| 861 | + } |
| 862 | + |
| 863 | + public static class BigIntegerSerializerStoringAsString |
| 864 | + extends JsonSerializer<BigInteger> |
| 865 | + { |
| 866 | + @Override |
| 867 | + public void serialize(BigInteger value, JsonGenerator gen, SerializerProvider serializers) |
| 868 | + throws IOException, JsonProcessingException |
| 869 | + { |
| 870 | + gen.writeNumber(String.valueOf(value)); |
| 871 | + } |
| 872 | + } |
| 873 | + |
| 874 | + @Test |
| 875 | + public void serializeStringAsBigInteger() |
| 876 | + throws IOException |
| 877 | + { |
| 878 | + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 879 | + objectMapper.registerModule( |
| 880 | + new SimpleModule().addSerializer(BigInteger.class, new BigIntegerSerializerStoringAsString())); |
| 881 | + |
| 882 | + BigInteger bi = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE); |
| 883 | + assertThat( |
| 884 | + MessagePack.newDefaultUnpacker(objectMapper.writeValueAsBytes(bi)).unpackDouble(), |
| 885 | + is(bi.doubleValue())); |
| 886 | + } |
735 | 887 | } |
0 commit comments