1

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

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
user2824073
  • 2,407
  • 10
  • 39
  • 73
  • 1
    don't forget to add the name of programming language tags to your questions. this will bring right people to your questions – Igor Milla Nov 19 '14 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) ); – Ran Adler Nov 19 '14 at 10:59

1 Answers1

1

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?

Community
  • 1
  • 1
dubes
  • 5,324
  • 3
  • 34
  • 47