G9JSONHandler 2.0.3.1

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

// Install G9JSONHandler as a Cake Tool
#tool nuget:?package=G9JSONHandler&version=2.0.3.1

G9TM G9JSONHandler

NuGet version (G9JSONHandler) Azure DevOps Pipeline Build Status Github Repository

G9JSONHandler is a pretty small library for working with JSON.

Sample Object

using G9JSONHandler;
using G9JSONHandler.Attributes;

class TestObject
{
    [G9AttrJsonComment("Custom JSON Comment")]
    public string Name = ".NET";
    public ConsoleColor Color = ConsoleColor.DarkMagenta;
    [G9AttrJsonStoreEnumAsString]
    public ConsoleColor Color2 = ConsoleColor.DarkGreen;
    [G9AttrJsonMemberCustomName("MyRegisterDateTime")]
    public DateTime RegisterDateTime = new(1990, 9, 1);
    public TimeSpan Time = new(9, 9, 9);
    public string[] Array = { "Item 1", "Item 2", "Item 3" };
    public Dictionary<string, string> Dictionary = new()
        { { "Key 1", "Value 1" }, { "Key 2", "Value 2" }, { "Key 3", "Value 3" } };
    [G9AttrJsonIgnoreMember] 
    public string Address = "...";
}

Object To JSON

TestObject testObject = new TestObject();
// Unformatted JSON
string unformattedJson = testObject.G9ObjectToJson();
// Formatted JSON
string formattedJson = testObject.G9ObjectToJson(true);
Result
// Unformatted JSON Result
{"#__Comment0__#":"Custom JSON Comment","Name":".NET","Color":5,"Color2":"DarkGreen","MyRegisterDateTime":"09/01/1990 00:00:00","Time":"09:09:09","Array":["Item 1","Item 2","Item 3"],"Dictionary":{"Key 1":"Value 1","Key 2":"Value 2","Key 3":"Value 3"}}

// Formatted JSON Result
{
	"#__Comment0__#": "Custom JSON Comment",
	"Name": ".NET",
	"Color": 5,
	"Color2": "DarkGreen",
	"MyRegisterDateTime": "09/01/1990 00:00:00",
	"Time": "09:09:09",
	"Array": 
	[
		"Item 1",
		"Item 2",
		"Item 3"
	],
	"Dictionary": 
	{
		"Key 1": "Value 1",
		"Key 2": "Value 2",
		"Key 3": "Value 3"
	}
}

JSON To Object

// Object To JSON
// string formattedJson = testObject.G9ObjectToJson(true);

// JSON To Object
TestObject newObject = formattedJson.G9JsonToObject<TestObject>();
Console.WriteLine(newObject.Name); // .NET
Console.WriteLine(newObject.Color); // DarkMagenta
Console.WriteLine(newObject.Array.Length); // 3
Console.WriteLine(newObject.Dictionary.Count); // 3
Console.WriteLine(newObject.Dictionary["Key 2"]); // Value 2

JSON To Object (with mismatching)

// Considered a situation where the JSON data has some mismatching value with the primary data type.
// For example, the primary data type is:
public class G9DtSmallSampleClass
{
    public string Name;
    public int Age;
    public ConsoleColor Color;
}

// The json data is:
var json = "{\"Name\":32,\"Age\":\"Iman\",\"Color\":5}";

// Default process:
// var newObject = formattedJson.G9JsonToObject<G9DtSmallSampleClass>();
// Note: Above process throws an exception like this:
// ### An exception occurred when the parser tried to parse the value 'Iman' for member 'Age' in type '....G9DtSmallSampleClass'.###
// But by setting the parameter 'ignoreMismatching,' it can specify that if in the parsing process a mismatch occurs, the exception (mismatch) is ignored.
var newObject = formattedJson.G9JsonToObject<G9DtSmallSampleClass>(true);
newObject.Name; // 32
newObject.Color; // DarkMagenta
newObject.Age; // 0 - default(int)

Attributes

  • G9AttrJsonComment
    • This attribute enables you to write several comments (notes) for each member in JSON.
      • Note: Indeed, JSON has no option for comments (notes). But this library considers two ways for that.
        1. Standard mode considers a custom member that consists of a key and value like the usual member item ("__ #CommentN __#": "Comment Data") and saves the comment note there.
        [G9AttrJsonComment("Custom JSON Comment")] public string Name = ".NET";
        // {
        //  "#__Comment0__#": "Custom JSON Comment",
        //  "Name": ".NET",
        // ...
        
        1. Nonstandard mode saves comments notes between two signs ("/* Comment Data /*") like JavaScript.
        [G9AttrJsonComment("Custom JSON Comment", true)] public string Name = ".NET";
        // {
        //  /* Custom JSON Comment */
        //  "Name": ".NET",
        // ...
        
      • Note: This attribute can use several times for a member.
  • G9AttrJsonStoreEnumAsString
    • This attribute enables you to store an Enum object as a string value in JSON (By default, an Enum object storing as a number).
  • G9AttrJsonMemberCustomName
    • This attribute enables you to choose a custom name for a member for storing in JSON.
      • Note: At parsing time (JSON to object), the parser can recognize and pair the member automatically.
  • G9AttrJsonIgnoreMember
    • This attribute enables you to ignore a member for storing in JSON.
  • G9AttrJsonMemberCustomParser
    • This attribute enables you to implement the custom parsing process for (String to Json, Json to String, Both of them).
    // A class that consists of custom parser methods.
    public class CustomParser
    {
        // Custom parser for parsing the object to string.
        // Note: The specified method must have two parameters (the first parameter is 'string' and the second one is 'G9IMemberGetter');
        // in continuation, it must return an object value that is parsed from the string value.
        public string ObjectToString(object objectForParsing, G9IMemberGetter accessToObjectMember)
        {
            if (accessToObjectMember.MemberType == typeof(CustomChildObject))
                return ((CustomChildObject)objectForParsing).Number1 + "-" +
                      ((CustomChildObject)objectForParsing).Number2 +
                      "-" + ((CustomChildObject)objectForParsing).Number3;
            return default;
        }
    
        // Custom parser for parsing the string to object.
        // Note: The specified method must have two parameters (the first parameter is 'object' and the second one is 'G9IMemberGetter');
        // in continuation, it must return a string value that is parsed from the object value.
        public object StringToObject(string stringForParsing, G9IMemberGetter accessToObjectMember)
        {
            if (accessToObjectMember.MemberType != typeof(CustomChildObject))
                return default;
            var numberData = stringForParsing.Split('-').Select(int.Parse).ToArray();
            return new CustomChildObject
            {
                Number1 = numberData[0],
                Number2 = numberData[1],
                Number3 = numberData[2]
            };
        }
    }
    
    // Custom object
    public class CustomObject
    {
        // This attribute has several overloads.
        // The popular way (used below), for use, must specify the type of custom parse class in the first parameter,
        // the second parameter specifies the string to object method name, and the last one specifies the object to string method name.
        [G9AttrJsonMemberCustomParser(typeof(CustomParser), nameof(CustomParser.StringToObject),
            nameof(CustomParser.ObjectToString))]
        public CustomChildObject CustomChild = new();
    }
    
    // Custom child object
    public class CustomChildObject
    {
        public int Number1 = 9;
        public int Number2 = 8;
        public int Number3 = 7;
    }
    
  • G9AttrJsonMemberEncryption
    • This attribute enables you to add automated encrypting and decrypting processes for a member value.
    • Note: The specified member must have a convertible value to the string type.
    • Note: The priority of executing this attribute is higher than the others.
    • Note: If your member data type is complex, or you need to implement the custom encryption process, you can implement a custom (encryption/decryption) process with the attribute 'G9AttrJsonMemberCustomParser'.
    // A class that consists of members with the attribute 'G9AttrJsonMemberEncryption'.
    // This attribute has several overloads.
    public class G9DtSampleClassForEncryptionDecryption
    {
        // With standard keys and default config
        [G9AttrJsonMemberEncryption("G-JaNdRgUkXp2s5v", "3t6w9z$C&F)J@NcR")]
        public string User = "G9TM";
    
        // With custom nonstandard keys and custom config
        [G9AttrJsonMemberEncryption("MyCustomKey", "MyCustomIV", PaddingMode.ANSIX923, CipherMode.CFB, enableAutoFixKeySize: true)]
        public string Password = "1990";
    
        // With custom nonstandard keys and custom config
        [G9AttrJsonMemberEncryption("MyCustomKey", "MyCustomIV", PaddingMode.ISO10126, CipherMode.ECB, enableAutoFixKeySize: true)]
        public DateTime Expire = DateTime.Now;
    }
    
    var objectWithEncryptionAttr = new G9DtSampleClassForEncryptionDecryption();
    
    // Encryption value
    var jsonData = objectWithEncryptionAttr.G9ObjectToJson();
    // Result
    //{
    //  "User": "fESJe1TvMr00Q7BKTwVadg==",
    //  "Password": "sWNkdxQ=",
    //  "Expire": "C/WjS9oA+FRLw3myST4EowLiM22tTidXoG7hgJy3ZHo="
    //}
    var objectData = jsonData.G9JsonToObject<G9DtSampleClassForEncryptionDecryption>();
    objectData.User; // "G9TM"
    objectData.Password; // "1990"
    
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 is compatible. 
.NET Framework net35 is compatible.  net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on G9JSONHandler:

Package Downloads
G9ConfigManagement

Effective .NET library designed for working with and managing configs; has many useful features. This module provides a flexible framework that is pretty easy to use and straightforward. On the other hand, it has many functional attributes for making a tremendous and powerful config like BindabaleMember, Comment, Encryption, Required, Ordering, CustomName, Ignoring, CustomParser, etc.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.2 704 9/25/2022
3.0.1.1 659 9/23/2022
3.0.1 658 9/21/2022
3.0.0.1 379 9/21/2022
3.0.0 385 9/20/2022
2.0.5.5 378 9/11/2022
2.0.5.4 367 9/7/2022
2.0.5.3 363 8/31/2022
2.0.5.2 371 8/28/2022
2.0.5.1 352 8/28/2022
2.0.4.2 370 8/27/2022
2.0.3.1 372 8/25/2022
2.0.2 355 8/20/2022
2.0.1.3 373 8/18/2022
2.0.1.2 355 8/17/2022
2.0.1.1 360 8/17/2022
2.0.1 369 8/12/2022
2.0.0.5 368 8/11/2022
2.0.0.4 349 8/11/2022
2.0.0.3 360 8/11/2022
2.0.0.2 368 8/11/2022
2.0.0 373 8/10/2022
1.2.0 384 8/9/2022
1.0.0.1 381 8/8/2022

-- Implemented G9AttrJsonMemberEncryptionAttribute.
-- Implemented ignore mismatching option on parsing.
-- Redesigned the structure of parsing (Performance).
-- Completed tests