INISerializer 1.1.0

dotnet add package INISerializer --version 1.1.0
NuGet\Install-Package INISerializer -Version 1.1.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="INISerializer" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add INISerializer --version 1.1.0
#r "nuget: INISerializer, 1.1.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 INISerializer as a Cake Addin
#addin nuget:?package=INISerializer&version=1.1.0

// Install INISerializer as a Cake Tool
#tool nuget:?package=INISerializer&version=1.1.0

[SKYNET] INISerializer

This is a library for Serialize - Deserialize objects and save them as ini settings files

Supported types

System.string
System.Net.IPAddress
System.Net.IPEndPoint
System.DateTime
System.TimeSpan
System.Enum
System.byte[]
System.Uri
System.Array
System.Version
System.Collections.Generic.List
System.Collections.Generic.Dictionary
System.Collections.Generic.KeyValuePair
System.Collections.Concurrent.ConcurrentDictionary
Primitive types(int, uint, long, decimal, bool, etc)

How to use

Use the attribute [INISection("Section name")] as section name<br />

[INISection("User Info")]
public string Name { get; set; }
[User Info]
Name = Hackerprod

Use the attribute [INIComment("Property description")] as property description<br />

[INISection("User Info")]
[INIComment("Person name")]
public string Name { get; set; }
[User Info]
# Person name
Name = Hackerprod

Use the attribute [INISection("Section name")] with IsArray for section array<br />

[INISection("Phone book", IsArray = true)]
public Dictionary<string, long> Phonebook { get; set; }
[Phone book]
Jhon = 19544818009
Aria = 18564418075

Code Example

Class implementation

namespace SKYNET
{
    public class Settings
    {
        // User Info Section
        [INISection("User Info")]
        public string Name { get; set; }

        [INISection("User Info")]
        public int Age { get; set; }

        [INISection("User Info")]
        public string Language { get; set; }

        [INISection("User Info")]
        public SexType Sex { get; set; }

        [INISection("User Info")]
        public double Height { get; set; } 

        [INISection("User Info")]
        public DateTime Birthday { get; set; }

        [INISection("User Info")]
        public bool Working { get; set; }

        [INISection("User Info")]
        public long SecureCode { get; set; }

        [INISection("User Info")]
        public object PersonalObject { get; set; }

        [INISection("User Info")]
        public byte[] Welcome { get; set; }

        // Work Info Section
        [INISection("Work Info")]
        [INIComment("IP address of the user at work")]
        public IPAddress IPAddress { get; set; }

        [INISection("Work Info")]
        public IPEndPoint EndPoint { get; set; }

        [INISection("Work Info")]
        public uint Salary { get; set; }

        [INISection("Work Info")]
        public List<string> Coworkers { get; set; }

        [INISection("Work Info")]
        public ConcurrentDictionary<string, int> Jobs { get; set; }

        [INISection("Work Info")]
        public TimeSpan YearsWorking { get; set; }

        [INISection("Work Info")]
        public Uri URL { get; set; }

        // Phone book Section
        [INISection("Phone book", IsArray = true)]
        public Dictionary<string, long> Phonebook { get; set; }

        // Phone book info Section
        [INISection("Phone book info")]
        public Version BookVersion { get; set; }

        [INISection("Phone book info")]
        public KeyValuePair<string, int> BookCategory { get; set; }
    }
   
    public enum SexType
    {
        Unknown,
        Male,
        Female
    }
}

Serializer implementation

Settings created = new Settings()
{
    Name = "Hackerprod",
    Age = 33,
    Language = "spanish",
    Birthday = new DateTime(1989, 04, 15),
    Sex = SexType.Male,
    Height = 1.72,
    Working = true,
    SecureCode = 76561198392279378,
    PersonalObject = null,
    Welcome = Encoding.Default.GetBytes("Welcome to INISerializer project"),
    IPAddress = IPAddress.Parse("192.168.1.20"),
    EndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.20"), 80),
    Salary = 4000,
    Coworkers = new List<string>() { "Tania", "Mercy", "Kati" },
    Jobs = new ConcurrentDictionary<string, int>(new Dictionary<string, int>() { { "Secretary", 2 }, { "Director", 1 }, { "Employees", 8 } }),
    YearsWorking = DateTime.Now - (new DateTime(2010, 06, 18)),
    URL = new Uri("https://github.com/Hackerprod"),
    Phonebook = new Dictionary<string, long>() { { "Daenerys", 17548081454 }, { "Jhon", 19544818009 }, { "Aria", 18564418075 } },
    BookVersion = new Version("1.0.8"),
    BookCategory = new KeyValuePair<string, int>("Contacts", 3)
};
string serializedSettings = INISerializer.Serialize(created);

Output

[User Info]
Name = Hackerprod
Age = 33
Language = spanish
Sex = Male
Height = 1,72
Birthday = 15/04/1989 12:00:00
Working = True
SecureCode = 76561198392279378
PersonalObject =
Welcome = 57656C636F6D6520746F20494E4953657269616C697A65722070726F6A656374

[Work Info]
# IP address of the user at work
IPAddress = 192.168.1.20
EndPoint = 192.168.1.20:80
Salary = 4000
Coworkers = Tania, Mercy, Kati
Jobs = [Employees, 8], [Director, 1], [Secretary, 2]
YearsWorking = 4369.12:46:30.9187699
URL = https://github.com/Hackerprod

[Phone book]
Daenerys = 17548081454
Jhon = 19544818009
Aria = 18564418075

[Phone book info]
BookVersion = 1.0.8
BookCategory = [Contacts, 3]

Deserializer implementation

Settings deserialized = INISerializer.Deserialize<Settings>(serializedSettings);

Console.WriteLine(
"Name = "            + deserialized.Name + Environment.NewLine +
"Age = "             + deserialized.Age + Environment.NewLine +
"Language = "        + deserialized.Language + Environment.NewLine +
"Birthday = "        + deserialized.Birthday + Environment.NewLine +
"Sex = "             + deserialized.Sex + Environment.NewLine +
"Height = "          + deserialized.Height + Environment.NewLine +
"Working = "         + deserialized.Working + Environment.NewLine +
"SecureCode = "      + deserialized.SecureCode + Environment.NewLine +
"PersonalObject = "  + deserialized.PersonalObject + Environment.NewLine +
"Welcome = "         + Encoding.Default.GetString(deserialized.Welcome) + Environment.NewLine +
"IPAddress = "       + deserialized.IPAddress + Environment.NewLine +
"EndPoint = "        + deserialized.EndPoint + Environment.NewLine +
"Salary = "          + deserialized.Salary + Environment.NewLine +
"Coworkers = "       + String.Join(", ", deserialized.Coworkers) + Environment.NewLine +
"Jobs = "            + String.Join(", ", deserialized.Jobs) + Environment.NewLine +
"YearsWorking = "    + deserialized.YearsWorking.TotalDays / 365 + Environment.NewLine +
"Uri = "             + deserialized.URL + Environment.NewLine +
"Phonebook = "       + String.Join(", ", deserialized.Phonebook) + Environment.NewLine +
"BookVersion = "     + deserialized.BookVersion + Environment.NewLine +
"BookCategory = "    + deserialized.BookCategory.Key + " : " + deserialized.BookCategory.Value
);

Output

Name = Hackerprod
Age = 33
Language = spanish
Birthday = 15/04/1989 12:00:00
Sex = Male
Height = 1,72
Working = True
SecureCode = 76561198392279378
PersonalObject =
Welcome = Welcome to INISerializer project
IPAddress = 192.168.1.20
EndPoint = 192.168.1.20:80
Salary = 4000
Coworkers = Tania, Mercy, Kati
Jobs = [Employees, 8], [Director, 1], [Secretary, 2]
YearsWorking = 11,971321376166
Uri = https://github.com/Hackerprod
Phonebook = [Daenerys, 17548081454], [Jhon, 19544818009], [Aria, 18564418075]
BookVersion = 1.0.8
BookCategory = Contacts : 3

Serialize and Deserialize to/from file

string fileName = Path.Combine("c:/", "Settings.ini");

// Serialize and save to file
INISerializer.SerializeToFile(settings, fileName);

// Deserialize to object from file
Settings settings = INISerializer.DeserializeFromFile<Settings>(fileName);

Debug Serialization and Deserialization errors

INISerializer.OnErrorMessage += INISerializer_OnErrorMessage;
private static void INISerializer_OnErrorMessage(object sender, string error)
{
    Console.WriteLine(error);
}

Github Repository https://github.com/Hackerprod/-SKYNET-INISerializer

Product Compatible and additional computed target framework versions.
.NET Framework 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 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has 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.1.0 426 6/4/2022
1.0.0 402 5/26/2022