Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

json

jackson

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.

like image 468
Vignesh Avatar asked May 15 '13 12:05

Vignesh


People also ask

How is boolean value represented in JSON?

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 ).

Can we send boolean value in JSON?

JSON BooleansValues in JSON can be true/false.

How do you push a boolean to an array?

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!

How do you pass a boolean value in Postman?

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”);


1 Answers

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.

like image 117
Michał Ziober Avatar answered Oct 19 '22 19:10

Michał Ziober