Ljson 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ljson --version 1.0.0                
NuGet\Install-Package Ljson -Version 1.0.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="Ljson" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ljson --version 1.0.0                
#r "nuget: Ljson, 1.0.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.
// Install Ljson as a Cake Addin
#addin nuget:?package=Ljson&version=1.0.0

// Install Ljson as a Cake Tool
#tool nuget:?package=Ljson&version=1.0.0                

A simple library for encoding objects into an LJSON string.

It is like json, but L (!) json. Try it.

The string contains only the object fields in the order specified by the user.

To separate the fields, a separate symbol is used at the beginning of the word and a separate one at the end (they must be different).

These characters are rare for human writing.

How to use

Simple object

Let us have this class:

internal class TestObject
{
    public string StrProp { get; set; } = "String Value";
    public int IntProp { get; set; } = 1123;
    public DateOnly Date { get; set; } = DateOnly.FromDateTime(DateTime.Now);

    public override string ToString()
    {
        return $"StrProp: {StrProp}\nIntProp: {IntProp}\nDate: {Date}";
    }
}

So we must have a corresponding converter. For example:

internal class TestObjectConverter : LjsonConvert<TestObject>
{
    public override string[] GetValues(TestObject obj)
    {
        return [
            obj.StrProp, obj.IntProp.ToString(),
            $"{obj.Date.Day}&{obj.Date.Month}"
            ];
    }

    public override void SetValues(TestObject obj, string[] values)
    {
        obj.StrProp = values[0];
        obj.IntProp = int.Parse(values[1]);
        var dateValues = values[2].Split("&");
        obj.Date = new DateOnly(DateTime.Now.Year, int.Parse(dateValues[1]), int.Parse(dateValues[0]));
    }
}

We must define the GetValues method that returns a string array with the data representing object's fields. The string array will be converted to ljson.

We must define the SetValues method to set our properties from string array got from ljson.

To launch it:

internal class Program
{

    static void Main(string[] args)
    {
        var testObject = new TestObject();
        var converter = new TestObjectConverter();
        var ljson = converter.ToLjson(testObject);
        Console.WriteLine(ljson + "\n"); // outputs: ╥String Value╛╥1123╛╥12&6╛

        var testObject2 = converter.FromLjson(ljson);
        Console.WriteLine(testObject2);
        /*
         StrProp: String Value
         IntProp: 1123
         Date: 12.06.2024
         */
    }
}

Private fields

If you have the class with private fields that must be included in ljson, you can use the Iljsonable interface.

internal class PrivateTestObject : ILjsonable
{
    private int IntField = 100;
    private string StringField = "str field";
    private string[] strings = ["str1", "str2", "str3", "str4"];

    public string[] GetValues()
    {
        var arr = new string[1 + 1 + strings.Length];
        arr[0] = IntField.ToString();
        arr[1] = StringField;
        for(int i = 0; i < strings.Length; i++)
        {
            arr[2 + i] = strings[i];
        }
        return arr;
    }

    public void SetValues(string[] values)
    {
        IntField = int.Parse(values[0]);
        StringField = values[1];
        strings = values[2..];
    }

    public override string ToString()
    {
        return $"String field: {StringField}\nInt: {IntField}\nStrings: {string.Join("[]", strings)}";
    }
}

And your converter can use its methods (in future versions its behaviour will be added to the package):

internal class PrivateTestObjectConverter : LjsonConvert<PrivateTestObject>
{
    public override string[] GetValues(PrivateTestObject obj)
    {
        return obj.GetValues();
    }

    public override void SetValues(PrivateTestObject obj, string[] values)
    {
        obj.SetValues(values);
    }
}

And use it:

namespace Ljson.Examples;

internal class Program
{

    static void Main(string[] args)
    {
        var privateObject = new PrivateTestObject();
        var privateConverter = new PrivateTestObjectConverter();
        var privateLjson = privateConverter.ToLjson(privateObject);
        Console.WriteLine(privateLjson + "\n"); // ╥100╛╥str field╛╥str1╛╥str2╛╥str3╛╥str4╛


        var privateObject2 = privateConverter.FromLjson(privateLjson);
        Console.WriteLine(privateObject2);

        /*
        String field: str field
        Int: 100
        Strings: str1[]str2[]str3[]str4
         */
    }
}

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 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 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 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  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.
  • .NETStandard 2.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.2.0 94 12/3/2024
1.1.2 106 9/30/2024
1.1.0 98 9/30/2024
1.0.0 106 6/12/2024