IonKiwi.Json - Yet another json parser

Posted on 2019-06-12

IonKiwi.Json

Yet another json parser
Features:
  • parse/write JSON content
  • parse/write ECMAScript like content
    • Unquoted property names
    • Single / multi-line comments
    • Trailing comma allowed for objects and arrays
    • Single quoted strings
    • Multi-line strings (by escaping new line characters)
    • Unicode CodePoint escape
    • Hexadecimal/octal/binary numbers
    • Numbers with leading or trailing decimal point
    • Positive infinity, negative infinity, NaN
    • Explicit plus sign for numbers
  • Support for C#/.NET Tuples (using Tuple Element Names)
  • async writing/parsing

Usage

Parsing json
  • parsing a json string synchronously
using (var reader = new StringReader(json)) {
  var value = JsonParser.ParseSync(new JsonReader(reader));
}

or

var value = JsonUtility.ParseSync(json);
  • parsing a json string asynchronously
using (var reader = new StringReader(json)) {
  var value = await JsonParser.Parsec(new JsonReader(reader));
}

or

var value = await JsonUtility.Parse(json);
  • parsing a json stream synchronously
using (var reader = new StreamReader(stream)) {
  var value = JsonParser.ParseSync(new JsonReader(reader));
}

or

var value = JsonUtility.ParseSync(stream);
  • parsing a json stream asynchronously
using (var reader = new StreamReader(stream)) {
  var value = await JsonParser.Parse(new JsonReader(reader));
}

or

var value = await JsonUtility.Parse(stream);
Writing json
  • serializing a value as json string synchronously
var sb = new StringBuilder();
using (var writer = new StringWriter(sb)) {
  var value = JsonWriter.SerializeSync(writer, value);
}
var json = sb.ToString();

or

var json = JsonUtility.SerializeSync(value);
  • serializing a value as json string asynchronously
var sb = new StringBuilder();
using (var writer = new StringWriter(sb)) {
  var value = await JsonWriter.Serialize(writer, value);
}
var json = sb.ToString();

or

var json = await JsonUtility.Serialize(value);
  • serializing a value to a stream synchronously
using (var writer = new StreamWriter(stream)) {
  JsonWriter.SerializeSync(writer, value);
}

or

JsonUtility.SerializeSync(stream, value);
  • serializing a value to a stream asynchronously
using (var writer = new StreamWriter(stream)) {
  await JsonWriter.Serialize(writer, value);
}

or

await JsonUtility.Serialize(stream, value);

Available on NuGet

Code

© 2018 - Ewout van der Linden - IonKiwi.nl