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

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 strings in json. But I could not find a similar function to write a boolean as a string.

+6
source share
2 answers

I also could not find a similar function for Booleans. So, I suggest writing a 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 the 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); 

You should now be able to serialize Boolean / Object List-s and Map-s.

+5
source

I do not quite understand what you are saying, but try changing the type of boolean strings. Instead of true try "true" as String

-2
source

Source: https://habr.com/ru/post/945068/


All Articles