SharpYAJ 2.0.0

SharpYAJ - YetAnotherJSON Reader/Writer for C# is a .NET Standard 2.0 compatible library to serialize and deserialize JSON strings

Install-Package SharpYAJ -Version 2.0.0
dotnet add package SharpYAJ --version 2.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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&lt;string, object&gt;
  • Arrays are represented as IEnumerable&lt;object&gt;
  • 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&lt;string, object&gt;
  • 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

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&lt;string, object&gt;
  • Arrays are represented as IEnumerable&lt;object&gt;
  • 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&lt;string, object&gt;
  • 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

  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.0 166 1/4/2019
1.3.0 127 11/20/2018
1.2.1 124 11/17/2018
1.2.0 137 11/17/2018
1.1.0 177 10/12/2018
1.0.0 254 5/6/2018