Skip to content

[Question] Deserialize data with mixed mode attribute #286

@mark-b2c2

Description

@mark-b2c2

Hi,
I need to deserialize data from an external provider where I have no control of the format and one of the attributes can be an array or an object.

Sample data:

{"type":"snapshot","data": [{"name": "John"},{"name":"Jane"}]}

and

{"type":"update","data": {"name": "John"}}

I have tried creating a POJO class with constructors supporting both object and array format, i.e. Data and Data[]
I have tried creating variations using a mix of constructors and setters.
I started to look at mixins but I didn't get very far as documentation and sample code referring to mixins is minimal.

I have created many converters to serialize/deserialize many custom data types like timestamps for example. Would a custom converter or more specifically a custom JsonReader.ReadObject be required?

Is it supported using dsl_json in any way?

More generally, I have often wanted to optionally deserialize data based on the presence of an attribute and have never managed to figure out if it was possible.

As always, thanks for all your effort in a great library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions