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 8454433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:01:48+00:00 2026-06-10T12:01:48+00:00

I am consuming a RESTful Web service sending JSON, that I try to deserialize

  • 0

I am consuming a RESTful Web service sending JSON, that I try to deserialize using HttpContent.ReadAsAsync<T>. The type I try to deserialize to declares a property that returns an IEnumerable containing an interface type. This code snippet demonstrates the kind of type I’m trying to deserialize to:

public class Data
{
    public IEnumerable<IChild> Children { get; set; };
}

The problem is that Newtonsoft.Json, underlying HttpContent.ReadAsAsync<T> doesn’t understand how to deserialize objects of type IChild, the latter being an interface. How can I specify to Newtonsoft.Json how to deserialize IChild to a concrete type?

  • 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-10T12:01:50+00:00Added an answer on June 10, 2026 at 12:01 pm

    You can use a custom Converter to tell JSON.NET how to deserialize types of that interface. The code below shows an example.

    public class StackOverflow_12197892
    {
        public class Data
        {
            public IEnumerable<IChild> Children { get; set; }
    
            public override string ToString()
            {
                return string.Format("Data{{Children=[{0}]}}",
                    string.Join(", ", Children.Select(c => string.Format("{0}/{1}", c.Name, c.IsFemale ? "girl" : "boy"))));
            }
        }
        public interface IChild
        {
            string Name { get; }
            bool IsFemale { get; }
        }
        public class Son : IChild
        {
            public Son(string name)
            {
                this.Name = name;
            }
    
            public string Name { get; private set; }
            public bool IsFemale { get { return false; } }
        }
        public class Daughter : IChild
        {
            public Daughter(string name)
            {
                this.Name = name;
            }
    
            public string Name { get; private set; }
            public bool IsFemale { get { return true; } }
        }
        class ChildConverter : Newtonsoft.Json.JsonConverter
        {
            public override bool CanConvert(Type objectType)
            {
                return typeof(IChild).IsAssignableFrom(objectType);
            }
    
            public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
            {
                JObject obj = serializer.Deserialize<JToken>(reader) as JObject;
                if (obj != null)
                {
                    bool isFemale = obj["isFemale"].ToObject<bool>();
                    string name = obj["name"].ToObject<string>();
                    if (isFemale)
                    {
                        return new Daughter(name);
                    }
                    else
                    {
                        return new Son(name);
                    }
                }
                else
                {
                    return null;
                }
            }
    
            public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
            {
                throw new NotImplementedException();
            }
        }
        public static void Test()
        {
            Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
            serializer.Converters.Add(new ChildConverter());
            string json = "{'Children':[{'name':'John',isFemale:false},{'name':'Mary',isFemale:true}]}".Replace('\'', '\"');
            var obj = serializer.Deserialize(new StringReader(json), typeof(Data));
            Console.WriteLine(obj);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm consuming a web service using a stand-alone VBScript. The web service returns to
For my iPhone app, I'm consuming a RESTful service and getting JSON. I've found
I am consuming a SOAP web service using php5's soap extension. The service' wsdl
I'm trying to consuming a client's web service using WCF. The client's web service
We are consuming a web service that returns a fax document in the form
I'm consuming a web service that returns an array of objects called $viewData like
I am using Cherrypy in a RESTful web service and server returns XML as
I have a WCF restful service that I'm trying to upload an image to.
I'm consuming a JSON WebService by using the WebClient.DOwnloadStringAsync. The returning string contains some
I am consuming a webservice using my java web application.This returns a number of

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.