Intercept "type" before deserializing
See original GitHub issueHi, 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:
- Created 5 years ago
- Reactions:2
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Looks great, thank you.