Jackson serializer for primitive types

后端 未结 2 428
鱼传尺愫
鱼传尺愫 2020-12-20 01:01

I am writing a custom serializer to convert double values into strings in JSON objects. My code so far:

public String toJson(Object obj) throws IOException {         


        
相关标签:
2条回答
  • 2020-12-20 01:32

    You can register the JsonSerializer for the primitive type double.

    module.addSerializer(double.class, new DoubleSerializer());
    
    0 讨论(0)
  • 2020-12-20 01:39

    You can do it with custom serializer, but there is a simpler way to. Enable JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS:

    mapper.getFactory().enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);
    
    0 讨论(0)
提交回复
热议问题