【问题标题】:Adding properties to nested objects during JSON serialization of EF model在 EF 模型的 JSON 序列化期间向嵌套对象添加属性
【发布时间】:2016-01-07 13:05:27
【问题描述】:

我们有一种情况,我们使用 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 中,而不是将其添加到模型本身中。

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

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

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

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

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

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

谢谢。

【问题讨论】:

  • 为什么需要将表名存储在 JSON 中?这是您的架构的一部分,而不是您的数据,客户端不应该真正关心并且服务器已经知道。例如。在反序列化Foo 时,JSON.Net 应该能够创建整个对象图,而无需了解有关您的表的任何信息。将新创建的Foo 附加到上下文并保存,它应该可以工作,因为上下文已经通过数据属性或流式配置或任何您想要的方式知道您的架构。
  • 数据永远不会在另一端反序列化回 CLR 对象。它作为 JSON 传递给同步过程。
  • 写的“同步过程”是什么,对数据有什么作用?假设你的表没有改变,同步过程应该已经知道哪些属性映射到哪些表,它不应该在每次收到有效负载时都被告知。
  • 这是一个不受我控制的存储过程。它需要表名。

标签: c# entity-framework serialization json.net


【解决方案1】:

虽然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
        }
      ]
    }
  ]
}

小提琴:https://dotnetfiddle.net/zG5Zmm

【讨论】:

  • 我必须稍微修改一下以适应我们的情况,但这正是我想要的。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-11-07
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 2013-04-11
  • 1970-01-01
相关资源
最近更新 更多