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

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

    <tfoot id='etbgj'></tfoot>

      • <bdo id='etbgj'></bdo><ul id='etbgj'></ul>

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

        在 JsonConverter.ReadJson() 中有效地获取完整的 json

        时间:2023-08-24

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

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

              <tbody id='q4GNJ'></tbody>
          • <legend id='q4GNJ'><style id='q4GNJ'><dir id='q4GNJ'><q id='q4GNJ'></q></dir></style></legend>

                • <tfoot id='q4GNJ'></tfoot>
                  <i id='q4GNJ'><tr id='q4GNJ'><dt id='q4GNJ'><q id='q4GNJ'><span id='q4GNJ'><b id='q4GNJ'><form id='q4GNJ'><ins id='q4GNJ'></ins><ul id='q4GNJ'></ul><sub id='q4GNJ'></sub></form><legend id='q4GNJ'></legend><bdo id='q4GNJ'><pre id='q4GNJ'><center id='q4GNJ'></center></pre></bdo></b><th id='q4GNJ'></th></span></q></dt></tr></i><div id='q4GNJ'><tfoot id='q4GNJ'></tfoot><dl id='q4GNJ'><fieldset id='q4GNJ'></fieldset></dl></div>
                  本文介绍了在 JsonConverter.ReadJson() 中有效地获取完整的 json 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何在 JsonConverter.ReadJson() 中有效地获取完整的 json 字符串?

                  我能做到:

                  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {var json = JObject.Load(reader).ToString(Formatting.None);

                  但是这似乎非常低效,因为我无缘无故地序列化和反序列化

                  还有更好的方法吗?

                  解决方案

                  ReadJson() 必须完全解析正在读取的 JSON,以便确认 JSON 格式正确且 JsonReader 正确退出时位于当前值的末尾.但是,无需将整个 JSON 加载到中间 JObject 层次结构中,只需将其重新转换为 JSON 字符串.相反,您可以通过使用 JRaw.Create 获得更好的性能():

                  var json = JRaw.Create(reader).ToString();

                  可以在 参考源,此方法直接从传入的 JsonReader 流式传输到 StringWriter - 无需加载到中间 JToken 层次结构并重新序列化 - 通过使用 JsonWriter.WriteToken(JsonReader):

                  <块引用>

                  public static JRaw Create(JsonReader reader){使用 (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))使用 (JsonTextWriter jsonWriter = new JsonTextWriter(sw)){jsonWriter.WriteToken(reader);返回新的 JRaw(sw.ToString());}}

                  生成的 JRaw 只是将该字符串封装在其 .(当然,不能保证生成的 JSON 代表一个对象,只能代表格式正确的 JSON.)

                  请注意,JsonTextReader 会自动识别和解析 常用格式为DateTime对象,也将浮点值解析为double.如果您需要最字面"的 JSON 字符串,您可能希望抑制 DateTime 识别和/或将浮点值解析为 decimal.以下以 JRaw.Create() 为模型的扩展方法完成了这项工作:

                  public static string ReadOuterJson(this JsonReader reader, Formatting formatting = Formatting.None, DateParseHandling? dateParseHandling = null, FloatParseHandling? floatParseHandling = null){//如果您希望空 JSON 值返回空字符串,请删除此行:if (reader.TokenType == JsonToken.Null)返回空值;var oldDateParseHandling = reader.DateParseHandling;var oldFloatParseHandling = reader.FloatParseHandling;尝试{if (dateParseHandling != null)reader.DateParseHandling = dateParseHandling.Value;if (floatParseHandling != null)reader.FloatParseHandling = floatParseHandling.Value;使用 (var sw = new StringWriter(CultureInfo.InvariantCulture))使用 (var jsonWriter = new JsonTextWriter(sw) { Formatting =formatting }){jsonWriter.WriteToken(reader);返回 sw.ToString();}}最后{reader.DateParseHandling = oldDateParseHandling;reader.FloatParseHandling = oldFloatParseHandling;}}

                  然后这样称呼它,例如:

                  var json = reader.ReadOuterJson(dateParseHandling: DateParseHandling.None);

                  有关为什么需要这样做的详细信息,请参阅:

                  • Json.NET 在反序列化时解释和修改 ISO 日期JObject #862.

                  • JObject.Parse 修改浮点值的结尾.

                  How can I efficiently get full json string in JsonConverter.ReadJson() ?

                  I can do:

                  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
                        var json = JObject.Load(reader).ToString(Formatting.None);
                  

                  However that seems very inefficient as I serialize and deserialize for no reason

                  Any better way ?

                  解决方案

                  ReadJson() must fully parse the JSON being read so that the JSON is confirmed to be well-formed and the JsonReader is correctly positioned at the end of the current value upon exit. However, it is not necessary to load the entire JSON into an intermediate JObject hierarchy simply to re-convert it to a JSON string. Instead, you may be able to get better performance by using JRaw.Create():

                  var json = JRaw.Create(reader).ToString();
                  

                  As can be seen in the reference source, this method streams directly from the incoming JsonReader to a StringWriter - without loading into an intermediate JToken hierarchy and re-serializing - by using JsonWriter.WriteToken(JsonReader):

                  public static JRaw Create(JsonReader reader)
                  {
                      using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
                      using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
                      {
                          jsonWriter.WriteToken(reader);
                  
                          return new JRaw(sw.ToString());
                      }
                  }
                  

                  The resulting JRaw simply encapsulates that string in its Value. (Of course, there is no guarantee that the resulting JSON represents an object, only that it represents well-formed JSON.)

                  Note that JsonTextReader will automatically recognize and parse dates and times in common formats as DateTime objects, and also parse floating point values as double. If you need the "most literal" JSON string you may want to suppress DateTime recognition and/or parse floating point values as decimal. The following extension method, modeled on JRaw.Create(), does the job:

                  public static string ReadOuterJson(this JsonReader reader, Formatting formatting = Formatting.None, DateParseHandling? dateParseHandling = null, FloatParseHandling? floatParseHandling = null)
                  {
                      // If you would prefer a null JSON value to return an empty string, remove this line:
                      if (reader.TokenType == JsonToken.Null)
                          return null;
                      var oldDateParseHandling = reader.DateParseHandling;
                      var oldFloatParseHandling = reader.FloatParseHandling;
                      try
                      {
                          if (dateParseHandling != null)
                              reader.DateParseHandling = dateParseHandling.Value;
                          if (floatParseHandling != null)
                              reader.FloatParseHandling = floatParseHandling.Value;
                          using (var sw = new StringWriter(CultureInfo.InvariantCulture))
                          using (var jsonWriter = new JsonTextWriter(sw) { Formatting = formatting })
                          {
                              jsonWriter.WriteToken(reader);
                              return sw.ToString();
                          }
                      }
                      finally
                      {
                          reader.DateParseHandling = oldDateParseHandling;
                          reader.FloatParseHandling = oldFloatParseHandling;
                      }
                  }
                  

                  And then call it like, e.g.:

                  var json = reader.ReadOuterJson(dateParseHandling: DateParseHandling.None);
                  

                  For details on why this may be necessary, see:

                  • Json.NET interprets and modifies ISO dates when deserializing to JObject #862.

                  • JObject.Parse modifies end of floating point values.

                  这篇关于在 JsonConverter.ReadJson() 中有效地获取完整的 json 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <tfoot id='VcrGW'></tfoot>

                    <tbody id='VcrGW'></tbody>

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

                • <legend id='VcrGW'><style id='VcrGW'><dir id='VcrGW'><q id='VcrGW'></q></dir></style></legend>

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