1

I want to convert a sqlResult mapped to a very complex Object to JSON in order to save it to a redis database a value. Now I'm a getting Error

java.lang.IllegalArgumentException: class 'xx' declares multiple JSON fields named 'XX'

How can I solve this problem without chaging the classes as mentioned in the error 'xx'?
Or are other libs avaiable, that are supporting converting object to and from JSON with supporting of multiple JSON fields names e.g. json-io?


I updated my project with the following suggestedd class class A declares multiple JSON fields in order to avoid multiple JSON fields.

But now I have got a another problem
nested exception is: java.lang.StackOverflowError Any suggestions for that problem? Because I am using a very large collection/object for the convertion.

Community
  • 1
  • 1
imalik8088
  • 1,501
  • 5
  • 21
  • 39

1 Answers1

15

You didn't post a very detailed question so I hope this will help you a bit:

A problem you can have is that the field already exists in a Class that you extend. In this case the field would already exist in Class B.

Say:

public class A extends B {
    private BigDecimal netAmountTcy;
    private BigDecimal netAmountPcy;   
    private BigDecimal priceTo;  
    private String segment;

    private BigDecimal taxAmountTcy;
    private BigDecimal taxAmountPcy;   
    private BigDecimal tradeFeesTcy;
    private BigDecimal tradeFeesPcy;

// getter and setter for the above fields

}

where class B is something like (and maybe more duplicates of course):

public class B {
    private BigDecimal netAmountPcy;   
// getter and setter for the above fields

}

Just remove the field "netAmountPcy" Class A and you will still have the field (because it extends the class).

Bart Burg
  • 4,786
  • 7
  • 52
  • 87
  • 1
    thanks! The intial problem has been solved with http://stackoverflow.com/questions/16476513/class-a-declares-multiple-json-fields with multiple fields and the other Stackoverflow could been solved with json-io an another lib for java/json. – imalik8088 Aug 20 '14 at 13:48
  • As I said, the question is too broad, but this could be the problem. Doesn't have to be – Bart Burg Mar 21 '16 at 12:18
  • 1
    Thanks! So simple! I lost an hour trying to solve this problem. In my case a variable was with a different type in classes A and B. But I deleted it in a superclass. – CoolMind Jul 25 '16 at 07:46
  • @CoolMind how do/did you handle it if there is a two different types but they both have the same name in the JSON? – Ben Akin Jun 01 '20 at 05:20
  • @BenAkin, a good question. Currently I use Kotlin and it is hardly possible. Maybe in that year I had a superclass C without a variable `name`, `class A extends C` with the variable `type1 name`, `class B extends C` with the variable `type2 name`. – CoolMind Jun 01 '20 at 06:18