0

I have a web api written in aspnet webapi. The main idea is to work with inherirance. What you see here is a test project to play arround before adding this to the real project. To do that on the webconfig file i have added:

config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;

Here is the model

 public abstract class Person
 {
   public string Name { get; set; }
   public string DocumentNumber { get; set; }
 }

 public class Teacher : Person
 {
    public string School { get; set; }
 }
 public class Employee : Person
 {
   public string PersonnelNumber { get; set; }
 }

As a result, here is a response from the server:

[
    {
        "$type": "JsonConverterTest.Models.Employee, JsonConverterTest",
        "PersonnelNumber": "1001",
        "Name": "Emp1",
        "DocumentNumber": "01"
    },
    {
        "$type": "JsonConverterTest.Models.Employee, JsonConverterTest",
        "PersonnelNumber": "1002",
        "Name": "Emp2",
        "DocumentNumber": "02"
    },
    {
        "$type": "JsonConverterTest.Models.Teacher, JsonConverterTest",
        "Name": "Teacher1",
        "DocumentNumber": "10"
    }
]

I need $type for web api deserization process to create the right instance for person.

What can i do to change the $type value to a more simple one like: "Models.Employee" or "Models.Teacher" and get rid of the assembly name?

Thanks in advance.

Müsli
  • 1,744
  • 7
  • 27
  • 50
  • Why do you need a simpler `$type` value? Doesn't it work correctly the way it is? – Brian Rogers May 12 '14 at 15:35
  • hi! yes it works this way. but i feel that it should be simplier, and also i'm exposing the assembly name. That part i do not like – Müsli May 13 '14 at 22:21
  • You can use JsonConverters see http://stackoverflow.com/questions/29810004/using-custom-jsonconverter-and-typenamehandling-in-json-net – Damien McGivern Feb 12 '16 at 12:36

0 Answers0