Quotes enclosed JSON strings with custom converter

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

Quotes enclosed JSON strings with custom converter

Forage
Hi,

I'm writing a custom converter to change the default JSON output of maps from:

[["first", "value1"], ["second", "value2"]]

To:

{"first" : "value1", "second" : "value2"}

Writing the keys is no problem (startNode), but its string values (setValue) never get enclosed by quotes, even if it contains characters that need to be escaped to prevent breaking the JSON code. Adding them manually with something like the following will leave the escape character in the result.

writer.setValue("\""+ var1 + "\"");

I'm using the JsonHierarchicalStreamDriver driver.

How do I overcome this problem?

Yours sincerely,

Age
Reply | Threaded
Open this post in threaded view
|

Re: Quotes enclosed JSON strings with custom converter

Jörg Schaible-3
Hi Age,

Forage wrote:

> Hi,
>
> I'm writing a custom converter to change the default JSON output of maps
> from:
>
> [["first", "value1"], ["second", "value2"]]
>
> To:
>
> {"first" : "value1", "second" : "value2"}
>
> Writing the keys is no problem (startNode), but its string values
> (setValue) never get enclosed by quotes, even if it contains characters
> that need to be escaped to prevent breaking the JSON code. Adding them
> manually with something like the following will leave the escape character
> in the result.
>
> writer.setValue("\""+ var1 + "\"");
>
> I'm using the JsonHierarchicalStreamDriver driver.
>
> How do I overcome this problem?

For JSON the model should internally know what kind of data you're writing.
Therefore you have to tell it with a special call:

((ExtendedHierarchicalStreamWriter)writer).startNode("first", String.class);
writer.writeValue(var1);

Hope this helps,
Jörg



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

    http://xircles.codehaus.org/manage_email