Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8462337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:08:20+00:00 2026-06-10T14:08:20+00:00

My problem is that I need to deserialize JSON to a type that in

  • 0

My problem is that I need to deserialize JSON to a type that in turn contains interface type objects, and thus am required to instruct Json.NET on how to convert that interface type, but am not in control of the Newtonsoft.Json.Deserializer directly. The deserialization is handled indirectly by framework code, i.e. by HttpContent.ReadAsAsync<T>.

How can I specify a conversion from the interface type to a concrete type, without being able to add a converter to JsonSerializer.Converters directly? If I use the [JsonConverter] attribute on the interface definition, the converter also gets called for implementations of the interface (i.e., the concrete type I’m converting to).

The below code shows a C# console program that attempts to deserialize the same JSON to first the Parent2 class and then the Parent1 class, instructing Json.NET to convert the IChild1 interface type to the Child1 concrete type via JsonConverterAttribute. The deserialization to Parent2 works since I add ChildConverter directly to serializer.Converters, whereas deserializing to Parent1 results in a stack overflow exception, due to ChildConverter.Deserialize being called even for the Child1 type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

namespace TestConsole
{
    [JsonConverter(typeof(ChildConverter))]
    interface IChild1
    {
        string Name { get; set; }
    }

    class Child1 : IChild1
    {
        public string Name { get; set; }
    }

    class Parent1
    {
        public IEnumerable<IChild1> Children { get; set; }
    }

    interface IChild2
    {
        string Name { get; set; }
    }

    class Child2 : IChild2
    {
        public string Name { get; set; }
    }

    class Parent2
    {
        public IEnumerable<IChild2> Children { get; set; }
    }

    class ChildConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(IChild1) || objectType == typeof(IChild2);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (objectType == typeof(IChild1))
                return serializer.Deserialize<Child1>(reader);
            return serializer.Deserialize<Child2>(reader);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var json = "{'Children': [{'Name': 'Child1'}, {'Name': 'Child2'}]}";

            var serializer = new JsonSerializer();

            serializer.Converters.Add(new ChildConverter());
            var obj = serializer.Deserialize(new StringReader(json), typeof(Parent2));

            serializer = new JsonSerializer();
            obj = serializer.Deserialize(new StringReader(json), typeof(Parent1));
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:08:22+00:00Added an answer on June 10, 2026 at 2:08 pm

    I found a solution to my particular problem, being to call HttpContent.ReadAsAsync<T> and have that deserialize a JSON response. While my solution doesn’t resolve the general problem of configuring Json.NET type conversion without access to the serializer, it does resolve my particular problem. What I found is that HttpContent.ReadAsync<T> accepts an IEnumerable<MediaTypeFormatter>, that is used to deserialize the response. Here one can pass a JsonMediaTypeFormatter, which has been configured with your JsonConverter of choice (in my example, ChildConverter).

    var formatter = new JsonMediaTypeFormatter();
    formatter.SerializerSettings.Converters.Add(new ChildConverter());
    var obj = await resp.Content.ReadAsAsync<T>(new MediaTypeFormatter[] {formatter});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm receiving JSON, that i need to Deserialize. I'm using JavaScriptSerializer to do so.
I need to serialize and deserialize an object that contains an System.Uri property using
I have some JSON that I need to deserialize so I'm using JavaScriptSerializer.DeserializeObject like:
I have an interesting SQL problem that I need help with. Here is the
My specific problem is that I need to execute a (potentially) large number of
I am doing an e-learning application using CakePHP. The problem is that I need
Problem I have some pages that need dynamic data from website to generate output
Problem There are data gaps that need to be filled. Would like to avoid
The problem is this: I have multiple competing threads (100+) that need to access
I bumped into this problem when I created a chart that need a axis

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.