C# 类型安全的 JSON 行反序列化

问题描述

目前我正在使用 Shopify GraphQL Bulk Query
查询返回一个 JSON Lines 文件。这样的文件可能如下所示:

{"id":"gid:\/\/shopify\/Product\/5860091625632","title":"Levis Jeans","description":"Cool Jeans","vendor":"Levis","status":"ACTIVE"}
{"id":"gid:\/\/shopify\/Productimage\/20289865679008","__parentId":"gid:\/\/shopify\/Product\/5860091625632"}
{"id":"gid:\/\/shopify\/Productvariant\/37178118963360","title":"32","position":1,"image":null,"selectedOptions":[{"name":"Size","value":"32"}],"inventoryItem":{},"__parentId":"gid:\/\/shopify\/Product\/5860091625632"}
{"available":10,"location":{"id":"gid:\/\/shopify\/Location\/57510625440"},"__parentId":"gid:\/\/shopify\/Productvariant\/37178118963360"}
{"id":"gid:\/\/shopify\/Productvariant\/37178118996128","title":"31","position":2,"value":"31"}],"__parentId":"gid:\/\/shopify\/Product\/5860091625632"}
{"available":5,"__parentId":"gid:\/\/shopify\/Productvariant\/37178118996128"}
{"available":3,"location":{"id":"gid:\/\/shopify\/Location\/57951518880"},"__parentId":"gid:\/\/shopify\/Productvariant\/37178118996128"}
{"id":"gid:\/\/shopify\/Productvariant\/37178119028896","title":"34","position":3,"value":"34"}],"__parentId":"gid:\/\/shopify\/Productvariant\/37178119028896"}
{"available":15,"__parentId":"gid:\/\/shopify\/Productvariant\/37178119028896"}

文件的每一行都是一个有效的 JSON 对象,这些行通过 __parentId 相互连接。
我的目标是将其反序列化为 C# 类,如下所示:

class Product
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public IEnumerable<Productimage> Images { get; set; }
    public IEnumerable<Productvariant> Variants { get; set; }
}
class Productimage
{
    public string Id { get; set; }
}
class Productvariant
{
    public string Id { get; set; }
    public IEnumerable<IDictionary<string,string>> SelectedOptions { get; set; }
    public IEnumerable<InventoryLevel> Levels { get; set; }
}
class InventoryLevel
{
    public int Available { get; set; }
} 

以及执行反序列化的潜在函数输出

var file = new System.IO.StreamReader(@"c:\test.jsonl");
var products = DeserializeJsonL<IEnumerable<Product>>(file);

Shopify 建议反向读取文件。我明白了。 但是我无法想象如何以类型安全的方式反序列化这个文件。我如何确定当前行是 ProductvariantProductimage 还是其他?我无法影响 JSONL 输出以包含类型信息。

我很确定没有类型信息我无法安全地反序列化它。但是我应该如何处理这些数据然后插入到数据库中?

编辑 {"id":"gid:\/\/shopify\/Product\/5860091625632"} 中的类名不能用于确定类型!

解决方法

我最终通过为每种类型定义一个唯一的字段名,将某种类型信息添加到我的 graphql 查询中,该字段名可能位于生成的 JSON 行文件中的新行。

为此,我使用了 GraphQL 字段别名:

someQuery {
   uniqueFieldAlias : fieldName
}

当我阅读文件时,我会在每一行中搜索唯一的字段名。然后我将该行反序列化为相应的类。

using (var file = new StreamReader(await res.Content.ReadAsStreamAsync()))
{
    string line;

    while ((line = await file.ReadLineAsync()) != null)
    {
        if (line.Contains("\"uniqueFieldAlias\""))
        {
            var product = JsonSerializer.Deserialize<Product>(line);
            products.Add(product);
            continue;
        }
        if (line.Contains("\"otherUniqueAlias\""))
        {
            var somethingElse = JsonSerializer.Deserialize<SomeClass>(line);
            products[productIndex].Something.Add(somethingElse);
            continue;
        }
    }
}

这个想法的灵感来自@Caius Jard 的评论