I have a bunch of POJOs I want to serialize / deserialize to JSON.
However, I'm selectively reading only some of the fields from cassandra.
So when I serialize the object to JSON, I only want those objects that were read from the database.
So all setters need to code to track that the member has been mutated/defined.
That way when I go to serialize, only the modified fields are included.
I have dozens of fields in this schema so I'd prefer to not have to write the setters directly.
Are there any POJO frameworks that do this for me?
there's no 'undefined' in Java so the default is 'null' unfortunately...
null
is your friend in this case.
If I'm understanding you correctly and the "unmodified" fields will be null at the time of serialization, and assuming you're using Jackson, just add the following annotation to the top of your class:
@JsonInclude(Include.NON_NULL) //alternatively @JsonInclude(Include.NON_EMPTY)
class Foo {
This will exclude any null (or, alternatively, empty) values from the serialized object.
If I'm not understanding you correctly and "unmodified" fields will not be null or empty, it'll be a little trickier, but not much. Let me know and I'll update with some tips.
(PS if you check out that link, you'll see that Jackson has a lot of other handy annotations)