Serializing Java Map/Collection into JSON

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Serializing Java Map/Collection into JSON

Venkatesh
Hi !

I have been using XStream to convert from Java objects to JSON and vice versa. I had a question and was hoping someone in this list can provide some help/guidance. I have a Java object that has a HashMap as a member:

public class MyObject  {
    private HashMap<String, String> myMap = new ConcurrentHashMap();
}

I am trying to use XStream to serialize this object and the serialized object using default XStream looks like the following:

{
"MyObject: : {
"myMap" : [ {"@class" : "java.util.ConcurrentHashMap", "id1" : "data"} ]
 }
}

I am looking for something like the following:

"MyObject" : {
"myMap" : {
"id1" : "data",
}
}

Essentially not treat the myMap object as a array and then adding the object type data in the "class". I was looking at the mailing list; but I haven't had much success. Any help would be really appreciated.

Venkatesh


Reply | Threaded
Open this post in threaded view
|

Re: Serializing Java Map/Collection into JSON

Venkatesh
My problem is close to the issue reported here:

but I am not sure I understand what "writing the key as a tag" means. Sorry for the newbie question.


On Sat, Apr 27, 2013 at 11:22 AM, Venkatesh <[hidden email]> wrote:
Hi !

I have been using XStream to convert from Java objects to JSON and vice versa. I had a question and was hoping someone in this list can provide some help/guidance. I have a Java object that has a HashMap as a member:

public class MyObject  {
    private HashMap<String, String> myMap = new ConcurrentHashMap();
}

I am trying to use XStream to serialize this object and the serialized object using default XStream looks like the following:

{
"MyObject: : {
"myMap" : [ {"@class" : "java.util.ConcurrentHashMap", "id1" : "data"} ]
 }
}

I am looking for something like the following:

"MyObject" : {
"myMap" : {
"id1" : "data",
}
}

Essentially not treat the myMap object as a array and then adding the object type data in the "class". I was looking at the mailing list; but I haven't had much success. Any help would be really appreciated.

Venkatesh



Reply | Threaded
Open this post in threaded view
|

Re: Serializing Java Map/Collection into JSON

Venkatesh
In reply to this post by Venkatesh
A little more context:

I wrote a custom MapConverter. Below is the implementation:

public MyCustomMapConverter extends MapConverter  {

.....
        @Override
    public void marshal(Object source, HierarchicalStreamWriter writer,
            MarshallingContext context) {
        Map map = (Map)source;
        for (java.util.Iterator itor = map.entrySet().iterator(); 
                itor.hasNext(); ) {
            Map.Entry entry = (Map.Entry)itor.next();
            writer.startNode(entry.getKey().toString());
            writer.setValue(entry.getValue().toString());
            writer.endNode();
        }
    }
}

With this implementation, I am still unable to get XStream to add them as "direct" values. It still drops these key/values as a part of a JSON array?

Venkatesh


On Sat, Apr 27, 2013 at 11:22 AM, Venkatesh <[hidden email]> wrote:
Hi !

I have been using XStream to convert from Java objects to JSON and vice versa. I had a question and was hoping someone in this list can provide some help/guidance. I have a Java object that has a HashMap as a member:

public class MyObject  {
    private HashMap<String, String> myMap = new ConcurrentHashMap();
}

I am trying to use XStream to serialize this object and the serialized object using default XStream looks like the following:

{
"MyObject: : {
"myMap" : [ {"@class" : "java.util.ConcurrentHashMap", "id1" : "data"} ]
 }
}

I am looking for something like the following:

"MyObject" : {
"myMap" : {
"id1" : "data",
}
}

Essentially not treat the myMap object as a array and then adding the object type data in the "class". I was looking at the mailing list; but I haven't had much success. Any help would be really appreciated.

Venkatesh



Reply | Threaded
Open this post in threaded view
|

Re: Serializing Java Map/Collection into JSON

Jörg Schaible-2
Hi,

Venkatesh wrote:

> A little more context:
>
> I wrote a custom MapConverter. Below is the implementation:
>
> public MyCustomMapConverter extends MapConverter  {
>
> .....
>         @Override
>     public void marshal(Object source, HierarchicalStreamWriter writer,
>             MarshallingContext context) {
>         Map map = (Map)source;
>         for (java.util.Iterator itor = map.entrySet().iterator();
>                 itor.hasNext(); ) {
>             Map.Entry entry = (Map.Entry)itor.next();
>             writer.startNode(entry.getKey().toString());
>             writer.setValue(entry.getValue().toString());
>             writer.endNode();
>         }
>     }
> }
>
> With this implementation, I am still unable to get XStream to add them as
> "direct" values. It still drops these key/values as a part of a JSON
> array?

How do you initialize the XStream now?

- Jörg

>
> Venkatesh
>
>
> On Sat, Apr 27, 2013 at 11:22 AM, Venkatesh
> <[hidden email]> wrote:
>
>> Hi !
>>
>> I have been using XStream to convert from Java objects to JSON and vice
>> versa. I had a question and was hoping someone in this list can provide
>> some help/guidance. I have a Java object that has a HashMap as a member:
>>
>> public class MyObject  {
>>     private HashMap<String, String> myMap = new ConcurrentHashMap();
>> }
>>
>> I am trying to use XStream to serialize this object and the serialized
>> object using default XStream looks like the following:
>>
>> {
>> "MyObject: : {
>> "myMap" : [ {"@class" : "java.util.ConcurrentHashMap", "id1" : "data"} ]
>>  }
>> }
>>
>> I am looking for something like the following:
>>
>> "MyObject" : {
>> "myMap" : {
>> "id1" : "data",
>> }
>> }
>>
>> Essentially not treat the myMap object as a array and then adding the
>> object type data in the "class". I was looking at the mailing list; but I
>> haven't had much success. Any help would be really appreciated.
>>
>> Venkatesh
>>
>>
>>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Serializing Java Map/Collection into JSON

Venkatesh
Jorge:

Thanks for responding to me. I didn't do anything specific; only set Xstream's mode to use NO_REFERENCES.

Venkatesh




On Sat, Apr 27, 2013 at 4:43 PM, Jörg Schaible <[hidden email]> wrote:
Hi,

Venkatesh wrote:

> A little more context:
>
> I wrote a custom MapConverter. Below is the implementation:
>
> public MyCustomMapConverter extends MapConverter  {
>
> .....
>         @Override
>     public void marshal(Object source, HierarchicalStreamWriter writer,
>             MarshallingContext context) {
>         Map map = (Map)source;
>         for (java.util.Iterator itor = map.entrySet().iterator();
>                 itor.hasNext(); ) {
>             Map.Entry entry = (Map.Entry)itor.next();
>             writer.startNode(entry.getKey().toString());
>             writer.setValue(entry.getValue().toString());
>             writer.endNode();
>         }
>     }
> }
>
> With this implementation, I am still unable to get XStream to add them as
> "direct" values. It still drops these key/values as a part of a JSON
> array?

How do you initialize the XStream now?

- Jörg

>
> Venkatesh
>
>
> On Sat, Apr 27, 2013 at 11:22 AM, Venkatesh
> <[hidden email]> wrote:
>
>> Hi !
>>
>> I have been using XStream to convert from Java objects to JSON and vice
>> versa. I had a question and was hoping someone in this list can provide
>> some help/guidance. I have a Java object that has a HashMap as a member:
>>
>> public class MyObject  {
>>     private HashMap<String, String> myMap = new ConcurrentHashMap();
>> }
>>
>> I am trying to use XStream to serialize this object and the serialized
>> object using default XStream looks like the following:
>>
>> {
>> "MyObject: : {
>> "myMap" : [ {"@class" : "java.util.ConcurrentHashMap", "id1" : "data"} ]
>>  }
>> }
>>
>> I am looking for something like the following:
>>
>> "MyObject" : {
>> "myMap" : {
>> "id1" : "data",
>> }
>> }
>>
>> Essentially not treat the myMap object as a array and then adding the
>> object type data in the "class". I was looking at the mailing list; but I
>> haven't had much success. Any help would be really appreciated.
>>
>> Venkatesh
>>
>>
>>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Serializing Java Map/Collection into JSON

Venkatesh
Hit the "Send" button a bit too soon. I also wanted to say that I am using the HierarchicalStreamDriver. 


On Sat, Apr 27, 2013 at 4:58 PM, Venkatesh <[hidden email]> wrote:
Jorge:

Thanks for responding to me. I didn't do anything specific; only set Xstream's mode to use NO_REFERENCES.

Venkatesh




On Sat, Apr 27, 2013 at 4:43 PM, Jörg Schaible <[hidden email]> wrote:
Hi,

Venkatesh wrote:

> A little more context:
>
> I wrote a custom MapConverter. Below is the implementation:
>
> public MyCustomMapConverter extends MapConverter  {
>
> .....
>         @Override
>     public void marshal(Object source, HierarchicalStreamWriter writer,
>             MarshallingContext context) {
>         Map map = (Map)source;
>         for (java.util.Iterator itor = map.entrySet().iterator();
>                 itor.hasNext(); ) {
>             Map.Entry entry = (Map.Entry)itor.next();
>             writer.startNode(entry.getKey().toString());
>             writer.setValue(entry.getValue().toString());
>             writer.endNode();
>         }
>     }
> }
>
> With this implementation, I am still unable to get XStream to add them as
> "direct" values. It still drops these key/values as a part of a JSON
> array?

How do you initialize the XStream now?

- Jörg

>
> Venkatesh
>
>
> On Sat, Apr 27, 2013 at 11:22 AM, Venkatesh
> <[hidden email]> wrote:
>
>> Hi !
>>
>> I have been using XStream to convert from Java objects to JSON and vice
>> versa. I had a question and was hoping someone in this list can provide
>> some help/guidance. I have a Java object that has a HashMap as a member:
>>
>> public class MyObject  {
>>     private HashMap<String, String> myMap = new ConcurrentHashMap();
>> }
>>
>> I am trying to use XStream to serialize this object and the serialized
>> object using default XStream looks like the following:
>>
>> {
>> "MyObject: : {
>> "myMap" : [ {"@class" : "java.util.ConcurrentHashMap", "id1" : "data"} ]
>>  }
>> }
>>
>> I am looking for something like the following:
>>
>> "MyObject" : {
>> "myMap" : {
>> "id1" : "data",
>> }
>> }
>>
>> Essentially not treat the myMap object as a array and then adding the
>> object type data in the "class". I was looking at the mailing list; but I
>> haven't had much success. Any help would be really appreciated.
>>
>> Venkatesh
>>
>>
>>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|

Re: Re: Serializing Java Map/Collection into JSON

Jörg Schaible-2
Hi Venkatesh,

Venkatesh wrote:

> Hit the "Send" button a bit too soon. I also wanted to say that I am using
> the HierarchicalStreamDriver.


I though you wanted also deserialization?
 
 
> On Sat, Apr 27, 2013 at 4:58 PM, Venkatesh
> <[hidden email]> wrote:
>
>> Jorge:
>>
>> Thanks for responding to me. I didn't do anything specific; only set
>> Xstream's mode to use NO_REFERENCES.

And how is your converter then registered?

- Jörg



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Re: Serializing Java Map/Collection into JSON

Venkatesh
Jorge:

Yes, I need both. So, I also have unmarshal implementation in this class. It works and so I left that out.

I register the converter by simply calling xstream.registerConverter(new MyMapConverter(xstream.mapper())).

Venkatesh

On Apr 28, 2013, at 8:15 AM, Jörg Schaible <[hidden email]> wrote:

> Hi Venkatesh,
>
> Venkatesh wrote:
>
>> Hit the "Send" button a bit too soon. I also wanted to say that I am using
>> the HierarchicalStreamDriver.
>
>
> I though you wanted also deserialization?
>
>
>> On Sat, Apr 27, 2013 at 4:58 PM, Venkatesh
>> <[hidden email]> wrote:
>>
>>> Jorge:
>>>
>>> Thanks for responding to me. I didn't do anything specific; only set
>>> Xstream's mode to use NO_REFERENCES.
>
> And how is your converter then registered?
>
> - Jörg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Re: Serializing Java Map/Collection into JSON

Venkatesh
I see your question now. I am actually using a JettisonMappedXmlDriver.


On Sun, Apr 28, 2013 at 9:28 AM, Venkatesh <[hidden email]> wrote:
Jorge:

Yes, I need both. So, I also have unmarshal implementation in this class. It works and so I left that out.

I register the converter by simply calling xstream.registerConverter(new MyMapConverter(xstream.mapper())).

Venkatesh

On Apr 28, 2013, at 8:15 AM, Jörg Schaible <[hidden email]> wrote:

> Hi Venkatesh,
>
> Venkatesh wrote:
>
>> Hit the "Send" button a bit too soon. I also wanted to say that I am using
>> the HierarchicalStreamDriver.
>
>
> I though you wanted also deserialization?
>
>
>> On Sat, Apr 27, 2013 at 4:58 PM, Venkatesh
>> <[hidden email]> wrote:
>>
>>> Jorge:
>>>
>>> Thanks for responding to me. I didn't do anything specific; only set
>>> Xstream's mode to use NO_REFERENCES.
>
> And how is your converter then registered?
>
> - Jörg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: Serializing Java Map/Collection into JSON

Jörg Schaible-2
Hi Venkatesh,

Venkatesh wrote:

> I see your question now. I am actually using a JettisonMappedXmlDriver.
>
>
> On Sun, Apr 28, 2013 at 9:28 AM, Venkatesh
> <[hidden email]> wrote:
>
>> Jorge:
>>
>> Yes, I need both. So, I also have unmarshal implementation in this class.
>> It works and so I left that out.
>>
>> I register the converter by simply calling xstream.registerConverter(new
>> MyMapConverter(xstream.mapper())).

OK. You will have to overwrite JettisonStaxWriter.startNode(String,Class),
because the current implementation will tell Jettison to handle a tag as
JSON array if it is a Map. Normally you would simply overwrite the driver's
createWriter method, to return your adjusted implementation, but
unfortunately this is not possible, because you don't have access to the
objects you need to feed the constructor of JettisonStaxWriter.

So, simply take a private copy of the JettisonMappedXmlDriver and adjust the
two createWriter methods to return your writer implementation:

 class MyWriter extends JettisonStaxWriter {
   ...
   public void startNode(String tag, Class clazz) {
     if (Map.class.isAssignableFrom(clazz))
       startNode(tag);
     else
       super.startNode(tag, clazz);
   }
 }

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: Serializing Java Map/Collection into JSON

Golenko
This post has NOT been accepted by the mailing list yet.
Hi, Jörg!
I'm having the same issue with Map as the topicstarter.
The solution with JettisonStaxWriter works fine, but as I understand (after searching on this forum) I can't drop the root when using JettisonMappedXmlDriver.

And using JsonHierarchicalStreamDriver I can't make the Map serializing like:
"myMap" : {
"id1" : "data",
}

Can You help me with this? Thanks!