Gson: error happend when converting object to json

Created on 19 Mar 2015  Â·  9Comments  Â·  Source: google/gson

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 [email protected] on 11 Jan 2012 at 3:50

Most helpful comment

I recently got below error:
ERROR:class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits

The reason is I used "private final java.text.DecimalFormat" in a class which is deserialized by Gson. And DecimalFormat and its base class NumberFormat, both define "private int maximumIntegerDigits".
I fixed the problem by removing the field.

But my point is, as gson knows duplicate fields, if they are same type, can gson just deserialize the value to subclass instead of throwing an exception?

All 9 comments

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

  • Changed state: Invalid
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 [email protected] on 15 Aug 2012 at 11:47

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 [email protected] on 10 Nov 2012 at 8:30

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 [email protected] on 27 Mar 2013 at 12:52

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 [email protected] on 2 Apr 2013 at 2:29

I recently got below error:
ERROR:class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits

The reason is I used "private final java.text.DecimalFormat" in a class which is deserialized by Gson. And DecimalFormat and its base class NumberFormat, both define "private int maximumIntegerDigits".
I fixed the problem by removing the field.

But my point is, as gson knows duplicate fields, if they are same type, can gson just deserialize the value to subclass instead of throwing an exception?

That's only half the problem. What if they have different values when serializing?

You should not be relying on the implementation details of java.* types anyway. Write a type adapter for DecimalFormat which calls toPattern() to serialize as a String and uses the DecimalFormat(String) constructor to deserialize from a String.

Agree. @JakeWharton. Not a good way to have duplicate fields.

But the condition is if we cannot modify those classes and we have to use them, it's better that gson provides a work around.

I just told you the workaround: a type adapter which doesn't serialize
implementation details of classes outside your control. Use their public
APIs to encode their data.

This is less of a workaround and more just the correct thing to do.

On Fri, Feb 5, 2016 at 11:39 AM William [email protected] wrote:

Agree. @JakeWharton https://github.com/JakeWharton. Not a good way to
have duplicate fields.

But the condition is if we cannot modify those classes and we have to use
them, it's better that gson provides a work around.

—
Reply to this email directly or view it on GitHub
https://github.com/google/gson/issues/399#issuecomment-180431956.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  Â·  19Comments

RobMans426 picture RobMans426  Â·  20Comments

GoogleCodeExporter picture GoogleCodeExporter  Â·  15Comments

GoogleCodeExporter picture GoogleCodeExporter  Â·  25Comments

GoogleCodeExporter picture GoogleCodeExporter  Â·  20Comments