jasonzue / google-gson

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

Map key can not use java bean. #214

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.User.java

package gson.test;

public class User {

    private String id;

    private String name;

    /** {@inheritDoc} */
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
2.Test.java
package gson.test;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Gson gson = new Gson();
        User user = new User();
        user.setId("1001");
        user.setName("张三");

        Map<User,User> map = new HashMap<User, User>();
        map.put(user, user);
        String jsonString = gson.toJson(map);
        System.out.println(jsonString);
        Object o = gson.fromJson(jsonString,new TypeToken<Map<User, User>>(){}.getType());
        System.out.println( o);
    }

}

3.

What is the expected output? What do you see instead?
{"User [id=1001, name=张三]":{"id":"1001","name":"张三"}}
Exception in thread "main" com.google.gson.JsonParseException: Expecting object 
found: "User [id=1001, name=张三]"
    at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:100)
    at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:150)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
    at com.google.gson.JsonDeserializationContextDefault.fromJsonPrimitive(JsonDeserializationContextDefault.java:84)
    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:53)
    at com.google.gson.DefaultTypeAdapters$MapTypeAdapter.deserialize(DefaultTypeAdapters.java:531)
    at com.google.gson.DefaultTypeAdapters$MapTypeAdapter.deserialize(DefaultTypeAdapters.java:498)
    at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:50)
    at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
    at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:98)
    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:495)
    at com.google.gson.Gson.fromJson(Gson.java:444)
    at com.google.gson.Gson.fromJson(Gson.java:396)
    at gson.test.Test.main(Test.java:24)

What version of the product are you using? On what operating system?
1.4

Please provide any additional information below.

Original issue reported on code.google.com by haoks...@gmail.com on 10 Jun 2010 at 5:52

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 3 Sep 2010 at 6:51