error happend when converting object to json
See original GitHub issueWhat 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:
- Created 9 years ago
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
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: