TeuJson 3.2.0

dotnet add package TeuJson --version 3.2.0
                    
NuGet\Install-Package TeuJson -Version 3.2.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="TeuJson" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TeuJson" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="TeuJson" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TeuJson --version 3.2.0
                    
#r "nuget: TeuJson, 3.2.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.
#:package TeuJson@3.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TeuJson&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=TeuJson&version=3.2.0
                    
Install as a Cake Tool

TeuJson

A Reflection-less and Lightweight Json Library using source generator.

Nuget

Installation

Install these two required packages.

dotnet add package TeuJson --version 3.1.2
dotnet add package TeuJson.Generator --version 3.1.2

Features

  • Serializers and deserializers.
  • 2 formatting options (Pretty, and Minimal).
  • Custom converters defined as functions.
  • Allows trailing commas.
  • Read and write as binary.

Usage

Creating a class with a Serializable.

// Unlike most libraries, TeuJson uses interfaces instead of an attribute on a type.
// This is much cleaner way to specify the type if it can serialized or deserialized.
public partial class Person : IDeserialize, ISerialize
{
    [Name("name")]
    public string Name { get; set; }
    public int Age { get; set; }
    [Ignore]
    public string Location { get; set; }
    [TeuObject]
    public string City;
}

// Then use it like this:
var person = new Person { 
  Name = "John Anthony",
  Age = 32,
  Location = "North Pole",
  City = "Santa's City"
};
var serialized = JsonConvert.Serialize(person);
JsonTextWriter.WriteToFile("person.json", person);

var johnJson = JsonTextReader.FromFile("person.json");
var john = JsonConvert.Deserialize<Person>(johnJson);

The output of the file will be:

{
  "name": "John Anthony",
  "Age": 32,
  "City": "Santa's City"
}

What it generates?

It generates the code like what you've expected, there might be a special cases with classes since they can have null values. All of Serializable classes will use a fully qualified name to instantiate themselves.

The reason why the methods are virtual is because the class might have a derived class which can also be a serializable to override those methods. If you don't want this, mark the class as sealed. Structs do not have inheritance, so it won't have a virtual method.

// Source Generated code
using TeuJson;

partial class Person
{
    public virtual void Deserialize(JsonObject @__obj)
    {
        Name = @__obj["name"];
        Age = @__obj["Age"];
        City = @__obj["City"];
    }
}

partial class Rect4
{
    public virtual JsonObject Serialize()
    {
        var __builder = new JsonObject();
        __builder["name"] = Name;
        __builder["Age"] = Age;
        __builder["City"] = City;
        return __builder;
    }
}

Custom Converters

You can create your own converters by defining a function inside of a static class. You will not necessarily needed if you have an access to that specific class or struct.

The way to declare it is differs from NET 6 and NET 7.

// Creating the converter
/* MyMathConverter.cs */
namespace Maths;

/* NET 6.0 and below */
/** public class MyMathConverter **/
public class MyMathConverter 
{
// Converters are named sensitive, it must follow the naming convetion in order to work.
// Writer = JsonValue ToJson(this <T> value);
// Reader = <T> To<T>(this JsonValue value);

    // Extensions are possible in NET 6, but not in NET 7 due to its limitation.
    public static JsonValue ToJson(Vector2 value) 
    {
        // Json object is similar to Dictionary.
        return new JsonObject 
        {
            ["x"] = value.X,
            ["y"] = value.Y
        };
    }

    public static Vector2 ToVector2(JsonValue value) 
    {
        // check if the json value is object
        if (value.IsObject) 
        {
            int x = value["x"];
            int y = value["y"];
            return new Vector2(x, y);
        }
        return Vector2.Zero;
    }
}
public sealed partial class Player : IDeserialize
{
    [Name("name")]
    public string Name { get; set; }
    /* NET 6 and below: Name must be fully qualified*/
    /** Name must be fully qualified **/
    /** [Custom("Maths.MyMathConverter")] **/
    [Custom<MyMathConverter>()]
    [Name("position")]
    public Vector2 Position { get; set;}
}

License

MIT License (Read 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 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  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.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TeuJson:

Package Downloads
Riateu

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 373 2/2/2024
3.1.2 417 4/20/2023
3.1.1 298 4/20/2023
3.1.0 299 4/20/2023
3.0.0 292 4/16/2023
2.2.2 304 4/10/2023
2.2.1 657 4/8/2023
2.2.0 301 4/7/2023
2.1.1 330 3/31/2023
2.1.0 331 3/21/2023
2.0.0 334 3/20/2023
1.5.0 344 3/18/2023
1.4.1 350 3/11/2023
1.4.0 357 3/11/2023
1.3.3 377 3/10/2023
1.3.2 351 3/9/2023
1.3.1 349 3/9/2023
1.3.0 352 3/9/2023
1.2.3 363 3/9/2023
1.2.2 336 3/9/2023
Loading failed