1

I actually try to deserialize the next object in a unit test in accordance with Patreon

string json = @"{
            ""data"": {
                ""attributes"": {
                    ""about"": """",
                    ""created"": ""2017-04-01T12:00:00+00:00"",
                    ""discord_id"": null,
                    ""email"": ""james@mymail.com"",
                    ""facebook"": null,
                    ""facebook_id"": null,
                    ""first_name"": ""Firstname"",
                    ""full_name"": ""Fullname"",
                    ""gender"": 0,
                    ""has_password"": true,
                    ""image_url"": ""https://c3.patreon.com/2/patreon-user/abc.jpg"",
                    ""is_deleted"": false,
                    ""is_email_verified"": true,
                    ""is_nuked"": false,
                    ""is_suspended"": false,
                    ""last_name"": """",
                    ""social_connections"": {
                        ""deviantart"": null,
                        ""discord"": null,
                        ""facebook"": null,
                        ""spotify"": null,
                        ""twitch"": null,
                        ""twitter"": null,
                        ""youtube"": null
                    },
                    ""thumb_url"": ""https://c3.patreon.com/2/patreon-user/abc.jpg"",
                    ""twitch"": null,
                    ""twitter"": null,
                    ""url"": ""https://www.patreon.com/user?u=123456"",
                    ""vanity"": null,
                    ""youtube"": null
                },
                ""id"": ""123456"",
                ""relationships"": {
                    ""pledges"": {
                        ""data"": []
                    }
                },
                ""type"": ""user""
            },
            ""links"": {
                ""self"": ""https://api.patreon.com/user/123456""
            }
            }";

But my attributes object or array is polymorphic, it can be User like my exemple but other object types, and I don't know how to do.
I tried something like that but without success.

And second question, how is it possible to flattern attributes in same user class with id and relationships.

EDIT: To anwser to my question, I found a better solution than "Duplicate" : http://jsonapi.org/

Arnaud
  • 84
  • 1
  • 10

1 Answers1

3

Nope, not gonna work.

The Newtonsoft.JSON package has options to include type names when you serialize an object, so deserializing works correctly with polymorphism. But with your plain JSON, the deserializer has no way of knowing which class to instantiate inside your array.

Xavier J
  • 4,326
  • 1
  • 14
  • 25