Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have such structure of object:

class A {
    List<B> bees;
}

class B {
    String с;
}

I'm using Gson parser which serializes such object into this string:

{"a":{"bees":[{"с":"text"}]}}

(with adding a root element "a")

API's format is a little bit different:

{"a":{"bees":[{"b":{"с":"text"}}]}}

I need to be able to parse such strings into A objects correctly.

By default B object (as a part of A) becomes not null, but empty (all fields are null) which is understandable, cause parser doesn't find any field "b" in it (when it is actually a class name).

I'm looking for a general solution for that, I have a lot of such complex objects and I don't want to implement many custom deserializers for each of them.

Gson is not obligatory, I can use another lib if it's necessary.

Thanks.

share|improve this question
add comment

1 Answer

If it is possible for you to change the class structure then change the class structure so as to match the API's format. For example,

class A {
    List<B> bees;
}

class B {
    MyTypeObjB B;
}
class MyTypeObjB {
    String c;
}

EDIT As per the comments, you can also try to customize deserialize method by implementing JsonDeserializer interface on your custom class.
You can find detail information how it can be performed on,

share|improve this answer
 
Well, I was thinking about it too. But it will increase number of classes in twice. Ideally I want to make it without any changes in bean classes structure (or with annotations or some extra field), to make it with some specific parser changes. –  a.toropov 2 days ago
 
@a.toropov refer the edited answer and use custom desrializer if that can work and you can alter according to your need... –  dbw 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.