c# - 在 EF 模型的 JSON 序列化期间向嵌套对象添加属性

标签 c# entity-framework serialization json.net

我们有一种情况,我们使用 JSON 序列化 EF 模型以用于数据同步。为了使同步正常工作,我们需要模型的表名。这并不难,我们已经有了代码。主要问题是在 JSON 中传输该数据。

例如,假设我们有以下模型。

public class Foo
{
    // ...
    public virtual ICollection<Bar> Bars { get; set; }
}

public class Bar
{
    // ...
    public virtual ICollection<FooBar> FooBars { get; set; }
}

public class FooBar
{
    // ...
}

我们通过 include 拉下所有嵌套项,然后将其序列化。问题是,我们需要将实体的表名作为元数据插入到 JSON 中,而不是将其添加到模型本身。

例如,在上面的场景中,JSON 看起来像

{
    "__tableName": "Foos",
    // ...
    "Bars": [
        {
           "__tableName": "Bars"
           // ...
           "FooBars": [
               {
                   "__tableName": "FooBars"
                   // ...
               }
            ]
        }
    ]
}

我认为 JSON.Net 中的自定义序列化器是最好的方式,但要么我没有插入正确的位置,要么它们没有按照我认为的方式工作。

我试图制作自定义 JsonConverter,因为这似乎是处理自定义序列化场景的默认方式。但是,它似乎只在要序列化的基础对象上调用,而不是任何子对象。

我需要在某个地方插入 JSON.Net 才能实际放入此元数据吗?我在这个主题上找到的几乎所有内容都指向 JsonConverter,但我不确定在这种情况下它是否真的能满足我的需要。

有人认为我是将对象加载到 JsonConverter 中的 JObject 中,然后自己遍历模型树并根据需要插入键,但我希望得到一些东西比那更优雅一点。

谢谢。

最佳答案

尽管 JsonConverter 在这里似乎是合适的选择,但实际上对于此类问题,它在实践中效果不佳。问题是您希望以编程方式将转换器应用于一组广泛的类,而这些类可以包含使用相同转换器的其他类。因此,您可能会遇到转换器中递归循环的问题,您需要解决这个问题。可以做到,但可能会有点困惑。

幸运的是,对于这种情况有更好的选择。您可以将自定义 IContractResolverIValueProvider 结合使用,以将 __tableName 属性插入到每个具有表名的对象的 JSON 中。解析器负责检查特定对象类型是否具有关联的表名,如果有,则为该类型设置额外的属性。值提供程序仅在被询问时返回表名。

这是您需要的代码:

class TableNameInsertionResolver : DefaultContractResolver
{
    private Dictionary<string, string> tableNames;

    public TableNameInsertionResolver(Dictionary<string, string> tableNames)
    {
        this.tableNames = tableNames;
    }

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);

        // If there is an associated table name for this type, 
        // add a virtual property that will return the name
        string tableName;
        if (tableNames.TryGetValue(type.FullName, out tableName))
        {
            props.Insert(0, new JsonProperty
            {
                DeclaringType = type,
                PropertyType = typeof(string),
                PropertyName = "__tableName",
                ValueProvider = new TableNameValueProvider(tableName),
                Readable = true,
                Writable = false
            });
        }

        return props;
    }

    class TableNameValueProvider : IValueProvider
    {
        private string tableName;

        public TableNameValueProvider(string tableName)
        {
            this.tableName = tableName;
        }

        public object GetValue(object target)
        {
            return tableName;
        }

        public void SetValue(object target, object value)
        {
            throw new NotImplementedException();
        }
    }
}

要将其插入序列化管道,请创建一个 JsonSerializerSettings 实例并将 ContractResolver 属性设置为自定义解析器的一个实例。然后将设置传递给序列化程序。就是这样;它应该“正常工作”。

这是一个演示:

class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Foo
        {
            Id = 1,
            Bars = new List<Bar>
            {
                new Bar
                {
                    Id = 10,
                    FooBars = new List<FooBar>
                    {
                        new FooBar { Id = 100 },
                        new FooBar { Id = 101 }
                    }
                },
                new Bar
                {
                    Id = 11,
                    FooBars = new List<FooBar>
                    {
                        new FooBar { Id = 110 },
                        new FooBar { Id = 111 },
                    }
                }
            }
        };

        // Dictionary mapping class names to table names.
        Dictionary<string, string> tableNames = new Dictionary<string, string>();
        tableNames.Add(typeof(Foo).FullName, "Foos");
        tableNames.Add(typeof(Bar).FullName, "Bars");
        tableNames.Add(typeof(FooBar).FullName, "FooBars");

        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.ContractResolver = new TableNameInsertionResolver(tableNames);
        settings.Formatting = Formatting.Indented;

        string json = JsonConvert.SerializeObject(foo, settings);
        Console.WriteLine(json);
    }
}

public class Foo
{
    // ...
    public int Id { get; set; }
    public virtual ICollection<Bar> Bars { get; set; }
}

public class Bar
{
    // ...
    public int Id { get; set; }
    public virtual ICollection<FooBar> FooBars { get; set; }
}

public class FooBar
{
    // ...
    public int Id { get; set; }
}

输出:

{
  "__tableName": "Foos",
  "Id": 1,
  "Bars": [
    {
      "__tableName": "Bars",
      "Id": 10,
      "FooBars": [
        {
          "__tableName": "FooBars",
          "Id": 100
        },
        {
          "__tableName": "FooBars",
          "Id": 101
        }
      ]
    },
    {
      "__tableName": "Bars",
      "Id": 11,
      "FooBars": [
        {
          "__tableName": "FooBars",
          "Id": 110
        },
        {
          "__tableName": "FooBars",
          "Id": 111
        }
      ]
    }
  ]
}

fiddle :https://dotnetfiddle.net/zG5Zmm

关于c# - 在 EF 模型的 JSON 序列化期间向嵌套对象添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040690/

相关文章:

c# - 为什么 File.Open 这么贵?

c# - 有没有一种简单的方法可以将 C# 枚举转换为字符串,然后再返回?

c# - Entity Framework V4 : "Code Only" performance considerations

c# - 如何在 C# 中获取异步 TCP 对象流?

c# - 将多个 GridView 导出到多个 excel 选项卡(工作表)

c# - 数据源不反射(reflect) DataGridView 中的排序

c# - Entity Framework 在嵌套实体上使用 Where 子句

c# - 什么是抽象类型?

json - 使用Redstone Mapper解码Dart中的List <int>

java - HashMap 数组未反序列化