dyf128 / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

SerializedName should allow the @ character #290

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm having trouble deserialising json of the following form:

{ "@value" : 3 }

As far as I can tell this is valid json, however gson's JsonFieldNameValidator 
appears to dislike the presence of the @ character.

I'm declaring the gson java class:

static class MyObj {
    @SerializedName("@value")
    int value;
}

I'd expect this to work, but instead I get an exception:

What is the expected output? What do you see instead?
xception in thread "main" java.lang.IllegalArgumentException: @value is not a 
valid JSON field name.
    at com.google.gson.JsonFieldNameValidator.validate(JsonFieldNameValidator.java:52)
    at com.google.gson.SerializedNameAnnotationInterceptingNamingPolicy.translateName(SerializedNameAnnotationInterceptingNamingPolicy.java:45)
    at com.google.gson.JsonObjectDeserializationVisitor.getFieldName(JsonObjectDeserializationVisitor.java:92)
    at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:97)
    at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
    at com.google.gson.JsonDeserializationVisitor.visitChild(JsonDeserializationVisitor.java:107)
    at com.google.gson.JsonDeserializationVisitor.visitChildAsObject(JsonDeserializationVisitor.java:95)
    at com.google.gson.JsonObjectDeserializationVisitor.visitObjectField(JsonObjectDeserializationVisitor.java:61)
    at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:164)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
    at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
    at com.google.gson.Gson.fromJson(Gson.java:568)
    at com.google.gson.Gson.fromJson(Gson.java:515)
    at com.google.gson.Gson.fromJson(Gson.java:484)
    at com.google.gson.Gson.fromJson(Gson.java:434)
    at com.google.gson.Gson.fromJson(Gson.java:406)

I'm using gson 1.6 on Ubuntu.

Original issue reported on code.google.com by t.denley on 17 Feb 2011 at 9:11

GoogleCodeExporter commented 9 years ago
Similar to issue 121, where the hyphen character was added.
http://code.google.com/p/google-gson/issues/detail?id=121

Original comment by t.denley on 17 Feb 2011 at 9:13

GoogleCodeExporter commented 9 years ago
I have the same problem, so i have done a patch to pass this.

Warning, the issue 121 isn't about the @ caracter.

Original comment by ybonnel on 5 Apr 2011 at 6:40

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for doing a patch. I think that allowing a bypass of field validation is 
a bit overkill though. I'd suggest the following patch might be a bit more 
targeted.

Original comment by t.denley on 5 Apr 2011 at 10:20

Attachments:

GoogleCodeExporter commented 9 years ago
Are there any committers reviewing bugs at the moment?
Given the simplicity of this fix, is there any chance of getting it into 1.7?

Original comment by t.denley on 7 Apr 2011 at 6:21

GoogleCodeExporter commented 9 years ago
Unfortunately, with certain browsers this would cause problems while doing 
evals on JSON objects. That said, I'm not too worried about relaxing this 
constraint on Gson since we should be aligning more closely with the JSON spec 
rather than focusing on what certain clients can and cannot handle.

Original comment by joel.leitch@gmail.com on 12 Apr 2011 at 4:42

GoogleCodeExporter commented 9 years ago
I'm surprised that we do any name policy enforcement at all. JSON permits 
anything in a name; we should too. If that means we can't map a property to a 
field, then we have to fail at runtime.

Original comment by limpbizkit on 12 Apr 2011 at 4:48

GoogleCodeExporter commented 9 years ago
Fixed in r798.

Original comment by joel.leitch@gmail.com on 12 Apr 2011 at 5:31

GoogleCodeExporter commented 9 years ago
Thanks for the fix.

Are there instructions for doing a local snapshot build anywhere?

I was hoping to lift a suitable artefact from gson CI, but 
http://continuousbuild.dyndns.org:8080/job/gson/ seems rather sad at the moment.

Original comment by t.denley on 12 Apr 2011 at 7:13

GoogleCodeExporter commented 9 years ago
We are planning to push a new version of Gson today. Can you wait another day 
for the public release?

Original comment by joel.leitch@gmail.com on 12 Apr 2011 at 9:36

GoogleCodeExporter commented 9 years ago
Sure, no rush.

I was under the impression that this fix hadn't made it into 1.7 though. Am I 
mistaken?

Original comment by t.denley on 12 Apr 2011 at 10:09

GoogleCodeExporter commented 9 years ago
The 1.7 push went out earlier today that contains this fix. Enjoy!

Original comment by joel.leitch@gmail.com on 13 Apr 2011 at 7:16

GoogleCodeExporter commented 9 years ago
Fantastic! Thanks.

Original comment by t.denley on 13 Apr 2011 at 8:03