使用JoiObject Mapper时出现问题。

huangapple go评论77阅读模式
英文:

Issue while using JoiObject Mapper

问题

我们正在使用JoiObjectMapper将一个POJO类转换为Json字符串。
Jackson版本:2.8.x
以下是对象映射器的配置:

import com.amazon.jacksonion.JoiObjectMapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public static ObjectMapper createObjectMapper() {
    ObjectMapper mapper = new JoiObjectMapper();

    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    mapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);

    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    mapper.registerModule(new JodaModule());

    mapper.registerModule(new JavaTimeModule());

    SimpleModule module = new SimpleModule();

    module.addSerializer(new JavaUtilDateSerializer());
    mapper.registerModule(module);
    return mapper;
}

我们使用objectMapper.writeValueAsString(object);方法将POJO转换为Json。

@Data
public class POJO {
    @JsonProperty("a")
    private String a;

    @JsonProperty("b")
    private String b;
}

问题:在转换为字符串时,对象映射器从Json键值中删除了双引号。

实际输出

{
 a : "abc",
 b : "cde"
} 

期望输出

{
   "a" : "abc",
   "b" : "cde"
}

我们需要带有双引号的Json。有人可以帮助我们找出在这里漏掉了什么吗?

英文:

We are using JoiObjectMapper to convert a POJO class into Json String.
Jackson version : 2.8.x
The following is object mapper configuration :


import com.amazon.jacksonion.JoiObjectMapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;


    public static ObjectMapper createObjectMapper() {        
        ObjectMapper mapper = new JoiObjectMapper();

        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        mapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);

        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        mapper.registerModule(new JodaModule());

        mapper.registerModule(new JavaTimeModule());

        SimpleModule module = new SimpleModule();

        module.addSerializer(new JavaUtilDateSerializer());
        mapper.registerModule(module);
return mapper;
}

We are using objectMapper.writeValueAsString(object); method to convert the POJO into Json.

@Data
public class POJO {
@JsonProperty("a")
private String a;

@JsonProperty("b")
private String b;

}

Issue: While converting to string the object mapper is removing the double quotes from Json Key values.

Actual Output :

{
 a : "abc",
 b : "cde"
} 

Expected Output:

{
   "a" : "abc",
   "b" : "cde"
}

We need the json with double quotes. Can someone help us what are we missing here ??

答案1

得分: 1

我认为你正在寻找布尔属性 JsonGenerator.Feature.QUOTE_FIELD_NAMES。如果我记得没错,几年前它默认为 true。也许现在已经改变了。尝试将其设置为 truefalse 看看是否有效。

英文:

I think you are looking for the boolean property JsonGenerator.Feature.QUOTE_FIELD_NAMES. If I am remembering well it was true by default some years ago. Maybe this has changed now. Try to set it true or false to see if it works.

huangapple
  • 本文由 发表于 2020年4月9日 05:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61110037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定