CypherPotato.LightJson
0.12.0
See the version list below for details.
dotnet add package CypherPotato.LightJson --version 0.12.0
NuGet\Install-Package CypherPotato.LightJson -Version 0.12.0
<PackageReference Include="CypherPotato.LightJson" Version="0.12.0" />
paket add CypherPotato.LightJson --version 0.12.0
#r "nuget: CypherPotato.LightJson, 0.12.0"
// Install CypherPotato.LightJson as a Cake Addin #addin nuget:?package=CypherPotato.LightJson&version=0.12.0 // Install CypherPotato.LightJson as a Cake Tool #tool nuget:?package=CypherPotato.LightJson&version=0.12.0
LightJson
This project was based in the awesome work of LightJson, originally made by Marcos Lopez C.
This fork includes some personal tweaks. It is a JSON library focused on not using reflection, where object mapping is made manually and requires mapping to serialize/deserialize typed JSON messages.
It also does not use Source Generators to read or write messages, which makes it possible to build code with bflat or AOT-Compilation without source generation.
Almost everything in this class is inherited from the main project mentioned above, with new features:
- Unlike the original project, a
JsonValue
does not contain anyAs[Type]
properties, but has a method for each JSON type, likeGetString()
or evenGetNumber()
, and the main difference is that you cannot get an implicit value of what theJsonValue
is. For example, you cannot readJsonValue.GetBoolean()
if the stored value is a string, even if it's value is"true"
or0
. - All functions that return an object converted from a JsonValue, such as
JsonValue.GetString()
for example, do not return nullable values. You can check for nullable JSON values usingJsonValue.MaybeNull()
. Null values would throw an exception if not used withMaybeNull()
. - Added
JsonOptions
, which contains:PropertyNameCaseInsensitive
, which indicates whether a property's name uses a case-insensitive comparison when getting values.SerializeFields
, gets or sets whether theJsonValue.Serialize
should serialize fields or not.Converters
, which aims to manage JSON converters.NamingPolicy
, which transforms the property name of a JSON object on the JSON output.WriteIndented
, which sets whether the JSON serializer should write indentend, pretty formatted, output.SerializationFlags
, which allows to passJsonSerializationFlags
flags to the JSON serializer/deserializer.ThrowOnDuplicateObjectKeys
which allows to throw an exception on duplicate key names. This property was enabled by defaut in order versions and original project fork, but disabled since v0.9.
- Experimental JSON5 support with
SerializationFlags.All
. - Undefined values, as it is, values which aren't defined or does not exist in the parent object/array, will come with
JsonValueType.Undefined
type instead ofJsonValueType.Null
. - This projects targets .NET 6 and above.
- Experimental support for including the JSON value path into error messages.
Serialize and deserialize data
All serialized or deserialized information results in the JsonValue
structure. From this object, you can manipulate the JSON document.
In the examples below, we will show how serialization and deserialization works.
// serialize primitive values
json = new JsonValue("hello").ToString();
Console.WriteLine(json); // "hello"
// serialize complex objects
json = JsonValue.Serialize(new { prop1 = "hello", prop2 = "world" }).ToString();
Console.WriteLine(json); // {"prop1":"hello","prop2":"world"}
// for custom types, an converter must be defined in the JsonOptions.Converters
json = JsonValue.Serialize(Guid.NewGuid(), new JsonOptions()).ToString();
Console.WriteLine(json); // "9d282aa8-9385-4158-a094-55a01a39feae"
// deserialize primitive values
json = """
{
"number": 12.52,
"name": "John Lennon",
"arrayOfInts": [ 20, 30, 40 ],
"object": {
"guid": "9d282aa8-9385-4158-a094-55a01a39feae"
}
}
""";
var objJson = JsonValue.Deserialize(json);
// implicitly converts the JsonValue into an JsonObject when acessing
// through key value
double objNumber = objJson["number"].GetNumber();
// MaybeNull() indicates that the value at $.name can be null
string? name = objJson["name"].MaybeNull()?.GetString();
// gets $.arrayOfInts[1] as integer. it must be an non-null number
int intAtIndex1 = objJson["arrayOfInts"][1].GetInteger();
// explicitly gets an Guid from $.object.guid
Guid convertedValue = objJson["object"]["guid"].Get<Guid>();
JSON converters
Here's an example of an System.DateTime
converter, which serializes and deserializes values into it:
static void Main(string[] args)
{
JsonOptions.Default.Converters.Add(new DatetimeMapper());
string json = JsonValue.Serialize(DateTime.Now).ToString();
Console.WriteLine(json);
}
public class DatetimeMapper : JsonConverter
{
public override Boolean CanSerialize(Type obj)
{
return obj == typeof(DateTime);
}
public override Object Deserialize(JsonValue value, Type requestedType)
{
return DateTime.Parse(value.GetString());
}
public override JsonValue Serialize(Object value)
{
DateTime t = (DateTime)value;
return new JsonValue(t.ToString("s"));
}
}
Also, these converters are defined by default:
- DictionaryConverter, which converts
IDictionary<string, object?>
into an JsonObject, and vice-versa. - GuidConverter, which converts
System.Guid
into an string, and vice-versa. - EnumConverter which converts an enum value into it's string representation, and vice versa, enabled through
EnumConverter.EnumToString
. - DatetimeConverter which converts
System.DateTime
into string, and vice-versa, using the formatDatetimeConverter.Format
. - DateOnlyConverter which converts
System.DateOnly
into string, and vice-versa, using the formatDateOnlyConverter.Format
. - TimeOnlyConverter which converts
System.TimeOnly
into string, and vice-versa. - TimeSpanConverter which converts
System.TimeSpan
into string, and vice-versa. - CharConverter which converts
System.Char
into string, and vice-versa. - DecimalConverter which converts
System.Decimal
into an double number, and vice-versa.
Fluent syntax for retrieving items
string json = """
{
"foobar": "hello",
"bazdaz": null,
"duzkaz": [
"foo",
"bar",
"daz"
],
"user": {
"name": "John McAffee", "age": 52
}
}
""";
var obj = JsonValue.Deserialize(json);
// $.foobar must be present, non null and carry an string value.
string stringValue = obj["foobar"].GetString();
// $.bazdaz can be null or undefined, but if not, it must be an string.
string? optionalValue = obj["bazdaz"].MaybeNull()?.GetString();
// $.duzkaz must be present, non null, be an json array and every children on it
// must be an string value.
string[] arrayItems = obj["duzkaz"].GetJsonArray().Select(i => i.GetString()).ToArray();
// $.user must be present, non null, and must be converted to the User type, which it's converter
// is defined on JsonOptions.Converters.
User user = obj["user"].Get<User>();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CypherPotato.LightJson:
Package | Downloads |
---|---|
Sisk.JsonRpc
This package provides an JSON-RPC 2.0 interface for Sisk projects. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.13.0 | 76 | 12/16/2024 |
0.12.0 | 75 | 12/11/2024 |
0.11.0 | 104 | 11/11/2024 |
0.10.7 | 81 | 10/25/2024 |
0.10.6 | 103 | 10/10/2024 |
0.10.5 | 103 | 9/30/2024 |
0.10.4 | 97 | 9/26/2024 |
0.10.3 | 101 | 9/26/2024 |
0.10.1 | 119 | 8/30/2024 |
0.10.0-beta4 | 89 | 8/27/2024 |
0.10.0-beta3 | 105 | 8/26/2024 |
0.10.0-beta1 | 118 | 8/23/2024 |
0.9.3 | 128 | 7/18/2024 |
0.9.2 | 127 | 7/3/2024 |
0.9.1 | 120 | 7/1/2024 |
0.9.0 | 116 | 5/28/2024 |
0.9.0-beta3 | 102 | 5/14/2024 |
0.9.0-beta2 | 114 | 5/4/2024 |
0.9.0-beta1 | 64 | 5/2/2024 |
0.8.0 | 129 | 4/4/2024 |
0.7.0 | 121 | 3/24/2024 |
0.6.0 | 144 | 1/18/2024 |
0.5.6 | 205 | 12/6/2023 |
0.5.5 | 155 | 11/30/2023 |