question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intercept "type" before deserializing

See original GitHub issue

Hi, First off thanks for creating this library it works great!. Recently came across a requirement where a resource could be of any type and we need to deserialize into concrete type based on “type” property

For example we have sample json as

  "data": {
    "type": "products",
    "id": "123456",
    "attributes": {},
    "relationships": {
      "content": {
        "links": {
          "self": "/v1/products/123456/relationships/content",
          "related": "/v1/products/123456/content"
        },
        "data": {
          "type": "contentA",
          "id": "999999"
        }
      }
    }
  }
}

content.data.type could be “contentA”, “contentB” and so on.

And have defined below classes to deserialize into

using System.IO;
using System.Reflection;
using JsonApiSerializer;
using JsonApiSerializer.JsonApi;
using Newtonsoft.Json;

namespace MyJsonApiSerializer
{
    interface IContent
    {
        string Type { get; }
        string Id { get; }
    }

    class ContentA : IContent
    {
        public string Type { get; } = "contentA";
        public string Id { get; set; }
    }

    class ContentB : IContent
    {
        public string Type { get; } = "contentB";
        public string Id { get; set; }
    }

    class Product
    {
        public string Type { get; } = "products";
        public string Id { get; set; }

        [JsonProperty("content")] public Relationship<IContent> Content { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var thisAssembly = Assembly.GetExecutingAssembly();
            using (var stream = thisAssembly.GetManifestResourceStream("MyJsonApiSerializer.product.json"))
            {
                using (var reader = new StreamReader(stream))
                {
                    var json = reader.ReadToEnd();
                    var product = JsonConvert.DeserializeObject<Product>(json, new JsonApiSerializerSettings());
                }
            }
        }
    }
}

What we need is to deserialize the json into Product class but Content propery should get deserialized into Relationship<ContentA> or Relationship<ContentB> as per “type”. Don’t think JsonApiSerializer is smart enough to infer the type and deserialize accordingly but any help on how to extend the current behavior to support this would be appreciated.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

1reaction
alex-daviescommented, Jul 15, 2018

Thanks for your help in implementing this. I have made a few tweaks to the PR and the feature has been added to 1.4.0

Feel free to reopen issue if you are having issues

0reactions
alastairtreecommented, Jul 16, 2018

Looks great, thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Intercept a data-type before deserialization, when it ...
We need a mechanism so that when ever a particular data-type (say date-time) reaches Web API, we should be able intercept it &...
Read more >
Avoiding deserialization
In our app we need to accept a (large) Grpc message, extract a field, and then based on the value of that field...
Read more >
Getting Started with Custom Deserialization in Jackson
This quick tutorial will illustrate how to use Jackson 2 to deserialize JSON using a custom Deserializer.
Read more >
jonpeterson/jackson-module-json-interceptor
Jackson 2.x module for intercepting and manipulating raw JSON before deserialization and after serialization. Compatibility. Compiled for Java 6 and tested with ...
Read more >
Serialization Guide
The Json.NET serializer can serialize a wide variety of .NET objects. This guide looks at how it works, first at a high level...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found