Skip to content

jackson-dataformat-msgpack can't handle numbers as String type #421

@komamitsu

Description

@komamitsu

I found this code fails while it works with replacing new ObjectMapper(new MessagePackFactory()) with new ObjectMapper().

    public static class MyIntegerSerializer
        extends JsonSerializer<Integer>
    {
        @Override
        public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers)
                throws IOException, JsonProcessingException
        {
            gen.writeNumber(String.valueOf(value));
        }
    }

    public static class MyIntegerDeserializer
            extends JsonDeserializer<Integer>
    {
        @Override
        public Integer deserialize(JsonParser p, DeserializationContext ctxt)
                throws IOException, JsonProcessingException
        {
            return p.getNumberValue().intValue();
        }
    }

    public static void main(String[] args)
            throws IOException
    {
        ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
        objectMapper.registerModule(
                new SimpleModule()
                        .addSerializer(Integer.class, new MyIntegerSerializer())
                        .addDeserializer(Integer.class, new MyIntegerDeserializer())
        );
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        objectMapper.writeValue(out, 42);

        Integer integer = objectMapper.readValue(out.toByteArray(), Integer.class);
        System.out.println(integer);
    }

>>>> com.fasterxml.jackson.databind.JsonMappingException: writeNumber(String encodedValue) isn't supported yet

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions