There is com.google.gson.Gson as well, which I have used in OIDCDiscovery project [1], (which is not yet merged) to convert a Java object to JSON. But jackson seems more suitable, as it supports data-binding functionality.
[1] https://github.com/wso2/carbon-identity/pull/1694/ On Sat, Jan 30, 2016 at 9:28 PM, Rajeenthini Satkunam <[email protected]> wrote: > Hi, > > I have worked on writing Jaggery test for Jaggery-Product.I can observe > when we tried to log the object it always gives us empty JSON > ({}).Currently the implementation goes this way when we try to log java > object. > > So I was trying to get rid of this observed behavior of Jaggery.I have > gone through a solution. > > *using Jackson[1] * > > We can convert java object to JSON[2] and can log it. > > I have shared the piece of code I have tried out with Jackson below. > > *Simple POJO Student class* > > public class Student { > private int age; > private String name; > private String indexNo; > > public int getAge() { > return age; > } > > public void setAge(int age) { > this.age = age; > } > > public String getName() { > return name; > } > > public void setName(String name) { > this.name = name; > } > > public String getIndexNo() { > return indexNo; > } > > public void setIndexNo(String indexNo) { > this.indexNo = indexNo; > } > } > > *Simple class to test Jackson* > > import com.fasterxml.jackson.databind.ObjectMapper; > import util.Student; > public class MyTest { > public static void main(String args[]){ > Student st = new Student(); > st.setIndexNo("DS001"); > st.setAge(12); > st.setName("kareena"); > try{ > ObjectMapper mapper = new ObjectMapper(); > String jsonInString = mapper.writeValueAsString(st); > > System.out.println("************************************************"); > System.out.println(jsonInString); > > System.out.println("************************************************"); > > }catch(Exception e){ > System.out.print("Exception caught "+ e); > } > > } > } > > Actual output by above code. > ************************************************ > {"age":12,"name":"kareena","indexNo":"DS001"} > ************************************************ > > I have added a dependency in the POM.xml > > <dependency> > <groupId>com.fasterxml.jackson.core</groupId> > <artifactId>jackson-databind</artifactId> > <version>2.6.3</version> > </dependency> > > > IMHO we can use the same scenario in case of log JAVA object.Your help and > suggestions are more appreciated and guide me if I am wrong or I can have > better solution than this. > > [1] - https://github.com/FasterXML/jackson-databind > [2] - > http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ > -- > > *Thank You.* > > *Rajeenthini Satkunam* > > *Associate Software Engineer | WSO2* > > > *E:[email protected] <[email protected]>* > > *M :+94770832823 <%2B94770832823> * > > > _______________________________________________ > Dev mailing list > [email protected] > http://wso2.org/cgi-bin/mailman/listinfo/dev > > -- Hareendra Chamara Philips *Software Engineer* Mobile : +94 (0) 767 184161 <%2B94%20%280%29%20773%20451194> [email protected] <[email protected]>
_______________________________________________ Dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/dev
