StoneCo.JsonStream 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package StoneCo.JsonStream --version 1.0.6
NuGet\Install-Package StoneCo.JsonStream -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StoneCo.JsonStream" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StoneCo.JsonStream --version 1.0.6
#r "nuget: StoneCo.JsonStream, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StoneCo.JsonStream as a Cake Addin
#addin nuget:?package=StoneCo.JsonStream&version=1.0.6

// Install StoneCo.JsonStream as a Cake Tool
#tool nuget:?package=StoneCo.JsonStream&version=1.0.6

StoneCo.JsonStream

JsonStream is a thread-safe dotNet Standard Class Library to make easy to read and to write json structures from/to a stream.

Dependences

  1. NETStandard.Library v2.0.0
  2. Newtonsoft.Json v11.0.2

Introduction

This class library was built to have a high performance when is reading or writing sequential files.

If you are reading or writing a file in sequential mode (one object after another) prefer to use the optimized constructor (that where you pass the file name).

If you are reading or writing another kind of stream like a MemoryStream or NetworkStream, you can use the other constructor passing this stream.

If you used the optimized constructor to instantiate the JsonStream, you can't use the async methods.

How it works?

Before write, JsonStream calculates the length of the buffer and writes this buffer length into the stream before writes the buffer. With default configuration, each buffer uses 8 bytes to describe his length.

When reading, JsonStream firstly reads the length of the next buffer and than reads the next n bytes of the buffer. When reader reaches the end of the stream, the method returns the default value of the type.

When the Dispose method is called, the JsonStream calls the Dispose method of the given stream or the inner stream if you used the optimized constructor.

About the constructors

When you use the optimized constructor (the one with the filePath argument), the class will create internally one FileStream. See the arguments used in the FileStream constructor depending of the choosed mode.

ReadOnly
  • FileMode - Open
  • FileAccess - Read
  • FileShare - ReadWrite
  • FileOptions - SequentialScan
WriteOnly
  • FileMode - Append
  • FileAccess - Write
  • FileShare - Read
  • FileOptions - SequentialScan
ReadAndWrite
  • FileMode - OpenOrCreate
  • FileAccess - ReadWrite
  • FileShare - ReadWrite
  • FileOptions - RandomAccess

Default Values

DEFAULT_BUFFER_SIZE = 524288 bytes

The buffer size passed to the FileStream constructor.

DEFAULT_DOCUMENT_SIZE_LENGTH = 8 bytes

The length of the document size descriptor as explained above.

Examples

Base code


class Human
{
	public int Age { get; set; }
	
	public string Name { get; set; }
	
	public Gender Gender { get; set; }
}

enum Gender
{
	Male,
	Female
}

Writing an object


// Choose the WriteOnly mode and inform the file path.
using (IJsonStream writeJsonStream = new JsonStream(Modes.WriteOnly, "objects.json"))
{
	Human human = new Human
	{
		Age = 5,
		Gender = Gender.Female,
		Name = "Stone"
	};
	
	writeJsonStream.WriteObject(human);                    
}

Reading the whole file


// Choose the ReadOnly mode and inform the file path.
using (IJsonStream readJsonStream = new JsonStream(Modes.ReadOnly, "objects.json"))
{
	Human human;
	while ((human = readJsonStream.ReadObject<Human>()) != null)
	{
		Console.WriteLine($"Name {human.Name}, Gender: {human.Gender}, Age: {human.Age}");
	}	
}


Using the constructor receiving the stream


IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

NetworkStream networkStream = new NetworkStream(socket);
using (IJsonStream jsonStream = new JsonStream(networkStream))
{
	jsonStream.WriteObjectAsync(human);
}

License

This library is distributed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.17 1,304 6/1/2018
1.0.16 894 5/30/2018
1.0.15 884 5/28/2018
1.0.14 847 5/24/2018
1.0.13 863 5/22/2018
1.0.12 849 5/22/2018
1.0.11 944 5/16/2018
1.0.10 901 5/15/2018
1.0.9 791 5/15/2018
1.0.8 930 5/14/2018
1.0.7 943 5/4/2018
1.0.6 996 4/30/2018
1.0.5 903 4/30/2018

Publishing as OpenSource.