• <small id='75Yfl'></small><noframes id='75Yfl'>

      <bdo id='75Yfl'></bdo><ul id='75Yfl'></ul>
  • <i id='75Yfl'><tr id='75Yfl'><dt id='75Yfl'><q id='75Yfl'><span id='75Yfl'><b id='75Yfl'><form id='75Yfl'><ins id='75Yfl'></ins><ul id='75Yfl'></ul><sub id='75Yfl'></sub></form><legend id='75Yfl'></legend><bdo id='75Yfl'><pre id='75Yfl'><center id='75Yfl'></center></pre></bdo></b><th id='75Yfl'></th></span></q></dt></tr></i><div id='75Yfl'><tfoot id='75Yfl'></tfoot><dl id='75Yfl'><fieldset id='75Yfl'></fieldset></dl></div>

      1. <legend id='75Yfl'><style id='75Yfl'><dir id='75Yfl'><q id='75Yfl'></q></dir></style></legend>

        <tfoot id='75Yfl'></tfoot>
      2. JSON.Net:强制序列化所有私有字段和子类中的所有

        时间:2023-08-23

          1. <i id='I0VNh'><tr id='I0VNh'><dt id='I0VNh'><q id='I0VNh'><span id='I0VNh'><b id='I0VNh'><form id='I0VNh'><ins id='I0VNh'></ins><ul id='I0VNh'></ul><sub id='I0VNh'></sub></form><legend id='I0VNh'></legend><bdo id='I0VNh'><pre id='I0VNh'><center id='I0VNh'></center></pre></bdo></b><th id='I0VNh'></th></span></q></dt></tr></i><div id='I0VNh'><tfoot id='I0VNh'></tfoot><dl id='I0VNh'><fieldset id='I0VNh'></fieldset></dl></div>
              <tbody id='I0VNh'></tbody>
            <tfoot id='I0VNh'></tfoot>

              <bdo id='I0VNh'></bdo><ul id='I0VNh'></ul>
            • <small id='I0VNh'></small><noframes id='I0VNh'>

              <legend id='I0VNh'><style id='I0VNh'><dir id='I0VNh'><q id='I0VNh'></q></dir></style></legend>

                • 本文介绍了JSON.Net:强制序列化所有私有字段和子类中的所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个包含几个不同类的类,我将这些类中的信息发送给客户,但我不想将它们全部发送出去,所以有些是私有的,有些有 [JsonObject(MemberSerialization.OptIn)] 标志等

                  I have a class with several different classes and I send the information in these classes out to clients but I don't want to send them all out so some are private, some have the [JsonObject(MemberSerialization.OptIn)] flag etc.

                  但是,现在我想在需要关闭服务器时以及每 12 小时(我不想使用数据库)备份所有这些对象,所以我想做的(如果可能的话)是强制 JSON.Net Serializer 转换对象和属于该对象的所有对象.

                  However, now I want to do a backup of all these objects when I need to shutdown the server and every 12 hours (I don't want to use a database) so what I want to do (if possible) is to force the JSON.Net Serializer to convert the object and all the object belonging to that object.

                  例如:

                  class Foo
                  {
                    public int Number;
                    private string name;
                    private PrivateObject po = new PrivateObject();
                  
                    public string ToJSON()
                    { /* Serialize my public field, my property and the object PrivateObject */ }
                  }
                  

                  我尝试了这段代码(即使它已经过时),但它没有序列化与我的对象相关的对象:

                  I tried this code (even though it's obsolete) but it doesn't Serialize the objects related to my object:

                  Newtonsoft.Json.JsonSerializerSettings jss = new Newtonsoft.Json.JsonSerializerSettings();
                  
                  Newtonsoft.Json.Serialization.DefaultContractResolver dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                  dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
                  jss.ContractResolver = dcr;
                  
                  return Newtonsoft.Json.JsonConvert.SerializeObject(this, jss);
                  

                  推荐答案

                  这应该可行:

                  var settings = new JsonSerializerSettings() { ContractResolver = new MyContractResolver() };
                  var json = JsonConvert.SerializeObject(obj, settings);
                  

                  <小时>

                  public class MyContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
                  {
                      protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
                      {
                          var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                          .Select(p => base.CreateProperty(p, memberSerialization))
                                      .Union(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                                 .Select(f => base.CreateProperty(f, memberSerialization)))
                                      .ToList();
                          props.ForEach(p => { p.Writable = true; p.Readable = true; });
                          return props;
                      }
                  }
                  

                  这篇关于JSON.Net:强制序列化所有私有字段和子类中的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 JSON.NET 解析 JSON 对象流的正确方法是什么 下一篇:使属性反序列化但不使用 json.net 序列化

                  相关文章

                  最新文章

                  • <bdo id='1yUaY'></bdo><ul id='1yUaY'></ul>
                • <small id='1yUaY'></small><noframes id='1yUaY'>

                  1. <legend id='1yUaY'><style id='1yUaY'><dir id='1yUaY'><q id='1yUaY'></q></dir></style></legend>

                    1. <i id='1yUaY'><tr id='1yUaY'><dt id='1yUaY'><q id='1yUaY'><span id='1yUaY'><b id='1yUaY'><form id='1yUaY'><ins id='1yUaY'></ins><ul id='1yUaY'></ul><sub id='1yUaY'></sub></form><legend id='1yUaY'></legend><bdo id='1yUaY'><pre id='1yUaY'><center id='1yUaY'></center></pre></bdo></b><th id='1yUaY'></th></span></q></dt></tr></i><div id='1yUaY'><tfoot id='1yUaY'></tfoot><dl id='1yUaY'><fieldset id='1yUaY'></fieldset></dl></div>
                    2. <tfoot id='1yUaY'></tfoot>