简体   繁体   English

将 Decimal128 序列化为 JSON

[英]Serializing Decimal128 to JSON

I am using spring boot for creating an api where I retrieve values from mongodb.我正在使用 spring 引导来创建 api,其中 ICON 从 mongodb 检索值。 The problem I am facing is that Decimal128 fields are serialized like:我面临的问题是Decimal128字段被序列化为:

"userid": {
  "high": 3476778912330022912,
  "low": 10776,
  "naN": false,
  "infinite": false,
  "finite": true,
  "negative": false
}

Here is the body of my controller:这是我的 controller 的主体:

var wrappers = new EmbeddedWrappers(false);

var collection = mongoTemplate.getCollection("blah");

var result = collection.find().limit(1000).into(new ArrayList<>());

if (result == null) {
  return ResponseEntity.notFound().build();
}

return ResponseEntity.ok(new Resources<>(Arrays.asList(wrappers.wrap(result))));

Any ideas how I can correctly serialize Decimal128 values so that I only have the value itself?任何想法如何正确序列化Decimal128值以便我只有值本身?

Based on the comments:根据评论:

@Configuration
public class ObjectMapperConfig {

  @Bean
  public ObjectMapper objectMapper() {
    var mapper = new ObjectMapper();

    var module = new SimpleModule();
    module.addSerializer(Decimal128.class, new Decimal128Serializer());
    mapper.registerModule(module);

    return mapper;
  }

  private static class Decimal128Serializer extends JsonSerializer<Decimal128> {
    @Override
    public void serialize(Decimal128 v, JsonGenerator g, SerializerProvider p) throws IOException {
      g.writeNumber(v.bigDecimalValue());
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM