FlexJSON does not serialize class objects completely

I am new to FlexJson and have been following http://flexjson.sourceforge.net/ for a simple guide.
I wrote a simple program, but it does not seem to be serializing the attributes of an object. Please help me if anyone knows about this

package com.webapp.enter; import flexjson.JSONSerializer; class PObject { String name; int age; String country; public PObject (String n, int a , 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 from remote answer):

I tried to change the code using getter / setter methods, now it does not talk about the following. I apologize if I do it wrong, I'm new to 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 to deepSerialize at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:97) at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22) at flexjson.JSONContext.transform(JSONContext.java:75) at flexjson.JSONSerializer.serialize(JSONSerializer.java:378) at flexjson.JSONSerializer.deepSerialize(JSONSerializer.java:301) at com.webapp.enter.Person.main(Person.java:60) Caused by: java.lang.IllegalAccessException: Class flexjson.transformer.ObjectTransformer can not access a member of class com.webapp.enter.PObject with modifiers "public" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95) at java.lang.reflect.Method.invoke(Method.java:607) at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:45) ... 5 more Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) 
+7
java json flexjson
source share
2 answers

Flexjson works with Java Beans, and PObject does not comply with the Java Bean specification. You need to add getters for your properties: name, age and country, or you need to mark these fields as public. Any of them will work. Add setters if you plan to use the JSONDeserializer to deserialize your object.

+8
source share

If you use Spring 3 or higher, you need to delete the entry, for example

map.remove ("org.springframework.validation.BindingResult.string");

from the model card immediately before serialization. It works great. You better do this in the view component where you are implementing the view. Check out the following code.

 public class JsonView implements View{ @Override public String getContentType() { return "application/json"; } @Override public void render(Map<String, ?> map, HttpServletRequest hsr, HttpServletResponse response) throws Exception { map.remove("org.springframework.validation.BindingResult.string"); final String data = new JSONSerializer().deepSerialize(map); response.getWriter().write(data); } 

}

0
source share

All Articles