question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error happend when converting object to json

See original GitHub issue
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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
wxicommented, 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?

0reactions
JakeWhartoncommented, Feb 5, 2016

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 https://github.com/google/gson/issues/399#issuecomment-180431956.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stackoverflow error occuring when converting Object list to ...
I have a Java RESTapi, where I want to convert a list of my custom Pet object into Json, and display it in...
Read more >
JSON methods, toJSON - The Modern JavaScript Tutorial
Let's say we have a complex object, and we'd like to convert it into a string, to send it over a network, or...
Read more >
What is TypeError: Converting circular structure to JSON?
TypeError : Converting circular structure to JSON occurs when you try to reference your variable name within the JSON object.
Read more >
"Error occured while parsing Json string to map" on ...
"Error occured while parsing Json string to map" on Powershell Steps as JSON Val Encoding is not applied - Serialization of Array.
Read more >
Mule 4 : How to convert a JSON response to String?
In DW when you do `payload as String` you are saying: `transform this Object into a String` which is an invalid thing, in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found