簡體   English   中英

如何在 .net 核心 3.0 中使用 System.Text.Json 反序列化 json 的一部分?

[英]How to deserialize part of json using System.Text.Json in .net core 3.0?

我有一個 json 從這里https://api.nasa.gov/insight_weather/?api_key=DEMO_KEY&feedtype=json&ver=1.0看起來像:

{
  "782": {
    "First_UTC": "2021-02-06T17:08:11Z",
    "Last_UTC": "2021-02-07T17:47:46Z",
    "Month_ordinal": 12,
    "Northern_season": "late winter",
    "PRE": {
      "av": 721.77,
      "ct": 113450,
      "mn": 698.8193,
      "mx": 742.2686
    },
    "Season": "winter",
    "Southern_season": "late summer",
    "WD": {
      "most_common": null
    }
  },
  "783": {
    "First_UTC": "2021-02-07T17:47:46Z",
    "Last_UTC": "2021-02-08T18:27:22Z",
    "Month_ordinal": 12,
    "Northern_season": "late winter",
    "PRE": {
      "av": 722.186,
      "ct": 107270,
      "mn": 698.7664,
      "mx": 743.1983
    },
    "Season": "winter",
    "Southern_season": "late summer",
    "WD": {
      "most_common": null
    }
  },
  "sol_keys": [ "782", "783" ],
  "validity_checks": { /* Some complex object */ }
}

我只需要部分信息,所以我創建了以下類:

public class MarsWheather {
    [JsonPropertyName("First_UTC")]
    public DateTime FirstUTC { get; set; }
    [JsonPropertyName("Last_UTC")]
    public DateTime LastUTC { get; set; }
    [JsonPropertyName("Season")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public Season MarsSeason { get; set; }
    [JsonPropertyName("PRE")]
    public DataDescription AtmosphericPressure { get; set; }
}

public enum Season {
    winter,
    spring,
    summer,
    autumn
}

public class DataDescription{
    [JsonPropertyName("av")]
    public double Average { get; set; }
    [JsonPropertyName("ct")]
    public double TotalCount { get; set; }
    [JsonPropertyName("mn")]
    public double Minimum { get; set; }
    [JsonPropertyName("mx")]
    public double Maximum { get; set; }
} 

問題是來自 NASA 的 JSON 根 object 包含我不需要並且想要跳過的屬性"validity_checks""sol_keys" 在 Newton.Json 我使用JObject.Parse來做到這一點,但在System.Text.Json我想使用

JsonSerializer.DeserializeAsync<Dictionary<string, MarsWheather>>(stream, new JsonSerializerOptions { IgnoreNullValues = true });

不幸的是,當我這樣做時,我得到了一個例外:

 System.Text.Json.JsonException: The JSON value could not be converted to MarsWheather. Path: $.sol_keys | LineNumber: 120 | BytePositionInLine: 15.

演示小提琴在這里

可能嗎?

您的 JSON 根 object 由某些固定鍵( "sol_keys""validity_checks" )組成,它們的值每個都有一些固定模式,以及任意數量的變量鍵( "782"數字鍵),它們的值都共享一個不同於固定鍵值的模式:

{
  "782": {
    // Properties corresponding to your MarsWheather object
  },
  "783": {
    // Properties corresponding to your MarsWheather object
  },
  // Other variable numeric key/value pairs corresponding to KeyValuePair<string, MarsWheather>
  "sol_keys": [
    // Some array values you don't care about
  ],
  "validity_checks": {
    // Some object you don't care about
  }
}

您只想反序列化變量鍵,但是當您嘗試反序列化為Dictionary<string, MarsWheather>時會出現異常,因為序列化程序會嘗試反序列化固定鍵值,就好像它是變量鍵值一樣 - 但由於固定鍵具有數組值,而可變鍵具有 object 值,則會引發異常。 如何告訴System.Text.Json跳過已知的固定鍵而不是嘗試反序列化它們?

如果您只想反序列化變量鍵並跳過固定的已知鍵,則需要創建一個自定義JsonConverter 最簡單的方法是首先為您的字典創建一些根 object:

[JsonConverter(typeof(MarsWheatherRootObjectConverter))]
public class MarsWheatherRootObject
{
    public Dictionary<string, MarsWheather> MarsWheathers { get; } = new Dictionary<string, MarsWheather>();
}

然后為它定義如下轉換器:

public class MarsWheatherRootObjectConverter : FixedAndvariablePropertyNameObjectConverter<MarsWheatherRootObject, Dictionary<string, MarsWheather>, MarsWheather>
{
    static readonly Dictionary<string, ReadFixedKeyMethod> FixedKeyReadMethods = new Dictionary<string, ReadFixedKeyMethod>(StringComparer.OrdinalIgnoreCase)
    {
        { "sol_keys", (ref Utf8JsonReader reader, MarsWheatherRootObject obj, string name, JsonSerializerOptions options) => reader.Skip() },
        { "validity_checks", (ref Utf8JsonReader reader, MarsWheatherRootObject obj, string name, JsonSerializerOptions options) => reader.Skip() },
    };

    protected override Dictionary<string, MarsWheather> GetDictionary(MarsWheatherRootObject obj) => obj.MarsWheathers;
    protected override void SetDictionary(MarsWheatherRootObject obj, Dictionary<string, MarsWheather> dictionary) => throw new RowNotInTableException();
    protected override bool TryGetFixedKeyReadMethod(string name, JsonSerializerOptions options, out ReadFixedKeyMethod method) => FixedKeyReadMethods.TryGetValue(name, out method);
    protected override IEnumerable<KeyValuePair<string, WriteFixedKeyMethod>> GetFixedKeyWriteMethods(JsonSerializerOptions options) => Enumerable.Empty<KeyValuePair<string, WriteFixedKeyMethod>>();
}

public abstract class FixedAndvariablePropertyNameObjectConverter<TObject, TDictionary, TValue> : JsonConverter<TObject> 
    where TDictionary : class, IDictionary<string, TValue>, new()
    where TObject : new()
{
    protected delegate void ReadFixedKeyMethod(ref Utf8JsonReader reader, TObject obj, string name, JsonSerializerOptions options);
    protected delegate void WriteFixedKeyMethod(Utf8JsonWriter writer, TObject value, JsonSerializerOptions options);
        
    protected abstract TDictionary GetDictionary(TObject obj);
    protected abstract void SetDictionary(TObject obj, TDictionary dictionary);
    protected abstract bool TryGetFixedKeyReadMethod(string name, JsonSerializerOptions options, out ReadFixedKeyMethod method);
    protected abstract IEnumerable<KeyValuePair<string, WriteFixedKeyMethod>> GetFixedKeyWriteMethods(JsonSerializerOptions options);
        
    public override TObject Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.Null)
            return (typeToConvert.IsValueType && Nullable.GetUnderlyingType(typeToConvert) == null)
                ? throw new JsonException(string.Format("Unepected token {0}", reader.TokenType))
                : default(TObject);
        if (reader.TokenType != JsonTokenType.StartObject)
            throw new JsonException(string.Format("Unepected token {0}", reader.TokenType));
        var obj = new TObject();
        var dictionary = GetDictionary(obj);
        var valueConverter = (typeof(TValue) == typeof(object) ? null : (JsonConverter<TValue>)options.GetConverter(typeof(TValue))); // Encountered a bug using the builtin ObjectConverter
        while (reader.Read())
        {
            if (reader.TokenType == JsonTokenType.PropertyName)
            {
                var name = reader.GetString();
                reader.ReadAndAssert();
                if (TryGetFixedKeyReadMethod(name, options, out var method))
                {
                    method(ref reader, obj, name, options);
                }
                else
                {
                    if (dictionary == null)
                        SetDictionary(obj, dictionary = new TDictionary());
                    dictionary.Add(name, valueConverter.ReadOrDeserialize(ref reader, typeof(TValue), options));
                }
            }
            else if (reader.TokenType == JsonTokenType.EndObject)
            {
                return obj;
            }
            else
            {
                throw new JsonException(string.Format("Unepected token {0}", reader.TokenType));
            }
        }
        throw new JsonException(); // Truncated file
    }

    public override void Write(Utf8JsonWriter writer, TObject value, JsonSerializerOptions options)
    {
        writer.WriteStartObject();
        var dictionary = GetDictionary(value);
        if (dictionary != null)
        {
            var valueConverter = (typeof(TValue) == typeof(object) ? null : (JsonConverter<TValue>)options.GetConverter(typeof(TValue))); // Encountered a bug using the builtin ObjectConverter
            foreach (var pair in dictionary)
            {
                // TODO: handle DictionaryKeyPolicy 
                writer.WritePropertyName(pair.Key);
                valueConverter.WriteOrSerialize(writer, pair.Value, typeof(TValue), options);
            }
        }
        foreach (var pair in GetFixedKeyWriteMethods(options))
        {
            writer.WritePropertyName(pair.Key);
            pair.Value(writer, value, options);
        }
        writer.WriteEndObject();
    }
}

public static partial class JsonExtensions
{
    public static void WriteOrSerialize<T>(this JsonConverter<T> converter, Utf8JsonWriter writer, T value, Type type, JsonSerializerOptions options)
    {
        if (converter != null)
            converter.Write(writer, value, options);
        else
            JsonSerializer.Serialize(writer, value, type, options);
    }

    public static T ReadOrDeserialize<T>(this JsonConverter<T> converter, ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        => converter != null ? converter.Read(ref reader, typeToConvert, options) : (T)JsonSerializer.Deserialize(ref reader, typeToConvert, options);

    public static void ReadAndAssert(this ref Utf8JsonReader reader)
    {
        if (!reader.Read())
            throw new JsonException();
    }
}

現在您將能夠反序列化為MarsWheatherRootObject ,如下所示:

var root = await System.Text.Json.JsonSerializer.DeserializeAsync<MarsWheatherRootObject>(
    stream, 
    new System.Text.Json.JsonSerializerOptions 
    { 
        PropertyNameCaseInsensitive = true 
    });

演示小提琴#1在這里

筆記:

  • FixedAndvariablePropertyNameObjectConverter<TObject, TDictionary, TValue>提供了一個通用框架,用於序列化和反序列化具有固定和可變屬性的對象。 如果稍后您決定反序列化例如"sol_keys" ,您可以修改MarsWheatherRootObject如下:

     [JsonConverter(typeof(MarsWheatherRootObjectConverter))] public class MarsWheatherRootObject { public Dictionary<string, MarsWheather> MarsWheathers { get; } = new Dictionary<string, MarsWheather>(); public List<string> SolKeys { get; set; } = new List<string>(); }

    轉換器如下:

     public class MarsWheatherRootObjectConverter: FixedAndvariablePropertyNameObjectConverter<MarsWheatherRootObject, Dictionary<string, MarsWheather>, MarsWheather> { static readonly Dictionary<string, ReadFixedKeyMethod> FixedKeyReadMethods = new(StringComparer.OrdinalIgnoreCase) { { "sol_keys", (ref Utf8JsonReader reader, MarsWheatherRootObject obj, string name, JsonSerializerOptions options) => { obj.SolKeys = JsonSerializer.Deserialize<List<string>>(ref reader, options); } }, { "validity_checks", (ref Utf8JsonReader reader, MarsWheatherRootObject obj, string name, JsonSerializerOptions options) => reader.Skip() }, }; static readonly Dictionary<string, WriteFixedKeyMethod> FixedKeyWriteMethods = new Dictionary<string, WriteFixedKeyMethod>() { { "sol_keys", (w, v, o) => { JsonSerializer.Serialize(w, v.SolKeys, o); } }, }; protected override Dictionary<string, MarsWheather> GetDictionary(MarsWheatherRootObject obj) => obj.MarsWheathers; protected override void SetDictionary(MarsWheatherRootObject obj, Dictionary<string, MarsWheather> dictionary) => throw new RowNotInTableException(); protected override bool TryGetFixedKeyReadMethod(string name, JsonSerializerOptions options, out ReadFixedKeyMethod method) => FixedKeyReadMethods.TryGetValue(name, out method); protected override IEnumerable<KeyValuePair<string, WriteFixedKeyMethod>> GetFixedKeyWriteMethods(JsonSerializerOptions options) => FixedKeyWriteMethods; }

    演示小提琴#2在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM