java - JSON: use deserializing name different than serializing name json -
i have 1 class user
, received json (for user class) system1 , should read info , validate forward system2, can't touch these 2 systems, problem names of keys different, want differentiate between deserialized , serialized name received json :
{"userid":"user1","pwd":"123456","country":"us"}
"{"username":"user1","password":"123456","country":"us"}"
but sent should
i using gson lib, , code, please help
user class:
public class user implements cloneable { @serializedname("username") private string username ; @serializedname("password") private string password ; @serializedname("country") private string country ; public user(string username, string password, string country) { super(); this.username = username; this.password = password; this.country = country; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getcountry() { return country; } public void setcountry(string country) { this.country = country; } }
testjson class:
import com.google.gson.gson; import com.google.gson.gsonbuilder; public class testjson { private static gsonbuilder gsonbuilder; private static gson gson; public static object fromjson(string json, class clz) { gson = new gson(); return gson.fromjson(json, clz); } public static string tojson(object obj) { gsonbuilder = new gsonbuilder(); gson = gsonbuilder.create(); string json = gson.tojson(obj); return json; } public static void main(string[] args) { string json2 = "{\"userid\":\"user1\",\"pwd\":\"123456\",\"country\":\"us\"}"; user user=(user) testjson.fromjson(json2, user.class); system.out.println(user.getpassword()); user u =new user("user1","123456","us"); string json1=testjson.tojson(u); system.out.println(json1); } }
you can create custom serializer/deserializer purpose.
serializer:
public class userserializer implements jsonserializer<user> { @override public jsonelement serialize(user obj, type type, jsonserializationcontext jsonserializationcontext) { .......... } }
deserializer:
public class userdeserializer implements jsondeserializer<user> { @override public user deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { ........... } }
and create gson instance:
gsonbuilder gsonbuilder = new gsonbuilder(); gsonbuilder.registertypeadapter(user.class, new userserializer()); gsonbuilder.registertypeadapter(user.class, new userdeserializer()); gson gson = gsonbuilder.create();
example
edit: example of custom deserializer might fit need. don't need custom serializer in case.
add userdeserializer.java
:
public class userdeserializer implements jsondeserializer<user> { @override public user deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { jsonobject obj = json.getasjsonobject(); user user = new user(obj.get("userid").getasstring(), obj.get("pwd").getasstring(), obj.get("country").getasstring()); return user; } }
replace fromjson
implementation (i use generic avoid need casting when calling fromjson
):
public static <t> t fromjson(string json, class<t> clz) { gsonbuilder = new gsonbuilder(); gsonbuilder.registertypeadapter(user.class, new userdeserializer()); gson = gsonbuilder.create(); return gson.fromjson(json, clz); }
Comments
Post a Comment