How to write boolean value as String in a json array?

为君一笑 提交于 2021-02-16 06:26:07

问题


JsonGenerator generator = 
                new JsonFactory().createJsonGenerator(new JSONWriter(response));
generator.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);

I used JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS to write numbers as string in json. But, I couldn't find similar feature to write boolean value as string.


回答1:


I couldn't find similar feature for boolean, also. So, I propose to write new serializer and deserializer for boolean fields.

See my example:

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        Foo foo = new Foo();
        foo.setB(true);
        foo.setS("Test");
        foo.setI(39);

        ObjectMapper objectMapper = new ObjectMapper();
        JsonFactory jsonFactory = new JsonFactory();

        StringWriter stringWriter = new StringWriter();
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);
        jsonGenerator.enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);
        objectMapper.writeValue(jsonGenerator, foo);
        System.out.println(stringWriter);

        JsonParser jsonParser = jsonFactory.createJsonParser(stringWriter.toString());
        Foo value = objectMapper.readValue(jsonParser, Foo.class);
        System.out.println(value);
    }
}

class BooleanSerializer extends JsonSerializer<Boolean> {

    @Override
    public void serialize(Boolean value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeString(value.toString());
    }
}

class BooleanDeserializer extends JsonDeserializer<Boolean> {

    public Boolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        return Boolean.valueOf(jsonParser.getValueAsString());
    }
}

class Foo {

    @JsonSerialize(using = BooleanSerializer.class)
    @JsonDeserialize(using = BooleanDeserializer.class)
    private boolean b;
    private String s;
    private int i;

    public boolean isB() {
        return b;
    }

    public void setB(boolean b) {
        this.b = b;
    }

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    @Override
    public String toString() {
        return "Foo [b=" + b + ", s=" + s + ", i=" + i + "]";
    }
}

Output:

{"b":"true","s":"Test","i":"39"}
Foo [b=true, s=Test, i=39]

EDIT

I think, you should add SimpleModule configuration to ObjectMapper:

SimpleModule simpleModule = new SimpleModule("BooleanModule");
simpleModule.addSerializer(Boolean.class, new BooleanSerializer());
simpleModule.addDeserializer(Boolean.class, new BooleanDeserializer());

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(simpleModule);

Now, you should be able to serialize boolean/Object List-s and Map-s.




回答2:


All I know of a working json string looks something like this:

string json = "
{
"somestring": "some string value"
"someboolean": true
}
";

Use the multiline syntax specific to the language you use.



来源:https://stackoverflow.com/questions/16564639/how-to-write-boolean-value-as-string-in-a-json-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!