使用JSON.NET序列化/反序列化动态属性名称

uwopmtnx  于 2022-12-05  发布在  .NET
关注(0)|答案(4)|浏览(173)

我有以下几类:

public class MyRequest
{
    public string Type {get;set;}
    public string Source {get;set;}
}

我想从名为Type的JSON字段序列化/反序列化Source的值,例如:

{
    "type": "bank",
    "bank": "Some value"
}

{
    "type": "card",
    "card": "Some value"
}

其中两者都系结至Source属性。

tjjdgumg

tjjdgumg1#

您可以建立自订JsonConverter来行程动态属性名称:

public class MyRequestConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(MyRequest);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);
        string type = (string)jo["type"];
        MyRequest req = new MyRequest
        {
            Type = type,
            Source = (string)jo[type ?? ""]
        };
        return req;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        MyRequest req = (MyRequest)value;
        JObject jo = new JObject(
            new JProperty("type", req.Type),
            new JProperty(req.Type, req.Source));
        jo.WriteTo(writer);
    }
}

若要使用转换器,请将[JsonConverter]属性加入至您的类别,如下所示:

[JsonConverter(typeof(MyRequestConverter))]
public class MyRequest
{
    public string Type { get; set; }
    public string Source { get; set; }
}

下面是一个有效的往返演示:https://dotnetfiddle.net/o7NDTV

uujelgoq

uujelgoq2#

我会编写自定义序列化/反序列化方法
第一个

rxztt3cl

rxztt3cl3#

我最近遇到了这样的问题,我需要使用一个有动态数据合约的API,所以我开发了一个名为SerializationInterceptor的包。https://github.com/essencebit/SerializationInterceptor/wiki。您也可以使用Nuget软件包管理器安装软件包。
下面的示例使用Newtonsoft.Json进行序列化/反序列化。当然,您可以使用任何其他工具,因为此包不依赖于任何工具。
您可以创建一个拦截器:

public class JsonPropertyInterceptorAttribute : SerializationInterceptor.Attributes.InterceptorAttribute
{
    public JsonPropertyInterceptorAttribute(string interceptorId)
        : base(interceptorId, typeof(JsonPropertyAttribute))
    {
    }

    protected override SerializationInterceptor.Attributes.AttributeBuilderParams Intercept(SerializationInterceptor.Attributes.AttributeBuilderParams originalAttributeBuilderParams)
    {
        object value;
        switch (InterceptorId)
        {
            case "some id":
                // For DESERIALIZATION you first need to deserialize the object here having the prop Source unmapped(we'll invoke the proper deserialization later to have Source prop mapped to the correct Json key),
                // then get the value of the prop Type and assign it to variable from below.
                // For SERIALIZATION you need somehow to have here access to the object you want to serialize and get
                // the value of the Type prop and assign it to variable from below.
                value = "the value of Type prop";
                break;
            default:
                return originalAttributeBuilderParams;
        }
        originalAttributeBuilderParams.ConstructorArgs = new[] { value };
        return originalAttributeBuilderParams;
    }
}

然后将拦截器放置在Source prop 上:

public class MyRequest
{
    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonPropertyInterceptor("some id")]
    [JsonProperty("source")]
    public string Source { get; set; }
}

然后调用正确的序列化/反序列化,如下所示:

var serializedObj = SerializationInterceptor.Interceptor.InterceptSerialization(obj, objType, (o, t) =>
{
    var serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
    using var stream = new MemoryStream();
    using var streamWriter = new StreamWriter(stream);
    using var jsonTextWriter = new JsonTextWriter(streamWriter);
    serializer.Serialize(jsonTextWriter, o, t);
    jsonTextWriter.Flush();
    return Encoding.Default.GetString(stream.ToArray());
})

var deserializedObj = SerializationInterceptor.Interceptor.InterceptDeserialization(@string, objType, (s, t) =>
{
    var serializer = new JsonSerializer();
    using var streamReader = new StreamReader(s);
    using var jsonTextReader = new JsonTextReader(streamReader);
    return serializer.Deserialize(jsonTextReader, t);
});
aemubtdh

aemubtdh4#

我的解决办法是:首先创建APIResultModel类:

public class APIResultModel<T> where T: APIModel, new()
{

    public string ImmutableProperty { get; set; }

    public T Result { get; set; }

    public APIResultModel<T> Deserialize(string json)
    {
        var jObj = JObject.Parse(json);
        T t = new T();
        var result = jObj[t.TypeName()];
        jObj.Remove(t.TypeName());
        jObj["Result"] = result;
        return jObj.ToObject<APIResultModel<T>>();
    }
}

其次创建APIModel抽象类:

public abstract class APIModel
{
    public abstract string TypeName();
}

第三个创建动态内容Model类:

public class MyContentModel: APIModel
{
    public string Property {get; set;}
    public override string TypeName()
    {
        return "JsonKey";
    }
}

需要反序列化json字符串时:

var jsonModel = new APIResultModel<MyContentModel>();
jsonModel = jsonModel.Deserialize(json);
MyContentModel dynimacModel = jsonModel.Result;

反序列化函数来自@Eser

相关问题