Hi devs/Sagara, I have done some background research on how others (jettison, Apache Camal, Apache wink etc....) convert XML to JSON format. Here is a summary of that.
*Apache camal* <http://camel.apache.org/> Apache camal uses json-lib to convert xml to json and vise-verse[0]<http://camel.apache.org/xmljson.html>. To use json-lib the xml should be in a special format which is supported by json-lib, Therefore it identifies whether the xml element should be written as json array or json object from class attribute of each xml start element in xml stream[1] <http://json-lib.sourceforge.net/usage.html#xml>[2]<http://json-lib.sourceforge.net/apidocs/jdk15/index.html>. eg: <a class="array"< <e type="number">1</e> <e type="number">2</e> <e type="number">3</e> </a> *Jettison * Jettison uses XMLEventWriter implementation to convert XML to JSON[3]<http://stackoverflow.com/a/8368752/942164>. Inside this XMLEventWriter it maintains stack which gets dynamically extended with next XMLEvent. Finally, when it receives EndDocument, it writes that stack to XMLStreamWriter. In this scenario, it can't recognise a JSON array which has only one value. *Apache wink * Apache wink also uses Jettison to convert xml to json. In stackoverflow they suggest implementing our own implementation for this [4] <http://stackoverflow.com/a/7218820/942164> [0]http://camel.apache.org/xmljson.html [1]http://json-lib.sourceforge.net/usage.html#xml [2]http://json-lib.sourceforge.net/apidocs/jdk15/index.html [3]http://stackoverflow.com/a/8368752/942164 [4]http://stackoverflow.com/a/7218820/942164 Thanks! Shameera On Fri, Jul 6, 2012 at 10:59 AM, Sagara Gunathunga < [email protected]> wrote: > On Fri, Jul 6, 2012 at 10:38 AM, Sagara Gunathunga > <[email protected]> wrote: > > @Shmeera, > > > > Can you look into how Apache Camel [1] [2] handle XML <=> JSON in your > > use cases I believe you can find some hints from there ? > > > > [1] - http://camel.apache.o*Apache > > camal*rg/xmljson.html<http://camel.apache.org/xmljson.html> > > [2] - http://json-lib.sourceforge.net/ > > Also you could use following convention [1] . > > > [1] - > http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html > > Thanks ! > > > > > Thanks ! > > > > On Wed, Jul 4, 2012 at 3:37 PM, Amila Suriarachchi > > <[email protected]> wrote: > >> > >> > >> On Mon, Jul 2, 2012 at 3:44 PM, Sagara Gunathunga > >> <[email protected]> wrote: > >>> > >>> On Sun, Jul 1, 2012 at 8:50 PM, Amila Suriarachchi > >>> <[email protected]> wrote: > >>> > > >>> > > >>> > On Fri, Jun 29, 2012 at 12:23 PM, Shameera Rathnayaka > >>> > <[email protected]> wrote: > >>> >> > >>> >> Hi devs, > >>> >> > >>> >> I have a problem with, writing OMElement to the wire > >>> >> at the JsonFormatter, The problem is how do i identify > >>> >> whether it is a JSON object or JSON array. > >>> >> > >>> >> As an example let's say i have this OMElement, > >>> >> > >>> >> <student> > >>> >> <name>micky</name> > >>> >> <age>15</age> > >>> >> <subject>History</subject> > >>> >> <subject>Science</subject> > >>> >> </student> > >>> >> > >>> >> So the expected JSON Object is, > >>> >> {"student":{"name":'micky" , "age":15 , "subject":["History", > >>> >> "Science"]}} > >>> >> > >>> >> The thing is how should I know the student should be a JsonArray, > when > >>> >> i > >>> >> receive a <subject> Start element? > >>> >> > >>> >> Another thing is if i get below OMElement, How do i know subject is > a > >>> >> JsonArray? > >>> >> <student> > >>> >> <name>micky</name> > >>> >> <age>15</age> > >>> >> <subject>History</subject> > >>> >> </student> > >>> >> > >>> >> The relevant JSON object should be, > >>> >> > >>> >> {"student":{"name":'micky" , "age":15 , "subject":["History"]}} > >>> >> > >>> >> It would be nice to hear your ideas to overcome these problems. > >>> > > >>> > > >>> > It think we can not solve this issue unless the serialiser aware of > some > >>> > scheme. So this seems to be another limitation about converting xml > to > >>> > json > >>> > and wise versa. > >>> > > >>> > Shall we follow this convention to over come this problem. > >>> > > >>> > if we take your example, shall we stick to a method where it always > try > >>> > to > >>> > serialise fields as attributes unless we sure it is an array. > >>> > > >>> > For an example > >>> > > >>> > <student> > >>> > <name>micky</name> > >>> > <age>15</age> > >>> > <subject>History</subject> > >>> > </student> > >>> > > >>> > this will be > >>> > > >>> > {"student":{"name":'micky" , "age":15 , "subject":"History"}} > >>> > > >>> > and > >>> > > >>> > <student> > >>> > <name>micky</name> > >>> > <age>15</age> > >>> > <subject>History</subject> > >>> > <subject>Science</subject> > >>> > </student> > >>> > > >>> > {"student":{"name":'micky" , "age":15 , "subject":["History", > >>> > "Science"]}} > >>> > > >>> > I think even implementation wise this will not be straight forward. > The > >>> > only > >>> > thing we know for sure root element is not an array. > >>> > > >>> > Then after completing the first element, if the second element is > also > >>> > have > >>> > the same element name then we can assume it is an array and other > wise > >>> > it is > >>> > a normal attribute. > >>> > >>> Amila, > >>> > >>> Your suggestion introduce another problem, format of response JSON > >>> message depends on return data quantities. As an example for a same > >>> service it use two response formats when the response having size 1 > >>> array and having size > 1 arrays. I think this is a major issue for > >>> caller side. > >> > >> > >> yes. But as you can see this problem can not solve. so my suggestion > works > >> for single field elements. But for arrays it has the problem you > mentioned. > >> Receiving end should process the message accordingly. > >> > >>> > >>> > >>> What about to use hybrid approach where XMLStramReader based approach > >>> ( 2nd approach) use to handle incoming requests and use Gson stream > >>> directly to write response message from Java ( Java => Gson) without > >>> using XMLStramWriter (1st approach) ? > >> > >> > >> We already have java ==> Gson binding and it work fine. > >> > >> The idea of this is to implement an xml stream using json so that any > >> service (eg POJO, ADB services) written top of those API can use that. > But > >> as Shammera pointed out this xml json conversion has problems. > >> > >> thanks, > >> Amila. > >> > >>> > >>> > >>> Thanks ! > >>> > >>> > > >>> > thanks, > >>> > Amila. > >>> > > >>> > > >>> >> > >>> >> > >>> >> Thanks, > >>> >> Shameera. > >>> >> > >>> >> > >>> >> > >>> >> > >>> >> On Mon, Jun 25, 2012 at 11:10 AM, Shameera Rathnayaka > >>> >> <[email protected]> wrote: > >>> >>> > >>> >>> Hi devs, > >>> >>> > >>> >>>> Everything is fine, if i try to print OMElement using > >>> >>>> omElement.toString(); > >>> >>>> it prints OMElement without any error. But after returning from > >>> >>>> JsonBuilder, > >>> >>>> and inside the TransportUtils class it throws a class cast > exception > >>> >>>> when it try > >>> >>>> to add this returned OMelement to the SoapEnvelop by using > >>> >>>> envelope.getBody().addChild(documentElement); here is the code > >>> >>>> segment > >>> >>>> in > >>> >>>> TransportUtils class > >>> >>>> > >>> >>>> envelope = soapFactory.getDefaultEnvelope(); > >>> >>>> if (documentElement != null) { > >>> >>>> envelope.getBody().addChild(documentElement); // THIS LINE > >>> >>>> THROWS > >>> >>>> AN EXCEPTION > >>> >>>> } > >>> >>>> > >>> >>>> The exception is java.lang.ClassCastException: > >>> >>>> org.apache.axiom.om.impl.llom.OMDocumentImpl cannot be cast to > >>> >>>> org.apache.axiom.om.impl.OMNodeEx > >>> >>> > >>> >>> > >>> >>> I could able to solve this , there was an error in my > >>> >>> GsonXMLStreamReader > >>> >>> , I had to dig AXIOM to identify this. AXIOM process the parser > while > >>> >>> it > >>> >>> receive XMLStreamReader.END_ELEMENT but my implementation had an > error > >>> >>> returning the END_ELEMENT, I solved it now I an not getting above > >>> >>> ClassCastException. > >>> >>> > >>> >>> Thanks, > >>> >>> Shameera. > >>> >>> > >>> >>>> > >>> >>>> > >>> >>>> What should be the problem for this? , do i use wrong way to get > >>> >>>> the OMElement or something else?? devs any comments on this ?? > >>> >>>> > >>> >>>> Thanks, > >>> >>>> Shameera. > >>> >>>> > >>> >>>> > >>> >>>> On Sat, May 5, 2012 at 11:30 AM, Shameera Rathnayaka > >>> >>>> <[email protected]> wrote: > >>> >>>>> > >>> >>>>> Hi devs , > >>> >>>>> > >>> >>>>> According to my second approach, I need to map json elements in > >>> >>>>> request > >>> >>>>> to relevant XML tags/text and vice versa. When i am working on > this, > >>> >>>>> there > >>> >>>>> is a problem arises that which convention should i expect for > this. > >>> >>>>> So i > >>> >>>>> need to clarify whether the following convention is correct or > are > >>> >>>>> there any > >>> >>>>> suggestions for it. > >>> >>>>> > >>> >>>>> json message > >>> >>>>> > >>> >>>>> { "alise": { > >>> >>>>> "bob": { > >>> >>>>> "x": "valueX", > >>> >>>>> "y": "valueY", > >>> >>>>> "z": ["valueU","valueV","valueW"], > >>> >>>>> "l": { "name": "john", "age": 23 }, > >>> >>>>> "m": [ { "a": ["A","B","C" ] }, { "b": ["D","E","F" > ] > >>> >>>>> } > >>> >>>>> ] > >>> >>>>> } > >>> >>>>> } > >>> >>>>> } > >>> >>>>> > >>> >>>>> Relevant,expected xml message from this json request (actually, > it > >>> >>>>> is > >>> >>>>> not converted to xml) > >>> >>>>> > >>> >>>>> <json> > >>> >>>>> <alise> > >>> >>>>> <bob> > >>> >>>>> <x>valueX</x> > >>> >>>>> <y>valueY</y> > >>> >>>>> <z>valueU</z> > >>> >>>>> <z>valueV</z> > >>> >>>>> <z>valueW</z> > >>> >>>>> <l> > >>> >>>>> <name>john</name> > >>> >>>>> <age>23</age> > >>> >>>>> </l> > >>> >>>>> <m> > >>> >>>>> <a>A</a> > >>> >>>>> <a>B</a> > >>> >>>>> <a>C</a> > >>> >>>>> </m> > >>> >>>>> <m> > >>> >>>>> <b>D</b> > >>> >>>>> <b>E</b> > >>> >>>>> <b>F</b> > >>> >>>>> </m> > >>> >>>>> </bob> > >>> >>>>> </alise> > >>> >>>>> </json> > >>> >>>>> > >>> >>>>> > >>> >>>>> Thanks > >>> >>>>> Shameera. > >>> >>>>> > >>> >>>>> > >>> >>>>> On Tue, Apr 24, 2012 at 7:12 PM, Shameera Rathnayaka > >>> >>>>> <[email protected]> wrote: > >>> >>>>>> > >>> >>>>>> Hi devs, > >>> >>>>>> > >>> >>>>>> As this project is accepted for GSoC 2012, I would like > >>> >>>>>> to continue my work and looking forward to finish my project > >>> >>>>>> as a success in this summer. According to my mentor (Amila > >>> >>>>>> Suriarachchi) > >>> >>>>>> it is better to improve my knowledge further about Axis2 > >>> >>>>>> architecture > >>> >>>>>> in this community bounding period. Therefore I'll start to read > the > >>> >>>>>> Documentation and Apache Axis2 book. > >>> >>>>>> > >>> >>>>>> Andreas, as you mentioned in the previous mail thread[1] could > you > >>> >>>>>> please point out some of the code samples in synapse which do > >>> >>>>>> a similar work as i do in my 2nd approach of the project?. > >>> >>>>>> > >>> >>>>>> I will use this thread for future discussion about the project. > >>> >>>>>> And I'll keep update my implementation patches to AXIS2-5270 > too. > >>> >>>>>> > >>> >>>>>> > >>> >>>>>> [1] > >>> >>>>>> > >>> >>>>>> > http://axis.markmail.org/thread/4lg7xefplv7o65z6#query:page:1+mid:u7zauh37nj4mje6n+state:results > >>> >>>>>> > >>> >>>>>> Thanks > >>> >>>>>> Shameera. > >>> >>>>>> > >>> >>>>>> -- > >>> >>>>>> Shameera Rathnayaka > >>> >>>>>> Undergraduate > >>> >>>>>> Department of Computer Science and Engineering > >>> >>>>>> University of Moratuwa. > >>> >>>>>> Sri Lanka. > >>> >>>>>> > >>> >>>>>> Blog : http://shameerarathnayaka.blogspot.com/ > >>> >>>>>> > >>> >>>>> > >>> >>>>> > >>> >>>>> > >>> >>>>> -- > >>> >>>>> Shameera Rathnayaka > >>> >>>>> Undergraduate > >>> >>>>> Department of Computer Science and Engineering > >>> >>>>> University of Moratuwa. > >>> >>>>> Sri Lanka. > >>> >>>>> > >>> >>>>> Blog : http://shameerarathnayaka.blogspot.com/ > >>> >>>>> > >>> >>>> > >>> >>>> > >>> >>>> > >>> >>>> -- > >>> >>>> Shameera Rathnayaka > >>> >>>> Undergraduate > >>> >>>> Department of Computer Science and Engineering > >>> >>>> University of Moratuwa. > >>> >>>> Sri Lanka. > >>> >>>> > >>> >>>> Blog : http://shameerarathnayaka.blogspot.com/ > >>> >>>> > >>> >>> > >>> >>> > >>> >>> > >>> >>> > >>> >>> -- > >>> >>> Shameera Rathnayaka > >>> >>> Undergraduate > >>> >>> Department of Computer Science and Engineering > >>> >>> University of Moratuwa. > >>> >>> Sri Lanka. > >>> >>> > >>> >>> Blog : http://shameerarathnayaka.blogspot.com/ > >>> >>> > >>> >> > >>> >> > >>> >> > >>> >> -- > >>> >> Shameera Rathnayaka > >>> >> Undergraduate > >>> >> Department of Computer Science and Engineering > >>> >> University of Moratuwa. > >>> >> Sri Lanka. > >>> >> > >>> >> Blog : http://shameerarathnayaka.blogspot.com/ > >>> >> > >>> > > >>> > > >>> > > >>> > -- > >>> > Amila Suriarachchi > >>> > WSO2 Inc. > >>> > blog: http://amilachinthaka.blogspot.com/ > >>> > >>> > >>> > >>> -- > >>> Sagara Gunathunga > >>> > >>> Blog - http://ssagara.blogspot.com > >>> Web - http://people.apache.org/~sagara/ > >>> LinkedIn - http://www.linkedin.com/in/ssagara > >>> > >>> --------------------------------------------------------------------- > >>> To unsubscribe, e-mail: [email protected] > >>> For additional commands, e-mail: [email protected] > >>> > >> > >> > >> > >> -- > >> Amila Suriarachchi > >> WSO2 Inc. > >> blog: http://amilachinthaka.blogspot.com/ > > > > > > > > -- > > Sagara Gunathunga > > > > Blog - http://ssagara.blogspot.com > > Web - http://people.apache.org/~sagara/ > > LinkedIn - http://www.linkedin.com/in/ssagara > > > > -- > Sagara Gunathunga > > Blog - http://ssagara.blogspot.com > Web - http://people.apache.org/~sagara/ > LinkedIn - http://www.linkedin.com/in/ssagara > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > > -- Shameera Rathnayaka Undergraduate Department of Computer Science and Engineering University of Moratuwa. Sri Lanka. Blog : http://shameerarathnayaka.blogspot.com/
