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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:34:47+00:00 2026-06-15T23:34:47+00:00

Please Please Please could somebody help me finish implementing the code below. I followed

  • 0

Please Please Please could somebody help me finish implementing the code below. I followed the example at http://dotnetbyexample.blogspot.com/2012/02/json-deserialization-with-jsonnet-class.html This example is posted all over on lots of blogs.

I’m trying to find a somewhat reusable way to handle abstract parameters in Web API actions. It works perfectly in Fiddler, but the EntityNote returned is null when I try to call Get via HttpClient. I find that the ReadJson method is never called, so Create is also never called. I guess I do not understand the flow of Web API enough to understand where ReadJson is supposed to be called.

Global.asax.cs

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;
config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.All;
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new JsonDeviceConverter());

The converter from the link (Nobody shows how to implement WriteJson)

public abstract class JsonCreationConverter<T> : JsonConverter
{
    protected abstract T Create(Type objectType, JObject jsonObject);

    public override bool CanConvert(Type objectType)
    {
        return objectType.IsSubclassOf(typeof(T));
    }

    //This method never gets called
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jsonObject = JObject.Load(reader);
        var target = Create(objectType, jsonObject);
        serializer.Populate(jsonObject.CreateReader(), target);
        return target;
    }

    //I just kinda guessed at this code
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        if (this.CanConvert(value.GetType()))
        {
            serializer.Serialize(writer, value);
        }
    }
}

EntityNote is an abstract class with PortalProviderRefundNote as an implementation.

public class JsonDeviceConverter : JsonCreationConverter<EntityNote>
{
    //This method never gets called, because ReadJson is never called
    protected override EntityNote Create(Type objectType, JObject jsonObject)
    {
        var typeName = jsonObject["EntityNote"].ToString();
        switch (typeName)
        {
            case "PortalProviderRefundNote":
                return new PortalProviderRefundNote() { EntityId = 1 };
            default: return null;
        }
    }
}

HttpClient – the tutorial uses the JsonConvert.DeserializeObject, but I can’t figure out how that fits in here…ReadAsAsync does this task

//simplified **Suggested Edit**
this.jsonFormatter = new JsonMediaTypeFormatter();
jsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.All;
jsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
jsonFormatter.SerializerSettings.Converters.Add(new JsonDeviceConverter());

new HttpClient().GetAsync("http://localhost/api/EntityNotesController/").Result.Content.ReadAsAsync<EntityType>(new List<MediaTypeFormatter>(){ jsonFormatter }).Result;

Service Controller

public EntityNote Get(int id)
{
    return new PortalProviderRefundNote() { EntityId = 1 };
}
  • 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-15T23:34:48+00:00Added an answer on June 15, 2026 at 11:34 pm

    I switched to XML and used the KnownType attribute. This works out of the box. I’d be interested to know if there is a solution with JSON, but this works good enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, please could somebody help me find out what's wrong with my code.
Could somebody please help me get Clang up and running? (I don't have 3.2)
could somebody please take a look at this http://jsfiddle.net/bloodygeese/EzkFR/1/ My aim is to on
Could somebody please take some time to show me a quick example on how
Was wondering if somebody could please help me solve this. I've follwed all steps
Could somebody please help me understand the concept of 'impersonation'? The way I understand
Could somebody please help me setup Emacs Tramp to do a double hop? I
Could somebody please help me with explode() function. I am reading values with $_POST[]
Could somebody help me please with configuring the vhosts in XAMPP, Linuxmint. Xampp starts
Could somebody please help remove the extra pixels under the orange box on the

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.