将具有基本类型的列表序列化为派生类型json

时间:2012-05-16 08:53:45

标签: c# .net json json.net asp.net-web-api

我有一个Web API控制器操作,其参数类型为CommandMessage。该类型有一个基类型列表,CommandBase。我的问题是我不能使它们成为派生类型,在这个例子中是MyCommand。

我正在使用Web API contrib项目中的JsonNetFormatter。它使用Json.Net作为序列化器。

Web API控制器:

public class CommandController : ApiController
{
    public HttpResponseMessage Post(CommandMessage command)
    {
        //command.Commands[0].GetType().Name is always "CommandBase" instead of "MyCommand"
        var message = new HttpResponseMessage(HttpStatusCode.OK)
                          {
                              Content = new StringContent(string.Format("Type of command 1 is '{0}'", command.Commands[0].GetType().FullName))
                          };
        return message;
    }
}

这是我的要求:

private void When()
    {
        using (client)
        {
            command = new MyCommand
                          {
                              CommandId = Guid.NewGuid(),
                              Id = Guid.NewGuid(),
                              Name = "Martin"
                          };

            var message = new CommandMessage(Guid.NewGuid(),new List<CommandBase> {command});

            var requestMessage = GetHttpRequestMessage(message);
            var task = client.PostAsync("http://localhost.:16218/api/command", requestMessage.Content);
            task.Wait();
            responseMessage = task.Result;
        }
    }

private static HttpRequestMessage GetHttpRequestMessage<T>(T data)
    {
        var mediaType = new MediaTypeHeaderValue("application/json");
        var jsonFormatter = new JsonNetFormatter(DefaultJsonSettings.Settings());

        var requestMessage = new HttpRequestMessage<T>(data, mediaType, new[] {jsonFormatter});

        return requestMessage;
    }

和JSON一样:

{
  "$type": "PostToBaseClassList.Models.CommandMessage, PostToBaseClassList",
  "Commands": [
    {
      "$type": "PostToBaseClassList.Models.MyCommand, PostToBaseClassList",
      "Id": "45923a41-0c15-46e3-907d-64dd06840539",
      "Name": "Martin"
    }
  ],
  "MessageId": "c873970a-8621-4223-806e-b809039438ab"
}

来自API控制器操作的返回消息是:

  

命令类型1是'PostToBaseClassList.Models.CommandBase'

命令类:

 [DataContract]
[XmlRoot]
public class MyCommand : CommandBase
{
    private readonly string name;

    public MyCommand()
    {
    }

    public MyCommand(Guid id, string name)
    {
        this.name = name;
        Id = id;
    }

    [DataMember(Order = 1)]
    [XmlElement]
    public Guid Id { get; set; }

    [DataMember(Order = 2)]
    [XmlElement]
    public string Name { get; set; }
}

 [DataContract]
[KnownType("GetKnownTypes")]
public class CommandBase
{
    public Guid CommandId { get; set; }

    public static Type[] GetKnownTypes()
    {
        var query =
            from type in typeof (CommandBase).Assembly.GetTypes()
            where typeof (CommandBase).IsAssignableFrom(type)
            select type;

        var types = query.ToArray();
        return types;
    }
}

[DataContract]
[Serializable]
[XmlRoot]
public class CommandMessage
{
    public CommandMessage()
    {
    }

    public CommandMessage(Guid messageId, IEnumerable<CommandBase> commands)
    {
        MessageId = messageId;
        Commands = new List<CommandBase>(commands);
    }

    [DataMember]
    [XmlElement]
    public List<CommandBase> Commands { get; set; }

    [DataMember]
    [XmlElement]
    public Guid MessageId { get; set; }
}

Json.Net设置:

//DefaultJsonSettings.Settings()
var settings = new JsonSerializerSettings
                           {
                               NullValueHandling = NullValueHandling.Ignore,
                               TypeNameHandling = TypeNameHandling.Objects,
                               Converters = new List<JsonConverter>
                                                {
                                                    new IsoDateTimeConverter
                                                        ()
                                                }
                           };

private static void RegisterFormatters(HttpConfiguration config)
    {
        var jsonNetFormatter = new JsonNetFormatter(DefaultJsonSettings.Settings());
        config.Formatters.Insert(0, jsonNetFormatter);
    }

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

Json.NET不尊重KnownTypes属性,您必须提供自己的转换器。

此问题为此问题提供了一些不同的解决方案: Using Json.NET converters to deserialize properties

答案 1 :(得分:0)

将此行添加到RegisterFormatters()

后,它才起作用
config.Formatters.Remove(config.Formatters.JsonFormatter);

这还不够

config.Formatters.Insert(0, jsonNetFormatter);

完整版

private void RegisterFormatters()
{
    config.Formatters.Remove(config.Formatters.JsonFormatter);
    var jsonNetFormatter = new JsonNetFormatter(DefaultJsonSettings.Settings());
    config.Formatters.Insert(0, jsonNetFormatter);
}