TeuJson.Generator 3.2.0

dotnet add package TeuJson.Generator --version 3.2.0
                    
NuGet\Install-Package TeuJson.Generator -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.Generator" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TeuJson.Generator" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="TeuJson.Generator" />
                    
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.Generator --version 3.2.0
                    
#r "nuget: TeuJson.Generator, 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.Generator@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.Generator&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=TeuJson.Generator&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).

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TeuJson.Generator:

Package Downloads
Riateu

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 411 2/2/2024
3.1.2 404 4/20/2023
3.1.1 282 4/20/2023
3.1.0 294 4/20/2023
3.0.0 295 4/16/2023
2.2.2 298 4/10/2023
2.2.1 687 4/8/2023
2.2.0 314 4/7/2023
2.1.1 320 3/31/2023
2.1.0 327 3/21/2023
2.0.0 356 3/20/2023
1.5.0 353 3/18/2023
1.4.1 346 3/11/2023
1.4.0 342 3/11/2023
1.3.3 385 3/10/2023
1.3.2 367 3/9/2023
1.3.1 369 3/9/2023
1.3.0 369 3/9/2023
1.2.3 377 3/9/2023
1.2.2 368 3/9/2023
Loading failed