java - How to serialize ANY Object into a URI? -
my basic question: there built automatically (doesn't have part of popular library/package)? main things i'm working spring (mvc) , jackson2.
i understand there few manual ways this:
- create method in each class serializes specific properties
property=value&
form (kind of stinks because it's bunch of logic duplication, feel). - create function accepts object, , uses reflection dynamically read properties (i guess getters), , build string getting each. i'm assuming how jackson works serialization/deserialization in general, don't know.
- use feature of jackson customly serialize object. i've researched custom serializers, seems specific class (so i'd have create 1 each class i'm trying serialize), while hoping generic way. i'm having trouble understanding how apply 1 universally objects. few of links:
- use
objectmapper.convertvalue(object, hashmap.class);
, iterate onhashmap
's key/value pairs, , build string (which what i'm using now, feel conversions excessive?). - i'm guessing there's others i'm not thinking of.
the main post i've looked java: getting properties of class construct string representation
my point have several classes want able serialize without having specify specific each. that's why i'm thinking function using reflection (#2 above) way handle (if have manually).
if helps, example of mean with, say, these 2 classes:
public class c1 { private string c1prop1; private string c1prop2; private string c1prop3; // getters , setters 3 properties } public class c2 { private string c2prop1; private string c2prop2; private string c2prop3; // getters , setters 3 properties }
(no, properties names , conventions not actual app using, it's example)
the results of serializing c1prop1=value&c1prop2=value&c1prop3=value
, c2prop1=value&c2prop2=value&c2prop3=value
, there's 1 place defines how serialization happens (already defined somewhere, or created manually me).
so idea have end using form of following (taken post linked above):
public string tostring() { stringbuilder sb = new stringbuilder(); try { class c = class.forname(this.getclass().getname()); method m[] = c.getdeclaredmethods(); object oo; (int = 0; < m.length; i++) if (m[i].getname().startswith("get")) { oo = m[i].invoke(this, null); sb.append(m[i].getname().substring(3) + ":" + string.valueof(oo) + "\n"); } } catch (throwable e) { system.err.println(e); } return sb.tostring(); }
and modify accept object, , change format of items appended stringbuilder
. works me, don't need modifying now.
so again, main question if there's handles (potentially simple) serialization instead of me having (quickly) modify function above, if have specify how deal each property , value , how combine each?
if helps, background of i'm using resttemplate
(spring) make request different server, , want pass specific object's properties/values in url. understand can use like:
resttemplate.getforobject("url?c1prop1={c1prop1}&...", string.class, c1object);
i believe properties automatically mapped. said, don't want have make different url template , method each object type. i'm hoping have following:
public string getrequest(string url, object obj) { string serializeduri = serialize_uri(obj); string response = resttemplate.getforobject("url?" + serializeduri, string.class); return response; }
where serialize_uri
i'd handle it. , call getrequest("whatever", c1object);
, getrequest("whateverelse", c2object);
.
i think, solution number 4 ok. simple understand , clear.
i propose similar solution in can use @jsonanysetter annotation. please, see below example:
import com.fasterxml.jackson.annotation.jsonanysetter; import com.fasterxml.jackson.databind.objectmapper; public class jacksonprogram { public static void main(string[] args) throws exception { c1 c1 = new c1(); c1.setprop1("a"); c1.setprop3("c"); user user = new user(); user.setname("tom"); user.setsurname("irg"); objectmapper mapper = new objectmapper(); system.out.println(mapper.convertvalue(c1, uriformat.class)); system.out.println(mapper.convertvalue(user, uriformat.class)); } } class uriformat { private stringbuilder builder = new stringbuilder(); @jsonanysetter public void addtouri(string name, object property) { if (builder.length() > 0) { builder.append("&"); } builder.append(name).append("=").append(property); } @override public string tostring() { return builder.tostring(); } }
above program prints:
prop1=a&prop2=null&prop3=c name=tom&surname=irg
and getrequest method this:
public string getrequest(string url, object obj) { string serializeduri = mapper.convertvalue(obj, uriformat.class).tostring(); string response = resttemplate.getforobject(url + "?" + serializeduri, string.class); return response; }
Comments
Post a Comment