IonKiwi.Json
1.0.15
dotnet add package IonKiwi.Json --version 1.0.15
NuGet\Install-Package IonKiwi.Json -Version 1.0.15
<PackageReference Include="IonKiwi.Json" Version="1.0.15" />
paket add IonKiwi.Json --version 1.0.15
#r "nuget: IonKiwi.Json, 1.0.15"
// Install IonKiwi.Json as a Cake Addin #addin nuget:?package=IonKiwi.Json&version=1.0.15 // Install IonKiwi.Json as a Cake Tool #tool nuget:?package=IonKiwi.Json&version=1.0.15
Yet Another JSON parser
Features
parse/write JSON
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)
Automatic for tuples included in a object
[JsonObject]
private sealed class TupleHolder {
[JsonProperty]
public (bool a, int b) Value1 { get; set; }
}
json => { Value1: { a: true, b: 42 } }
Manual for top level tuples or when using generics
JsonWriter.Serialize(
output,
(true, 42),
JsonWriter.DefaultSettings.With(s => s.JsonWriteMode = JsonWriteMode.ECMAScript),
tupleNames: new string[] { "a", "b" });
=> { a: true, b: 42 }
Manual using TupleElementNamesAttribute
public class ClassContaingTupleMethod {
public (string value1, bool value2) MethodReturningTuple() {
return ("test", true);
}
}
string json = JsonUtility.Serialize(
new ClassContaingTupleMethod().MethodReturningTuple(),
tupleNames:
typeof(ClassContaingTupleMethod).GetMethod("MethodReturningTuple", BindingFlags.Instance | BindingFlags.Public)
.ReturnParameter.GetCustomAttribute<TupleElementNamesAttribute>().TransformNames.ToArray());
// json => {"value1":"test","value2":true}
Usage
Parsing json
parsing a json string synchronously
using (var reader = new StringReader(json)) {
var value = JsonParser.Parse<ObjectType>(new JsonReader(reader));
}
or
var value = JsonUtility.Parse<ObjectType>(json);
parsing a json string asynchronously
using (var reader = new StringReader(json)) {
var value = await JsonParser.ParseAsync<ObjectType>(new JsonReader(reader));
}
or
var value = await JsonUtility.ParseAsync<ObjectType>(json);
parsing a json stream synchronously
using (var reader = new StreamReader(stream)) {
var value = JsonParser.Parse<ObjectType>(new JsonReader(reader));
}
or
var value = JsonUtility.Parse<ObjectType>(stream);
parsing a json stream asynchronously
using (var reader = new StreamReader(stream)) {
var value = await JsonParser.ParseAsync<ObjectType>(new JsonReader(reader));
}
or
var value = await JsonUtility.ParseAsync<ObjectType>(stream);
Writing json
serializing a value as json string synchronously
var sb = new StringBuilder();
using (var writer = new StringWriter(sb)) {
var value = JsonWriter.Serialize(writer, value);
}
var json = sb.ToString();
or
var json = JsonUtility.Serialize(value);
serializing a value as json string asynchronously
var sb = new StringBuilder();
using (var writer = new StringWriter(sb)) {
var value = await JsonWriter.SerializeAsync(writer, value);
}
var json = sb.ToString();
or
var json = await JsonUtility.SerializeAsync(value);
serializing a value to a stream synchronously
using (var writer = new StreamWriter(stream)) {
JsonWriter.Serialize(writer, value);
}
or
JsonUtility.Serialize(stream, value);
serializing a value to a stream asynchronously
using (var writer = new StreamWriter(stream)) {
await JsonWriter.SerializeAsync(writer, value);
}
or
await JsonUtility.SerializeAsync(stream, value);
Annotation
Objects
use IonKiwi.Json.MetaData.JsonObjectAttribute & IonKiwi.Json.MetaData.JsonPropertyAttribute
[JsonObject]
public class Object1 {
[JsonProperty]
public string Property1 { get; set; }
}
Collections
use IonKiwi.Json.MetaData.JsonCollectionAttribute and implement IEnumerable<>
[JsonCollection]
public class Collection1<T> : IEnumerable<T> {
}
Dictionaries
use IonKiwi.Json.MetaData.JsonDictionaryAttribute and implement IDictionary<,>
[JsonDictionary]
public class Dictionary1<TKey, TValue> : IDictionary<TKey, TValue> {
}
External/existing annotation
use existing DataContract/DataMember attributes (System.Runtime.Serialization)
IonKiwi.Json.Utilities.DataContractSupport.Register();
use existing Newtonsoft attributes
IonKiwi.Json.Newtonsoft.NewtonsoftSupport.Register();
Custom constructors
use IonKiwi.Json.MetaData.JsonConstructorAttribute & IonKiwi.Json.MetaData.JsonParameterAttribute
[JsonObject]
private class Object2 {
[JsonConstructor]
public Object2([JsonParameter("property1")]bool parameter1, int property2) {
Property1 = parameter1;
Property2 = property2;
}
[JsonProperty(Name = "property1")]
public bool Property1 { get; }
[JsonProperty(Name = "property2", Required = false)]
public int Property2 { get; }
[JsonProperty]
public int Property3 { get; }
}
For non required properties, the default value will be used. You can declare multiple [JsonConstructor] constructors, the one with the most available parameters will be called.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 | netcoreapp3.1 is compatible. |
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.7.2
- No dependencies.
-
net6.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 |
---|---|---|
1.0.15 | 391 | 12/27/2021 |
1.0.14 | 551 | 6/28/2020 |
1.0.13 | 474 | 6/17/2020 |
1.0.12 | 454 | 4/18/2020 |
1.0.11 | 499 | 3/5/2020 |
1.0.10 | 513 | 11/23/2019 |
1.0.9.6 | 561 | 10/5/2019 |
1.0.9.3 | 475 | 10/5/2019 |
1.0.9.2 | 472 | 10/5/2019 |
1.0.8 | 502 | 7/15/2019 |
1.0.7 | 526 | 6/23/2019 |
1.0.6 | 511 | 6/23/2019 |
1.0.5 | 506 | 6/17/2019 |
1.0.4 | 527 | 6/12/2019 |
1.0.3 | 544 | 6/11/2019 |
1.0.1 | 526 | 6/11/2019 |
1.0.0 | 615 | 6/11/2019 |