shau-lok / google-gson

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

error happend when converting object to json #399

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.class A declared a field age
2.class B extends A and also declared a field age 
3.new an instance of class B
4.convert the newed B instance to json
5.java.lang.IllegalArgumentException: class B declares multiple JSON fields 
named age

What is the expected output? What do you see instead?

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

Please provide any additional information below.

public class Main {
    public static void main(String[] args) {
        try {
            A a = new A();
            a.setAge(2);
            a.setName("someone");
            B b = new B();
            b.setAge(2);
            Gson gson = new Gson();
            System.out.println(gson.toJson(a));
            System.out.println(gson.toJson(b));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class A {
    String name;
    int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class B extends A {
    Date birthday;
    int age;

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Original issue reported on code.google.com by jackydu1...@gmail.com on 11 Jan 2012 at 3:50

GoogleCodeExporter commented 9 years ago
Working as intended.

If the age fields can be different, use @SerializedName() on one or more of 
them to give it an unambiguous name. If they're the same, remove the age field 
declaration on 'b'.

http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/a
nnotations/SerializedName.html

Original comment by limpbizkit on 11 Jan 2012 at 4:50

GoogleCodeExporter commented 9 years ago
What if Class A is an abstract parent class? The error still happens. I 
shouldn't have to put an annotation on an abstract class that would never be 
used with gson should I?

Original comment by ianwal...@gmail.com on 15 Aug 2012 at 11:47

GoogleCodeExporter commented 9 years ago
I just bumped into this issue. Where the parent class were abstract. And I 
cannot modify that class since I'm not the author but I need to serialize it.

Original comment by lajvbuti...@gmail.com on 10 Nov 2012 at 8:30

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.class A declared a field age
2.class B extends A and also declared a field age 
3.new an instance of class B
4.convert the newed B instance to json
5.java.lang.IllegalArgumentException: class B declares multiple JSON fields 
named age

Original comment by atsakthi...@gmail.com on 27 Mar 2013 at 12:52

GoogleCodeExporter commented 9 years ago
I have posted a question on stackoverflow with same problem. Can anybody answer 
it?
Question can be found at:

http://stackoverflow.com/questions/15756551/solr-java-error-class-com-restfb-typ
es-post-declares-multiple-json-fields-named

Original comment by ndthokar...@gmail.com on 2 Apr 2013 at 2:29