bracket causing problems?

1473 views
Skip to first unread message

jack

unread,
Sep 2, 2009, 3:03:07 PM9/2/09
to google-gson
i understand json uses bracket "[" to group objects. so if my object
has a string value which contains brackets "..[..]..", could it cause
problems for the gson parser when de-serializing json strings? thanks.

jack.

Ian Clarke

unread,
Sep 2, 2009, 3:42:03 PM9/2/09
to googl...@googlegroups.com

Jack, JSON uses square brackets for the start and end of JSON arrays
(which are like lists in other languages).

Using these square brackets inside JSON strings won't be a problem.
The only characters you have to worry about using inside a string are
" and \, these must be escaped. I would guess that this escaping is
handled automatically by Gson but I haven't tried it personally.

You can read about the JSON spec here, its pleasingly simple: http://json.org/

Ian.

--
Ian Clarke
CEO, Uprizer Labs
Email: i...@uprizer.com
Ph: +1 512 422 3588
Fax: +1 512 276 6674

John

unread,
Sep 7, 2009, 9:21:35 AM9/7/09
to google-gson
Surprisingly gson doesn't seem to escape string values and haven't
found a utility in the code to do this.

I think it should automatically do this as any recursed objects on
serialization are potentially problematic.

Can we check this in immediately on the toString() method? otherwise I
will have to use a different library for serialization which is a bit
mad.

On Sep 2, 8:42 pm, Ian Clarke <ian.cla...@gmail.com> wrote:

Ian Clarke

unread,
Sep 8, 2009, 1:30:00 PM9/8/09
to googl...@googlegroups.com
On Mon, Sep 7, 2009 at 8:21 AM, John<jo...@jdiligence.com> wrote:
> Surprisingly gson doesn't seem to escape string values and haven't
> found a utility in the code to do this.
>
> I think it should automatically do this as any recursed objects on
> serialization are potentially problematic.

I agree, correct behavior should be to escape strings per the JSON
spec while serializing to JSON, and de-escaping (?) them when
converting back to Java strings.

John, you should file a bug report for this.

inder

unread,
Sep 8, 2009, 1:31:02 PM9/8/09
to google-gson
Gson does escape Strings and if you have found something that should
be escaped but isn't please do let us know or file a bug.
An example with a string and expected value will be great in helping
debug the problem.

Inder

srinivas uppala

unread,
Aug 6, 2020, 1:52:54 PM8/6/20
to google-gson
Its causing problem when a string has open brackets.

ex: 
"tradeItemDescription": {
     "path": "version.namePublicLong.[0].data"
 }

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 159 path $.tradeItem.self.tradeItemDescription.path
    at com.google.gson.Gson.fromJson(Gson.java:942)
    at com.google.gson.Gson.fromJson(Gson.java:892)
    at com.google.gson.Gson.fromJson(Gson.java:841)
Reply all
Reply to author
Forward
0 new messages