painlessjson
painlessjson copied to clipboard
Should support Optional items
I started using painlessjson but because of #36 moved to jsonizer - but since #36 got fixed I was trying to move back to painlessjson - however there a few things which I really like about jsonzier - the first is that it provides an JsonizeOptional.yes which I can use to cope with cases when my json may or may not contain a particular field.
The other main thing is I feel I have more control with jsonizer - because I can explicitly mark the things I want to be included - but I see that painlessjson does this the opposite way and I can ignore things I don't want to be included. I think perhaps I just quite like marking the things I want rather than the things I don't.
And finally perhaps something you can add - you can group several things together - so if they are all optional they can do in a block:
@jsonize {
string id;
string name;
}
What does optional precisely mean in this case? PainlessJSON does ignore values that are not present in json. Is that what you mean?
Yes this is what I mean - It was not clear to me that values not present in the json were ignored. But having a way to mark something as optional could work the other way also - by marking something as not optional - could enforce correct serialising/deserialising as it would throw an missing non-optional member exception during.
I don't actually need to go that way at the moment and I guess I might never - so perhaps just knowing that items not present in my json won't end up in my object is enough for me.
On 6 Apr 2015, at 20:57, Edwin van Leeuwen [email protected] wrote:
What does optional precisely mean in this case? PainlessJSON does ignore values that are not present in json. Is that what you mean?
— Reply to this email directly or view it on GitHub https://github.com/BlackEdder/painlessjson/issues/39#issuecomment-90223643.
I think having the ability to mark things as required is a great idea actually!
To come back to your earlier comment. Yeah I decided at the start for an opt out approach (i.e. specify when to ignore), because I prefer that by default everything gets serialized. This is very much a subjective thing though. I guess we could support both at some point.
About the block: I would expect that to actually work now, but have not tested it:
@SerializeIgnore {
string id;
string name;
}
PainlessJSON will create an object as long as it can be instantiated. If you want to force a value to be present I think you should do that by forcing an invariant by having a limiting constructor. I think we should clarify that that's the recommended way of forcing values to be present. I still see that there are cases where this is not an option so we should provide alternatives as well. So my way forward is an annotation for forcing the value to be present and documentation about the recommended way.