使用 JSON.Net 反序列化$ref文档指针

本文关键字:ref 文档 指针 反序列化 JSON Net 使用 | 更新日期: 2023-09-27 18:35:07

我已经熬了几个小时试图弄清楚这一点。 似乎有很多帖子围绕我遇到的问题,但没有一个帖子似乎对我有用。

我非常简单地想使用 json.net 反序列化我的 json 文档中的 $ref 字段

示例 JSON:

{
  "items": [
    { "$ref": "#/parameters/RequestId" },
    {
      "name": "user",
      "in": "body",
      "description": "User metadata object",
      "required": true
    }
  ],
  "parameters": {
    "RequestId": {
      "name": "X-requestId",
      "in": "header",
      "description": "this is a request id",
      "required": false
    }
  }
}

示例类:

public class Item
{
  public string Name {get;set;}
  public string In {get;set;}
  public string Description {get;set;}
  public bool Required {get;set;}
}
public class RootDoc 
{   
  public List<Item> Items {get;set;}   
  public Dictionary<string, Item> Parameters {get;set;}
}

我期待什么:

var doc = JsonConvert.DeserializeObject<RootDoc>(json, new JsonSerializerSettings()
                {
                    MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
                    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                    Error = delegate (object sender, ErrorEventArgs args)
                    {
                        // Handle all errors
                        args.ErrorContext.Handled = true;
                    }
                });
Assert.IsNotNull(doc);
Assert.IsNotNull(doc.Items);
Assert.IsTrue(doc.Items.Count == 2);
Assert.IsTrue(doc.Items[0].Name == "X-requestId");
Assert.IsTrue(doc.Items[1].Name == "user");

编辑:

我回答了下面的问题。 我不敢相信我必须这样做,但它有效。

使用 JSON.Net 反序列化$ref文档指针

哇... 这就是我为解决问题所做的。 我不确定是否有更简单的方法,我不敢相信 json.net 没有提供开箱即用的方法。

var x = JObject.Parse(json);
            var xx = x.Descendants().ToList().Where(d => d.Path.ToLower().EndsWith("$ref"));
            foreach (var item in xx)
            {
                if (!item.HasValues)
                    continue;
                string str = item.First.ToString().TrimStart(new char[] { '#' }).TrimStart(new char[] { '/' });
                var split = str.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            JToken token = x;
            for (int i = 0;i<split.Length; i++)
            {
                token = token[split[i]];
            }
            item.Parent.Replace(token);
        }
        var doc = JsonConvert.DeserializeObject<RootDoc>(x.ToString(), new JsonSerializerSettings()
        {
            PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
            Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
            {
                // Handle all errors
                args.ErrorContext.Handled = true;
            }
        });
        Assert.IsNotNull(doc);
        Assert.IsNotNull(doc.Items);
        Assert.IsTrue(doc.Items.Count == 2);
        Assert.IsTrue(doc.Items[0].Name == "X-requestId");
        Assert.IsTrue(doc.Items[1].Name == "user");

您可以使用此代码。

JObject o = JObject.Parse(json);
JToken tokenToReplace = o.SelectToken("$..$ref");
string[] s = tokenToReplace.Value<string>().Split('/');
string a = s[s.Length - 1];
JToken newToken = o.SelectToken("$.." + a);
JArray items = (JArray)o["items"];
items.Add(newToken);
tokenToReplace.Parent.Parent.Remove();
string newJson = o.ToString();

newJson包含替换的$ref