java - Is there a way to make a "generic" json object? -
currently doing that:
public class myobject{ public string name; //constructor here }
so, if serialize it:
objectmapper mapper = new objectmapper(); myobject o = new myobject("peter"); mapper.writevalue(system.out,o);
i
{"name":"peter"}
i'd make generic, class be:
public class myobject{ public string name; public string value; //constructor here }
so call:
objectmapper mapper = new objectmapper(); myobject o = new myobject("apple","peter"); mapper.writevalue(system.out,o);
would lead to
{"apple":"peter"}
is possible?
you seem asking way store generically named properties , values, render them json. properties
natural representation this, , objectmapper
knows how serialize this:
objectmapper mapper = new objectmapper(); properties p = new properties(); p.put("apple", "peter"); p.put("orange", "annoying"); p.put("quantity", 3); mapper.writevalue(system.out, p);
output:
{"apple":"peter","orange":"annoying","quantity":3}
while it's true can build objectnode
s scratch, using objectnode
store data may lead undesirable coupling of jackson , internal business logic. if can use more appropriate existing object, such properties
or variant of map
, can keep json side isolated data.
adding this: conceptually, want ask "what object?"
- is json object node? consider
objectnode
. - is collection of properties? consider
properties
. - is general -> b map isn't properties (e.g. collection of, say, strings -> frequency counts)? consider type of
map
.
jackson can handle of those.
Comments
Post a Comment