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

      <bdo id='x6IbV'></bdo><ul id='x6IbV'></ul>

  • <small id='x6IbV'></small><noframes id='x6IbV'>

  • <tfoot id='x6IbV'></tfoot>

      1. json.net:为字典键指定转换器

        时间:2023-08-24
      2. <small id='6jfLK'></small><noframes id='6jfLK'>

          <tbody id='6jfLK'></tbody>

          <tfoot id='6jfLK'></tfoot>

              <bdo id='6jfLK'></bdo><ul id='6jfLK'></ul>
                <i id='6jfLK'><tr id='6jfLK'><dt id='6jfLK'><q id='6jfLK'><span id='6jfLK'><b id='6jfLK'><form id='6jfLK'><ins id='6jfLK'></ins><ul id='6jfLK'></ul><sub id='6jfLK'></sub></form><legend id='6jfLK'></legend><bdo id='6jfLK'><pre id='6jfLK'><center id='6jfLK'></center></pre></bdo></b><th id='6jfLK'></th></span></q></dt></tr></i><div id='6jfLK'><tfoot id='6jfLK'></tfoot><dl id='6jfLK'><fieldset id='6jfLK'></fieldset></dl></div>
                1. <legend id='6jfLK'><style id='6jfLK'><dir id='6jfLK'><q id='6jfLK'></q></dir></style></legend>
                  本文介绍了json.net:为字典键指定转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 JSON:

                  { 
                      "data": { "A": 5, "B": 6 }, 
                      "foo": "foo", 
                      "bar": "bar" 
                  }
                  

                  我需要将数据反序列化成一个类:

                  I need to deserialize data into a class:

                  public Dictionary<MyEnum, int> Data { get; set; }
                  public string Foo { get; set; }
                  public string Bar { get; set; }
                  

                  但 MyEnum 的值是 CodeACodeB 而不是分别简单的 AB.

                  But MyEnum values are CodeA, and CodeB instead of simply A and B respectively.

                  我有一个可以处理转换的自定义转换器.但是如何指定 JsonConverter 与 Dictionary 键一起使用?

                  I have a custom Converter that can handle conversion. But how do I specify a JsonConverter to use with Dictionary keys?

                  推荐答案

                  我认为唯一的方法是为整个 Dictionary 类型或 Dictionary<我的枚举,T>.

                  I believe the only way is to make a JsonConverter for the whole Dictionary<MyEnum, int> type, or Dictionary<MyEnum, T>.

                  字典键不被视为值,不会通过 JsonConverters 运行.TypeConverters 本来是一个解决方案,但是在查看 TypeConverters 之前,将输入默认字符串到枚举转换.

                  Dictionary keys are not regarded as values and will not be run through the JsonConverters. TypeConverters would have been a solution, but the default string to enum conversion will enter before it looks at the TypeConverters.

                  所以...我认为没有其他方法可以做到.

                  So... I don't think it can be done any other way.

                  未完全测试,但我在我的项目中使用了类似的东西:

                  Not fully tested, but I use something like this in a project of mine:

                  public class DictionaryWithSpecialEnumKeyConverter : JsonConverter
                  {
                      public override bool CanWrite
                      {
                          get { return false; }
                      }
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          throw new NotSupportedException();
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          if (reader.TokenType == JsonToken.Null)
                              return null;
                  
                          var valueType = objectType.GetGenericArguments()[1];
                          var intermediateDictionaryType = typeof(Dictionary<,>).MakeGenericType(typeof(string), valueType);
                          var intermediateDictionary = (IDictionary)Activator.CreateInstance(intermediateDictionaryType);
                          serializer.Populate(reader, intermediateDictionary);
                  
                          var finalDictionary = (IDictionary)Activator.CreateInstance(objectType);
                          foreach (DictionaryEntry pair in intermediateDictionary)
                              finalDictionary.Add(Enum.Parse(MyEnum, "Code" + pair.Key, false), pair.Value);
                  
                          return finalDictionary;
                      }
                  
                      public override bool CanConvert(Type objectType)
                      {
                          return objectType.IsA(typeof(IDictionary<,>)) &&
                                 objectType.GetGenericArguments()[0].IsA<MyEnum>();
                      }
                  }
                  

                  你需要这个小帮手:

                      public static bool IsA(this Type type, Type typeToBe)
                      {
                          if (!typeToBe.IsGenericTypeDefinition)
                              return typeToBe.IsAssignableFrom(type);
                  
                          var toCheckTypes = new List<Type> { type };
                          if (typeToBe.IsInterface)
                              toCheckTypes.AddRange(type.GetInterfaces());
                  
                          var basedOn = type;
                          while (basedOn.BaseType != null)
                          {
                              toCheckTypes.Add(basedOn.BaseType);
                              basedOn = basedOn.BaseType;
                          }
                  
                          return toCheckTypes.Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeToBe);
                      }
                  

                  希望对你有用.

                  这篇关于json.net:为字典键指定转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:没有转义反斜杠的 Newtonsoft.Json SerializeObject 下一篇:使用 Json.NET 序列化为 NDJSON

                  相关文章

                  最新文章

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

                      <legend id='zrZWH'><style id='zrZWH'><dir id='zrZWH'><q id='zrZWH'></q></dir></style></legend>
                    2. <tfoot id='zrZWH'></tfoot>