discomarathon / google-gson

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

JsonArray of primative to Collection of non-Primative or Strings fails #70

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have this model:
public class Foo {
  private String part1;
  private String part2;

  public Foo(String string) {
    String[] parts = strings.split["|"];
    part1 = parts[0];
    part2 = parts[1];
  }
}

I have this deserializer:
public class FooAdapter implements JsonDeserializer<Foo>,
InstanceCreator<Foo> {

    //Fill up Eden with objects that will be thrown away
    public Foo createInstance(Type type) {
        return new Foo("unknown|thing");
    }

    public Foodeserialize(JsonElement src, Type type,
JsonDeserializationContext context)
            throws JsonParseException {
        return new Foo(src.getAsString());
    }
}

I think have this line that fails:
new GsonBuilder().registerTypeAdapter(Foo.class, new FooAdapter())
                .create().fromJson("[\"Jacob|Tomaw\"]", new
TypeToken<Set<Foo>>() {}.getType());

com.google.gson.JsonParseException: Can not adapt type class
java.lang.String to class Foo

Original issue reported on code.google.com by jacob.to...@gmail.com on 14 Nov 2008 at 5:44

GoogleCodeExporter commented 9 years ago
I added testS in r292 that check this functionality but could not reproduce 
this bug. 
Either this bug is invalid, or has been fixed in the big overhaul that happened 
in
version 1.2.3. 

I noticed that the above test has a bug. strings.split("|") will split "abc" 
into
"a","b","c" because | is a reserved word for regex.

Original comment by inder123 on 14 Nov 2008 at 10:02

GoogleCodeExporter commented 9 years ago
Dang,
Caught coding in a textarea! :)

I was trying to make the example lest contrived, but the reality is that
FooAdapter.deserialize is never called in v1.2.2

Original comment by jacob.to...@gmail.com on 14 Nov 2008 at 10:05

GoogleCodeExporter commented 9 years ago
Added more tests for this bug in r293.

Jacob, can you look at the tests and see if that is what you were intending?

My tests (based on your code) essentially ensure that custom
serializers/deserializers are called for the elements of a collection or a map.

Original comment by inder123 on 14 Nov 2008 at 10:12

GoogleCodeExporter commented 9 years ago
Yeah that test looks spot on.

I copied the test into my check out of 1.2.2 and it fails exactly like I would
expect.  So if it work in 1.2.3 then I think I am satisfied.

Original comment by jacob.to...@gmail.com on 14 Nov 2008 at 10:35