<small id='PyCIa'></small><noframes id='PyCIa'>

    <legend id='PyCIa'><style id='PyCIa'><dir id='PyCIa'><q id='PyCIa'></q></dir></style></legend>
    • <bdo id='PyCIa'></bdo><ul id='PyCIa'></ul>

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

        覆盖 Json.Net 中的默认原始类型处理

        时间:2023-08-23

            <small id='hhM7i'></small><noframes id='hhM7i'>

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

                • <bdo id='hhM7i'></bdo><ul id='hhM7i'></ul>
                    <tbody id='hhM7i'></tbody>
                • 本文介绍了覆盖 Json.Net 中的默认原始类型处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在处理原始类型时,有没有办法覆盖 Json.net 的默认反序列化行为?例如,当将 json 数组 [3.14,10,"test"] 反序列化为 object[] 的类型时,3.14 的类型将是 double10 将是 long 类型.无论如何我可以拦截或覆盖这种类型决策,以便我可以将值分别反序列化为 decimalint?

                  Is there a way to override the default deserialization behaviour of Json.net when handling primitive types? For example when deserializing the json array [3.14,10,"test"] to a type of object[] 3.14 will be of type double and 10 will be of type long. Is there anyway I can intercept or override this type decision so I could deserialize the values as decimal and int respectively?

                  我基本上总是希望 json 整数总是以 int 的形式返回,而浮点数则以 decimal 的形式返回.这将使我不必在我的代码中注入 doubledecimal 转换.

                  I basically always want json integers to always return as int and floats to return as decimal. This will save me some having to inject double to decimal conversions in my code.

                  我已经研究过扩展 Newtonsoft.Json.Serialization.DefaultContractResolver 并实现我自己的 Newtonsoft.Json.JsonConverter 但我还没有发现任何方法来实现这个期望覆盖.

                  I've looked into extending Newtonsoft.Json.Serialization.DefaultContractResolver and implementing my own Newtonsoft.Json.JsonConverter but I have not discovered any way to implement this desired override.

                  重现的示例代码

                  object[] variousTypes = new object[] {3.14m, 10, "test"};
                  string jsonString = JsonConvert.SerializeObject(variousTypes);
                  object[] asObjectArray = JsonConvert.DeserializeObject<object[]>(jsonString); // Contains object {double}, object {long}, object {string}
                  

                  推荐答案

                  我想,这应该可行

                  public class MyReader : JsonTextReader
                  {
                      public MyReader(string s) : base(new StringReader(s))
                      {
                      }
                  
                      protected override void SetToken(JsonToken newToken, object value)
                      {
                          object retObj = value;
                          if (retObj is long) retObj = Convert.ChangeType(retObj, typeof(int));
                          if (retObj is double) retObj = Convert.ChangeType(retObj, typeof(decimal));
                  
                          base.SetToken(newToken, retObj);
                      }
                  }
                  
                  
                  object[] variousTypes = new object[] { 3.14m, 10, "test" };
                  string jsonString = JsonConvert.SerializeObject(variousTypes);
                  
                  JsonSerializer serializer = new JsonSerializer();
                  var asObjectArray = serializer.Deserialize<object[]>(new MyReader(jsonString));
                  

                  这篇关于覆盖 Json.Net 中的默认原始类型处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Json.NET 使用新的部分 JSON 数据修改现有对象 下一篇:JSON.Net:反序列化多态类型而不指定程序集

                  相关文章

                  最新文章

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

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

                  1. <small id='y9G7D'></small><noframes id='y9G7D'>

                      • <bdo id='y9G7D'></bdo><ul id='y9G7D'></ul>
                      <tfoot id='y9G7D'></tfoot>