java - FlexJSON doesn't serialize class objects fully -
i new flexjson , following http://flexjson.sourceforge.net/ simple tutorial.
wrote simple program seems not serializing object attributes. please me if knows this
package com.webapp.enter; import flexjson.jsonserializer; class pobject { string name; int age; string country; public pobject (string n, int , string c){ this.name = n; this.country = c; this.age = a; } public string tostring(){ return this.name + this.age + this.country; } public string[] getdata(){ return new string[]{ this.name, this.country}; } } public class person{ public static void main(string main[]){ pobject person = new pobject("harit",23,"india"); system.out.println(person.name + " - " + person.age + " - " + person.country); jsonserializer serializer = new jsonserializer(); string out = serializer.serialize(person); system.out.println("s : " + out); } }
output:
init: deps-module-jar: deps-ear-jar: deps-jar: compile-single: run-main: harit - 23 - india s : {"class":"com.webapp.enter.pobject"} build successful (total time: 0 seconds)
(update deleted answer):
i tried modify code using getter/setter methods, fails saying following. apologize if doing wrong, new this
package com.webapp.enter; import flexjson.jsonserializer; class pobject { string name; int age; string country; public pobject (){ } public void setname(string name){ this.name = name; } public void setage(int age){ this.age = age; } public void setcountry(string country){ this.country = country; } public string getname(){ return this.name; } public int getage(){ return this.age; } public string getcountry(){ return this.country; } } public class person{ public static void main(string main[]){ pobject person = new pobject(); person.setage(23); person.setcountry("usa"); person.setname("test"); system.out.println(person.name + " - " + person.age + " - " + person.country); jsonserializer serializer = new jsonserializer(); string out = serializer.serialize(person); system.out.println("s : " + out); } }
output:
test - 23 - usa exception in thread "main" flexjson.jsonexception: error trying deepserialize @ flexjson.transformer.objecttransformer.transform(objecttransformer.java:97) @ flexjson.transformer.transformerwrapper.transform(transformerwrapper.java:22) @ flexjson.jsoncontext.transform(jsoncontext.java:75) @ flexjson.jsonserializer.serialize(jsonserializer.java:378) @ flexjson.jsonserializer.deepserialize(jsonserializer.java:301) @ com.webapp.enter.person.main(person.java:60) caused by: java.lang.illegalaccessexception: class flexjson.transformer.objecttransformer can not access member of class com.webapp.enter.pobject modifiers "public" @ sun.reflect.reflection.ensurememberaccess(reflection.java:95) @ java.lang.reflect.method.invoke(method.java:607) @ flexjson.transformer.objecttransformer.transform(objecttransformer.java:45) ... 5 more java result: 1 build successful (total time: 0 seconds)
flexjson works off java beans , pobject not follow java bean specification. either need add getters properties: name, age, , country, or need mark fields public. either 1 work. add setters if plan on using jsondeserializer deserialize object.
Comments
Post a Comment