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.
In Python, "boolean" is analogous to bool . Note that in JSON, true and false are lower case, whereas in Python they are capitalized ( True and False ).
JSON BooleansValues in JSON can be true/false.
To initialize an array of boolean values, use the Array() constructor to create an array of a specific length and call the fill() method on the array to populate it with booleans, e.g. new Array(3). fill(false) creates an array with 3 elements with a value of false . Copied!
So even if you send a parameter like “active=true”, it is still a string, this is how the HTTP protocol works. Hope this helps clarify the mystery. var bflag = Boolean(“true”); var bflag1 = Boolean(“false”);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With