3

I have an object named Item with plenty of fields. I'm using a Java JSON (json-io) library to serialize its state in a File:

String json = JsonWriter.objectToJson(item);
// Write String to File

I'd like to make the json String less verbose so that it does not include null values (if possible also boolean which are false). Is it possible to do it somehow ?
Thanks

2
  • 1
    don't forget to add the name of programming language tags to your questions. this will bring right people to your questions Commented Nov 19, 2014 at 10:48
  • if there is an answer to compare with null and order you need to search the methode toJson to add configuration example for compare that i use assertJsonEquals("{\"test\":[1,2,3]}", "{\"test\":[3,2,1]}",Configuration.empty().when(Option.IGNORING_ARRAY_ORDER).when(Option.TREATING_NULL_AS_ABSENT) ); Commented Nov 19, 2014 at 10:59

1 Answer 1

3

I can recommend using jackson mapper if possible. Check out these two questions:

Jackson serialization: ignore empty values (or null)

How to tell Jackson to ignore a field during serialization if its value is null?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, seems the best option for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.