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

C# 在EF模型的JSON序列化期间向嵌套对象添加属性,c#,entity-framework,serialization,json.net,C#,Entity Framework,Serialization,Json.net,在这种情况下,我们使用JSON序列化EF模型以用于数据同步。为了使同步正常工作,我们需要模型的表名。这并不难,我们已经有了代码来提供这一点。主要问题是在JSON中传输数据 例如,假设我们有以下模型 public class Foo { // ... public virtual ICollection<Bar> Bars { get; set; } } public class Bar { // ... public virtual ICollect

在这种情况下,我们使用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
{
    // ...
}
我认为JSON.Net中的自定义序列化程序是最好的方法,但要么我没有在正确的位置插入,要么它们没有按照我认为的方式工作

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

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

有一种想法是,我将对象加载到
JsonConverter
中的
JObject
,然后自己遍历模型树并根据需要插入键,但我希望得到比这更优雅的东西


谢谢。

虽然
JsonConverter
在这里似乎是合适的选择,但实际上对于这种类型的问题,它在实践中并没有起到很好的作用。问题是您希望以编程方式将转换器应用于广泛的类集合,而这些类可以包含使用相同转换器的其他类。因此,您可能会遇到转换器中递归循环的问题,您需要解决这些问题。这是可以做到的,但可能会有点混乱

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

以下是您需要的代码:

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();
        }
    }
}
输出:

{
  "__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:

为什么需要将表名存储在JSON中?这是您的模式的一部分,而不是您的数据,客户机不应该真正关心这些,服务器也已经知道了。例如,当反序列化一个
Foo
时,JSON.Net应该能够创建整个对象图,而不必知道任何关于表的信息。将新创建的
Foo
附加到一个上下文并保存,它应该可以工作,因为上下文已经通过数据属性或fluent配置或其他方式知道您的模式。数据在另一端永远不会反序列化回CLR对象。它以JSON的形式传递给同步进程。什么是“同步进程”,它对数据做了什么?假设您的表没有改变,同步过程应该已经知道哪些属性映射到哪些表,而不需要在每次收到有效负载时都告诉它。这是一个我无法控制的存储过程。它需要表名。我必须做一些修改以适应我们的情况,但这正是我想要的。谢谢
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
        }
      ]
    }
  ]
}