Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object to Json including only fields not null

Tags:

java

json

string

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

like image 596
user2824073 Avatar asked Jun 06 '26 01:06

user2824073


1 Answers

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?

like image 74
dubes Avatar answered Jun 08 '26 15:06

dubes