Sunday 15 March 2015

java - JSON jackson creator: Default Value and nice to have values -



java - JSON jackson creator: Default Value and nice to have values -

i have build json mandatory fields, defaulted fields (if value not set), , optional (not mandatory) fields. class used de- , serialization. final json should different, if e.g. not mandatory field not set or mandatory value not set, should defaulted.

@jsonsnakecase @jsonignoreproperties(ignoreunknown=true) public class tryoutjson { @jsoncreator public tryoutjson( @notblank @jsonproperty("mandatory") final string mandatory, @...what here set default? @jsonproperty("valuedefaultedto5ifnotset") final string valuedefaultedto5ifnotset, @... here set if value given? @jsonproperty("thisfieldisnotmandatory") final string thisfieldisnotmandatory, ) throws illegalargumentexception { //.... } }

for illustration json strings should created in used constructor:

{"mandatory":"mandatoryvalue","valuedefaultedto5ifnotset":"5"} same {"mandatory":"mandatoryvalue"}

or

{"mandatory":"mandatoryvalue","valuedefaultedto5ifnotset":"2","thisfieldisnotmandatory":"valuexy"}

is possible have 1 constructor settings? dont want build several constructors each possible variant. if important, imports used:

import org.hibernate.validator.constraints.notblank; import com.fasterxml.jackson.annotation.jsoncreator; import com.fasterxml.jackson.annotation.jsonignoreproperties; import com.fasterxml.jackson.annotation.jsonproperty;

java json jackson

No comments:

Post a Comment