Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error happend when converting object to json #399

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 9 comments
Closed

error happend when converting object to json #399

GoogleCodeExporter opened this issue Mar 19, 2015 · 9 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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

@GoogleCodeExporter
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

@wxi
Copy link

wxi commented Feb 5, 2016

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?

@JakeWharton
Copy link
Contributor

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.

@wxi
Copy link

wxi commented Feb 5, 2016

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.

@JakeWharton
Copy link
Contributor

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 notifications@github.com 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
#399 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants