SharpYAJ 2.0.0

dotnet add package SharpYAJ --version 2.0.0                
NuGet\Install-Package SharpYAJ -Version 2.0.0                
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="SharpYAJ" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpYAJ --version 2.0.0                
#r "nuget: SharpYAJ, 2.0.0"                
#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 SharpYAJ as a Cake Addin
#addin nuget:?package=SharpYAJ&version=2.0.0

// Install SharpYAJ as a Cake Tool
#tool nuget:?package=SharpYAJ&version=2.0.0                

SharpYAJ - YetAnotherJSON Reader/Writer for C#

SharpYAJ (pronounced as 'Sharp-Jay') is a .NET Standard 2.0 compatible library to serialize and deserialize JSON strings.

There's the class JavaScriptSerializer in .NET which does exactly this task, but unfortunately it's not yet included in .NET Standard any therefore only available when using .NET Framework. There would be Newtonsofts JSON implementation, but that library is way too big if you just want to read or write small JSON files, or if you just want simple .NET types instead of wrappers for each JSON type.

So, until the JavaScriptSerializer is defined in .NET Standard, this library can be used alternatively.

Reading

SharpYAJ deserializes objects the same way, JavaScriptSerializer does:

  • Objects are represented as Dictionary<string, object>
  • Arrays are represented as IEnumerable<object>
  • Primitives are represented as their corresponding .NET type (int, double, string, bool, null (object))
using SharpYAJ;

var myJSONString = "[1, 3, 3, 7, \"is\", true]";

object deserialized = YAJReader.ReadJSON(myJSONString);
//deserialized: IEnumerable<object> { 1, 3, 3, 7, "is", true }

Numbers

In JSON, numbers can be infinitely high. In SharpYAJ they are read as follows: The Markdown renderer on NuGet doesn't support some parts of this README. Please read it on Github

Trailing commas

JSON doesn't allow trailing commas in arrays or objects like JavaScript does (e. g. [1, 2, 3,]), therefore SharpYAJ also doesn't allow it by default. However if you want to parse such "invalid" JSON, you can enable that feature by compiling with flags ALLOW_TRAILING_ARRAY_COMMAS and ALLOW_TRAILING_OBJECT_COMMAS.

As NuGet packages are precompiled, you have to compile the package yourself to use this feature.

A list of all supported flags is at the end of this README.

Comments

JSON doesn't allow comments like programming languages do (e. g. [1, 2, /* 3, */ 4]), therefore SharpYAJ also doesn't allow it by default. However if you want to parse such "invalid" JSON, you can enable that feature by compiling with flags ALLOW_LINE_COMMENTS for //-comments and ALLOW_BLOCK_COMMENTS for /* */-comments.

As NuGet packages are precompiled, you have to compile the package yourself to use this feature.

A list of all supported flags is at the end of this README.

Using internal methods

Beside ReadJSON, YAJReader contains methods like ReadArray, ReadInt, ReadBool and so on. These methods are used internally. If you want to use these methods for whatever reason, you have to tell SharpYAJ to perform additional checks in these methods, as by default they omit checks done by the SharpYAJ-caller method. To annouce the usage of these methods, compile the library with the SHARE_INTERNAL_METHODS flag.

To be more performant, the reading is done by simply moving a cursor over the string, so that a new substring does not have to be created for each element. This string-shifting is done by the internal class StringView. This class is also only shown if you activate compiliation flag SHARE_INTERNAL_METHODS. So for using the internal methods, you have to create a StringView instance.

Writing

For writing objects SharpYAJ expects basically the same object types like it produced when reading a string:

  • Objects have to be IDictionary<string, object>
  • Arrays have to be IEnumerable
  • Allowed primitive types are
    • short
    • int
    • long
    • float
    • double
    • string
    • bool
    • null
using SharpYAJ;

var serialized = YAJWriter.WriteJSON(deserialized);
//serialized: "[1,3,3,7,\"is\",true]"

Indention / PrettyPrint

By default, YAJWriter doesn't print any spaces or line-breaks to separate the elements. If you want a pretty-printed JSON string, pass the indention flag to WriteJSON:

using SharpYAJ;

var serialized = YAJWriter.WriteJSON(deserialized, true);
/*serialized:
"[
	1,
	3,
	3,
	7,
	\"is\",
	true
]"
*/

To specify the separation and line-break char, they can also be passed to WriteJSON:

using SharpYAJ;

var serialized = YAJWriter.WriteJSON(deserialized, "~~~~", "\r\n");
/*serialized:
"[
~~~~1,
~~~~3,
~~~~3,
~~~~7,
~~~~\"is\",
~~~~true
]"
*/
Own pretty print implementation

Pretty printing is done using the internal class IndentWriter. If you want to implement pretty print yourself, create a subclass overriding the methods of IndentWriter and pass the instance to the WriteJSON method:

using SharpYAJ;

class MyIndentWriter : IndentWriter
{
	public override void Write(StringBuilder sb)
	{
		/* ... */
	}
}

var myIndentWriter = new MyIndentWriter();

var serialized = YAJWriter.WriteJSON(deserialized, myIndentWriter);

Compiler flags

Following compiler flags are supported by SharpYAJ: The Markdown renderer on NuGet doesn't support some parts of this README. Please read it on Github

License

SharpYAJ is licensed 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.
  • .NETStandard 2.0

    • No dependencies.

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
2.0.0 779 1/4/2019
1.3.0 672 11/20/2018
1.2.1 658 11/17/2018
1.2.0 679 11/17/2018
1.1.0 745 10/12/2018
1.0.0 904 5/6/2018