Json.NET 可以对流进行序列化/反序列化吗?

问题描述 投票:0回答:7

我听说 Json.NET 比 DataContractJsonSerializer 更快,想尝试一下...

但是我在 JsonConvert 上找不到任何采用流而不是字符串的方法。

以WinPhone上反序列化包含JSON的文件为例,我使用以下代码将文件内容读取为字符串,然后反序列化为JSON。在我的(非常临时的)测试中,它似乎比使用 DataContractJsonSerializer 直接从流中反序列化慢大约 4 倍......

// DCJS
DataContractJsonSerializer dc = new DataContractJsonSerializer(typeof(Constants));
Constants constants = (Constants)dc.ReadObject(stream);

// JSON.NET
string json = new StreamReader(stream).ReadToEnd();
Constants constants = JsonConvert.DeserializeObject<Constants>(json);

我做错了吗?

.net serialization json.net
7个回答
349
投票

当前版本的 Json.net 不允许您使用接受的答案代码。当前的替代方案是:

public static object DeserializeFromStream(Stream stream)
{
    var serializer = new JsonSerializer();

    using (var sr = new StreamReader(stream))
    using (var jsonTextReader = new JsonTextReader(sr))
    {
        return serializer.Deserialize(jsonTextReader);
    }
}

文档:从文件流反序列化 JSON


120
投票
public static void Serialize(object value, Stream s)
{
    using (StreamWriter writer = new StreamWriter(s))
    using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
    {
        JsonSerializer ser = new JsonSerializer();
        ser.Serialize(jsonWriter, value);
        jsonWriter.Flush();
    }
}

public static T Deserialize<T>(Stream s)
{
    using (StreamReader reader = new StreamReader(s))
    using (JsonTextReader jsonReader = new JsonTextReader(reader))
    {
        JsonSerializer ser = new JsonSerializer();
        return ser.Deserialize<T>(jsonReader);
    }
}

65
投票

更新:这在当前版本中不再有效,请参阅下面以获得正确答案(无需投票否决,这在旧版本中是正确的)。

JsonTextReader
类与
StreamReader
一起使用,或使用直接采用
JsonSerializer
StreamReader
重载:

var serializer = new JsonSerializer();
serializer.Deserialize(streamReader);

31
投票

我编写了一个扩展类来帮助我从 JSON 源(字符串、流、文件)进行反序列化。

public static class JsonHelpers
{
    public static T CreateFromJsonStream<T>(this Stream stream)
    {
        JsonSerializer serializer = new JsonSerializer();
        T data;
        using (StreamReader streamReader = new StreamReader(stream))
        {
            data = (T)serializer.Deserialize(streamReader, typeof(T));
        }
        return data;
    }

    public static T CreateFromJsonString<T>(this String json)
    {
        T data;
        using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
        {
            data = CreateFromJsonStream<T>(stream);
        }
        return data;
    }

    public static T CreateFromJsonFile<T>(this String fileName)
    {
        T data;
        using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
        {
            data = CreateFromJsonStream<T>(fileStream);
        }
        return data;
    }
}

反序列化现在就像写作一样简单:

MyType obj1 = aStream.CreateFromJsonStream<MyType>();
MyType obj2 = "{\"key\":\"value\"}".CreateFromJsonString<MyType>();
MyType obj3 = "data.json".CreateFromJsonFile<MyType>();

希望对其他人有帮助。


17
投票

我遇到这个问题时正在寻找一种方法,将开放式对象列表流式传输到

System.IO.Stream
并从另一端读取它们,而无需在发送之前缓冲整个列表。 (具体来说,我通过 Web API 从 MongoDB 流式传输持久对象。)

@Paul Tyng 和@Rivers 出色地回答了最初的问题,我使用他们的答案为我的问题构建了概念验证。我决定在这里发布我的测试控制台应用程序,以防其他人遇到同样的问题。

using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace TestJsonStream {
    class Program {
        static void Main(string[] args) {
            using(var writeStream = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None)) {
                string pipeHandle = writeStream.GetClientHandleAsString();
                var writeTask = Task.Run(() => {
                    using(var sw = new StreamWriter(writeStream))
                    using(var writer = new JsonTextWriter(sw)) {
                        var ser = new JsonSerializer();
                        writer.WriteStartArray();
                        for(int i = 0; i < 25; i++) {
                            ser.Serialize(writer, new DataItem { Item = i });
                            writer.Flush();
                            Thread.Sleep(500);
                        }
                        writer.WriteEnd();
                        writer.Flush();
                    }
                });
                var readTask = Task.Run(() => {
                    var sw = new Stopwatch();
                    sw.Start();
                    using(var readStream = new AnonymousPipeClientStream(pipeHandle))
                    using(var sr = new StreamReader(readStream))
                    using(var reader = new JsonTextReader(sr)) {
                        var ser = new JsonSerializer();
                        if(!reader.Read() || reader.TokenType != JsonToken.StartArray) {
                            throw new Exception("Expected start of array");
                        }
                        while(reader.Read()) {
                            if(reader.TokenType == JsonToken.EndArray) break;
                            var item = ser.Deserialize<DataItem>(reader);
                            Console.WriteLine("[{0}] Received item: {1}", sw.Elapsed, item);
                        }
                    }
                });
                Task.WaitAll(writeTask, readTask);
                writeStream.DisposeLocalCopyOfClientHandle();
            }
        }

        class DataItem {
            public int Item { get; set; }
            public override string ToString() {
                return string.Format("{{ Item = {0} }}", Item);
            }
        }
    }
}

请注意,当

AnonymousPipeServerStream
被处置时,您可能会收到异常,我忽略了这一点,因为它与当前的问题无关。


0
投票

如果您正在阅读 Json,另一种选择是在 JsonConvert 类上使用 DeserializeObject:

using (StreamReader streamReader = new StreamReader("example.json"))
            {
                string json = streamReader.ReadToEnd();
                ObjectType object = JsonConvert.DeserializeObject<ObjectType>(json);
            }

-1
投票

内存不足时另一个方便的选择是定期刷新

/// <summary>serialize the value in the stream.</summary>
/// <typeparam name="T">the type to serialize</typeparam>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
/// <param name="settings">The json settings to use.</param>
/// <param name="bufferSize"></param>
/// <param name="leaveOpen"></param>
public static void JsonSerialize<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false)
{
    using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize,leaveOpen))
    using (var jsonWriter = new JsonTextWriter(writer))
    {
        var ser = JsonSerializer.Create( settings );
        ser.Serialize(jsonWriter, value);
        jsonWriter.Flush();
    }
}

/// <summary>serialize the value in the stream asynchronously.</summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
/// <param name="settings">The settings.</param>
/// <param name="bufferSize">The buffer size, in bytes, set -1 to not flush till done</param>
/// <param name="leaveOpen"> true to leave the stream open </param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
public static Task JsonSerializeAsync<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false, CancellationToken cancellationToken=default)
{
    using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize: bufferSize,leaveOpen: leaveOpen))
    using (var jsonWriter = new JsonTextWriter(writer))
    {
        var ser = JsonSerializer.Create( settings );
        ser.Serialize(jsonWriter, value);
        return  jsonWriter.Flush();
    }
    //jsonWriter.FlushAsnc with my version gives an error on the stream
    return Task.CompletedTask;
}

您可以像这样测试/使用它:

[TestMethod()]
public void WriteFileIntoJsonTest()
{
    var file = new FileInfo(Path.GetTempFileName());
    try
    {
        var list = new HashSet<Guid>();
        for (int i = 0; i < 100; i++)
        {
            list.Add(Guid.NewGuid());
        }
        file.JsonSerialize(list);


        var sr = file.IsValidJson<List<Guid>>(out var result);
        Assert.IsTrue(sr);
        Assert.AreEqual<int>(list.Count, result.Count);
        foreach (var item in result)
        {
            Assert.IsFalse(list.Add(item), $"The GUID {item} should already exist in the hash set");
        }
    }
    finally
    {
        file.Refresh();
        file.Delete();
    }
}

您需要创建扩展方法,这是整套方法:

public static class JsonStreamReaderExt
    {
       static JsonSerializerSettings _settings ;
        static JsonStreamReaderExt()
        {
            _settings = JsonConvert.DefaultSettings?.Invoke() ?? new JsonSerializerSettings();
            _settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
            _settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            _settings.DateFormatHandling = DateFormatHandling.IsoDateFormat ;
        }

    /// <summary>
    /// serialize the value in the stream.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="stream">The stream.</param>
    /// <param name="value">The value.</param>
    public static void JsonSerialize<T>(this Stream stream,[DisallowNull] T value)
    {
        stream.JsonSerialize(value,_settings);
    }

    /// <summary>
    /// serialize the value in the file .
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="file">The file.</param>
    /// <param name="value">The value.</param>
    public static void JsonSerialize<T>(this FileInfo file,[DisallowNull] T value)
    {
        if (string.IsNullOrEmpty(file.DirectoryName)==true && Directory.Exists(file.DirectoryName) == false)
        { 
            Directory.CreateDirectory(file.FullName);
        }
        
        using var s = file.OpenWrite();
        s.JsonSerialize(value, _settings);

        file.Refresh();
    }

    /// <summary>
    /// serialize the value in the file .
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="file">The file.</param>
    /// <param name="value">The value.</param>
    /// <param name="settings">the json settings to use</param>
    public static void JsonSerialize<T>(this FileInfo file, [DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings)
    {
        if (string.IsNullOrEmpty(file.DirectoryName) == true && Directory.Exists(file.DirectoryName) == false)
        {
            Directory.CreateDirectory(file.FullName);
        }

        using var s = file.OpenWrite();
        s.JsonSerialize(value, settings);

        file.Refresh();
    }

    /// <summary>
    /// serialize the value in the file .
    /// </summary>
    /// <remarks>File will be refreshed to contain the new meta data</remarks>
    /// <typeparam name="T">the type to serialize</typeparam>
    /// <param name="file">The file.</param>
    /// <param name="value">The value.</param>        
    /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
    public static async Task JsonSerializeAsync<T>(this FileInfo file, [DisallowNull] T value, CancellationToken cancellationToken = default)
    {
        if (string.IsNullOrEmpty(file.DirectoryName) == true && Directory.Exists(file.DirectoryName) == false)
        {
            Directory.CreateDirectory(file.FullName);
        }

        using (var stream = file.OpenWrite())
        {
            await stream.JsonSerializeAsync(value, _settings,bufferSize:1024,leaveOpen:false, cancellationToken).ConfigureAwait(false);
        }
        file.Refresh();
    }
    /// <summary>
    /// serialize the value in the file .
    /// </summary>
    /// <remarks>File will be refreshed to contain the new meta data</remarks>
    /// <typeparam name="T">the type to serialize</typeparam>
    /// <param name="file">The file to create or overwrite.</param>
    /// <param name="value">The value.</param>
    /// <param name="settings">the json settings to use</param>
    /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
    public static async Task JsonSerializeAsync<T>(this FileInfo file, [DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, CancellationToken cancellationToken=default)
    {
        if (string.IsNullOrEmpty(file.DirectoryName) == true && Directory.Exists(file.DirectoryName) == false)
        {
            Directory.CreateDirectory(file.FullName);
        }

        using (var stream = file.OpenWrite())
        {
            await stream.JsonSerializeAsync(value, settings,bufferSize:1024,leaveOpen:false, cancellationToken).ConfigureAwait(false);
        }
        file.Refresh();
    }

    /// <summary>serialize the value in the stream.</summary>
    /// <typeparam name="T">the type to serialize</typeparam>
    /// <param name="stream">The stream.</param>
    /// <param name="value">The value.</param>
    /// <param name="settings">The json settings to use.</param>
    /// <param name="bufferSize">The buffer size, in bytes, set -1 to not flush till done</param>
    /// <param name="leaveOpen"> true to leave the stream open </param>
    public static void JsonSerialize<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false)
    {
        using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize,leaveOpen))
        using (var jsonWriter = new JsonTextWriter(writer))
        {
            var ser = JsonSerializer.Create( settings );
            ser.Serialize(jsonWriter, value);
            jsonWriter.Flush();
        }
    }

    /// <summary>serialize the value in the stream asynchronously.</summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="stream">The stream.</param>
    /// <param name="value">The value.</param>
    /// <param name="settings">The settings.</param>
    /// <param name="bufferSize">The buffer size, in bytes, set -1 to not flush till done</param>
    /// <param name="leaveOpen"> true to leave the stream open </param>
    /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
    public static Task JsonSerializeAsync<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false, CancellationToken cancellationToken=default)
    {
        using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize: bufferSize,leaveOpen: leaveOpen))
        using (var jsonWriter = new JsonTextWriter(writer))
        {
            var ser = JsonSerializer.Create( settings );
            ser.Serialize(jsonWriter, value);
            jsonWriter.Flush();
        }
        return Task.CompletedTask;
       
    }



    /// <summary>
    /// Determines whether [is valid json] [the specified result].
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="stream">The stream.</param>
    /// <param name="result">The result.</param>
    /// <returns><c>true</c> if [is valid json] [the specified result]; otherwise, <c>false</c>.</returns>
    public static bool IsValidJson<T>(this Stream stream, [NotNullWhen(true)] out T? result)
    {
        if (stream is null)
        {
            throw new ArgumentNullException(nameof(stream));
        }

        if (stream.Position != 0)
        {
            stream.Seek(0, SeekOrigin.Begin);
        }
        JsonSerializerSettings settings = (JsonConvert.DefaultSettings?.Invoke()) ?? new JsonSerializerSettings() { DateTimeZoneHandling = DateTimeZoneHandling.Utc, DateFormatHandling = DateFormatHandling.IsoDateFormat };
        settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
        using (var reader = new StreamReader(stream))
        using (var jsonReader = new JsonTextReader(reader))
        {
            var ser = JsonSerializer.Create(settings);
            try
            {
                result = ser.Deserialize<T>(jsonReader);
            }
            catch { result = default; }
        }
        return result is not null;
    }
    /// <summary>
    /// Determines whether [is valid json] [the specified settings].
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="stream">The stream.</param>
    /// <param name="settings">The settings.</param>
    /// <param name="result">The result.</param>
    /// <returns><c>true</c> if [is valid json] [the specified settings]; otherwise, <c>false</c>.</returns>
    public static bool IsValidJson<T>(this Stream stream, JsonSerializerSettings settings, [NotNullWhen(true)] out T? result)
    {
        if (stream is null)
        {
            throw new ArgumentNullException(nameof(stream));
        }

        if (settings is null)
        {
            throw new ArgumentNullException(nameof(settings));
        }

        if (stream.Position != 0)
        {
            stream.Seek(0, SeekOrigin.Begin);
        }
        using (var reader = new StreamReader(stream))
        using (var jsonReader = new JsonTextReader(reader))
        {
            var ser = JsonSerializer.Create(settings);
            try
            {
                result = ser.Deserialize<T>(jsonReader);
            }
            catch { result = default; }
        }
        return result is not null;
    }
    /// <summary>
    /// Determines whether file contains valid json using the specified settings and reads it into the output.
    /// </summary>
    /// <typeparam name="T">Type to convert into</typeparam>
    /// <param name="file">The file.</param>
    /// <param name="settings">The settings.</param>
    /// <param name="result">The result.</param>
    /// <returns><c>true</c> if [is valid json] [the specified settings]; otherwise, <c>false</c>.</returns>
    /// <exception cref="System.ArgumentNullException">file</exception>
    /// <exception cref="System.ArgumentNullException"></exception>
    /// <exception cref="System.ArgumentNullException">settings</exception>
    /// <exception cref="System.IO.FileNotFoundException">File could not be accessed</exception>
    public static bool IsValidJson<T>(this FileInfo file, JsonSerializerSettings settings, [NotNullWhen(true)] out T? result)
    {
        if (file is null)
        {
            throw new ArgumentNullException(nameof(file));
        }
        if (File.Exists(file.FullName) == false)
        { 
            throw new FileNotFoundException("File could not be accessed",fileName: file.FullName);
        }

        using var stream = file.OpenRead();

        if (stream is null)
        {
            throw new ArgumentNullException(message:"Could not open the file and access the underlying file stream",paramName: nameof(file));
        }



        if (settings is null)
        {
            throw new ArgumentNullException(nameof(settings));
        }


        using (var reader = new StreamReader(stream))
        using (var jsonReader = new JsonTextReader(reader))
        {
            var ser = JsonSerializer.Create(settings);
            try
            {
                result = ser.Deserialize<T>(jsonReader);
            }
            catch { result = default; }
        }
        return result is not null;
    }
    /// <summary>
    /// Determines whether file contains valid json using the specified settings and reads it into the output.
    /// </summary>
    /// <typeparam name="T">Type to convert into</typeparam>
    /// <param name="file">The file.</param>
    /// <param name="result">The result.</param>
    /// <returns><c>true</c> if [is valid json] [the specified result]; otherwise, <c>false</c>.</returns>
    /// <exception cref="System.ArgumentNullException">file</exception>
    /// <exception cref="System.IO.FileNotFoundException">File could not be accessed</exception>
    public static bool IsValidJson<T>(this FileInfo file, [NotNullWhen(true)] out T? result)
    {
        if (file is null)
        {
            throw new ArgumentNullException(nameof(file));
        }
        if (File.Exists(file.FullName) == false)
        { 
            throw new FileNotFoundException("File could not be accessed",fileName: file.FullName);
        }

        JsonSerializerSettings settings =( JsonConvert.DefaultSettings?.Invoke()) ?? new JsonSerializerSettings() { DateTimeZoneHandling= DateTimeZoneHandling.Utc, DateFormatHandling= DateFormatHandling.IsoDateFormat };
        settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
        return file.IsValidJson<T>(settings,out result);


    }

} 
© www.soinside.com 2019 - 2024. All rights reserved.