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

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

        Newtonsoft Json.net - 如何序列化流的内容?

        时间:2023-05-22
        <legend id='lwqUQ'><style id='lwqUQ'><dir id='lwqUQ'><q id='lwqUQ'></q></dir></style></legend>
            <tbody id='lwqUQ'></tbody>
            <bdo id='lwqUQ'></bdo><ul id='lwqUQ'></ul>

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

              1. <tfoot id='lwqUQ'></tfoot>
              2. <i id='lwqUQ'><tr id='lwqUQ'><dt id='lwqUQ'><q id='lwqUQ'><span id='lwqUQ'><b id='lwqUQ'><form id='lwqUQ'><ins id='lwqUQ'></ins><ul id='lwqUQ'></ul><sub id='lwqUQ'></sub></form><legend id='lwqUQ'></legend><bdo id='lwqUQ'><pre id='lwqUQ'><center id='lwqUQ'></center></pre></bdo></b><th id='lwqUQ'></th></span></q></dt></tr></i><div id='lwqUQ'><tfoot id='lwqUQ'></tfoot><dl id='lwqUQ'><fieldset id='lwqUQ'></fieldset></dl></div>
                  本文介绍了Newtonsoft Json.net - 如何序列化流的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我需要将内存流转换为 JSON 任意内容.这是我正在尝试做的一个简单示例:

                  I need to convert to JSON arbitrary content of a memory stream. Here is a quick example of what I am trying to do:

                  class Program
                  {
                      class TestClass { public int Test1;}
                      static void Main(string[] args)
                      {
                          var ms = new MemoryStream();
                          var writer = new StreamWriter(ms);
                          writer.Write(new TestClass());
                          writer.Flush();
                          ms.Position = 0;
                  
                          var json = JsonConvert.SerializeObject(/*???*/, Formatting.Indented);
                          Console.Write(json);
                          Console.Read();
                      }
                  }
                  

                  不确定将什么传递给 SerializeObject 方法.如果我通过 MemoryStream (variable ms) 我得到一个错误:

                  Not sure what to pass to the SerializeObject method. If I pass the MemoryStream (variable ms) I get an error:

                  未处理的类型异常'Newtonsoft.Json.JsonSerializationException' 发生在Newtonsoft.Json.dll

                  An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

                  附加信息:从ReadTimeout"获取值时出错'System.IO.MemoryStream'.

                  Additional information: Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.

                  是否可以序列化流的任意内容?

                  Is that possible to serialize arbitrary content of a stream ?

                  谢谢

                  推荐答案

                  MemoryStream 内容的序列化和反序列化可以使用转换器实现:

                  Serializing and deserializing content of a MemoryStream can be achieved using a converter:

                  public class MemoryStreamJsonConverter : JsonConverter
                  {
                    public override bool CanConvert(Type objectType)
                    {
                       return typeof(MemoryStream).IsAssignableFrom(objectType);
                    }
                  
                    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                    {
                       var bytes = serializer.Deserialize<byte[]>(reader);
                       return bytes != null ? new MemoryStream(bytes) : new MemoryStream();
                    }
                  
                    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                    {
                       var bytes = ((MemoryStream)value).ToArray();
                       serializer.Serialize(writer, bytes);
                    }
                  }
                  

                  那么你的代码可能看起来像这样(我将new TestClass()"更改为Test string"以便于比较 json 序列化和反序列化):

                  Then your code could look like that (I changed "new TestClass()" to "Test string" for easier comparison of json serialization and deserialization):

                  private void CheckJsonSerialization()
                  {
                     var ms = new MemoryStream();
                     var writer = new StreamWriter(ms);
                     writer.WriteLine("Test string");
                     writer.Flush();
                     ms.Position = 0;
                  
                     var json = JsonConvert.SerializeObject(ms, Formatting.Indented, new MemoryStreamJsonConverter());
                     var ms2 = JsonConvert.DeserializeObject<MemoryStream>(json, new MemoryStreamJsonConverter());
                     var reader = new StreamReader(ms2);
                     var deserializedString = reader.ReadLine();
                  
                     Console.Write(json);
                     Console.Write(deserializedString);
                     Console.Read();
                  }
                  

                  当 Stream 是序列化对象的属性时,也可以使用这种转换器:

                  Such converter can be also used when Stream is a property of a serialized object:

                    public class ClassToCheckSerialization
                    {
                       public string StringProperty { get; set; }
                  
                       [JsonConverter(typeof(MemoryStreamJsonConverter))]
                       public Stream StreamProperty { get; set; }
                    }
                  
                    private void CheckJsonSerializationOfClass()
                    {
                       var data = new ClassToCheckSerialization();
                       var ms = new MemoryStream();
                       const string entryString = "Test string inside stream";
                       var sw = new StreamWriter(ms);
                       sw.WriteLine(entryString);
                       sw.Flush();
                       ms.Position = 0;
                       data.StreamProperty = ms;
                       var json = JsonConvert.SerializeObject(data);
                  
                       var result = JsonConvert.DeserializeObject<ClassToCheckSerialization>(json);
                       var sr = new StreamReader(result.StreamProperty);
                       var stringRead = sr.ReadLine();
                       //Assert.AreEqual(entryString, stringRead);
                    }
                  

                  这篇关于Newtonsoft Json.net - 如何序列化流的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将时区偏移量添加到 JSON.NET 序列化? 下一篇:如何使用 Newtonsoft.Json 将对象序列化为带有类型信息的 json?

                  相关文章

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

                        <bdo id='7HOTi'></bdo><ul id='7HOTi'></ul>

                    1. <small id='7HOTi'></small><noframes id='7HOTi'>