如何使用Newtonsoft.Json将对象序列化为带有类型信息的json?

时间:2013-10-14 12:20:00

标签: c# serialization json.net

当我序列化某些类型的对象时,我希望在JSON中拥有类型名称的属性。 我写了一个转换器:

public class TypeInfoConverter : JsonConverter {
    private readonly IEnumerable<Type> _types;

    public TypeInfoConverter(IEnumerable<Type> types) {
        Contract.Requires(types != null);

        _types = types;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
        var jObject = JObject.FromObject(value, serializer);
        jObject.AddFirst(new JProperty("Type", value.GetType().Name));
        jObject.WriteTo(writer);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        return serializer.Deserialize(reader, objectType);
    }

    public override bool CanConvert(Type objectType) {
        return _types.Any(t => t.IsAssignableFrom(objectType));
    }
}

但是当我尝试序列化对象时,我在这里有一个无限递归: var jObject = JObject.FromObject(value, serializer);很明显,因为我使用了与该转换器配置的JsonSerializer相同的实例。

如何防止使用此转换器,但我想使用为此序列化器配置的其他转换器?

我要序列化的类型:

public interface ITaskResult {
}

public class UserHasRejectedOffer : ITaskResult {
    public string Message { get; set; }
}

public class UserHasFilledForm : ITaskResult {
    public string FormValue1 { get; set; }

    public string Formvalue2 { get; set; }
}

...

3 个答案:

答案 0 :(得分:49)

var jsonSerializerSettings = new JsonSerializerSettings() { 
    TypeNameHandling = TypeNameHandling.All
};
var json = JsonConvert.SerializeObject(instance, jsonSerializerSettings);

http://james.newtonking.com/json/help/index.html?topic=html/SerializationSettings.htm

答案 1 :(得分:11)

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    var converters = serializer.Converters.Where(x => !(x is TypeInfoConverter)).ToArray();

    var jObject = JObject.FromObject(value);
    jObject.AddFirst(new JProperty("Type", value.GetType().Name));
    jObject.WriteTo(writer, converters);
}

答案 2 :(得分:2)

您是否尝试过创建JsonSerializer的新实例,然后复制原始序列化程序中的所有转换器,但导致无限递归的转换器除外?

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
    JsonSerializerSettings settings = new JsonSerializerSettings
    {
        Converters = serializer.Converters.Where(s => !(s is TypeInfoConverter)).ToList()
        // also copy any other custom settings from the serializer you wish to pass through
        DateFormatHandling = serializer.DateFormatHandling,
        MissingMemberHandling = serializer.MissingMemberHandling,
        NullValueHandling = serializer.NullValueHandling,
        Formatting = serializer.Formatting
    };
    var localSerializer = JsonSerializer.Create(settings);

    var jObject = JObject.FromObject(value, localSerializer);
    jObject.AddFirst(new JProperty("Type", value.GetType().Name));
    jObject.WriteTo(writer);
}