Plinth.Serialization 1.8.0

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

README

Plinth.Serialization

Serialization utilities for JSON and XML

Provides utilities for JSON and XML serialization with sensible defaults and custom converters.

Components

  • JsonUtil - Wrapper over Newtonsoft.Json with Plinth standard JSON serialization
  • XmlUtil - Shortcut methods for XML serialization
  • JsonNet Converters - Custom converters and contract resolvers for Newtonsoft.Json
  • UtcDateTimeConverter - System.ComponentModel.DateTimeConverter for ensuring all deserialized dates are UTC

JsonUtil

JsonUtil provides a simplified API for JSON serialization with Plinth's default settings:

  • Ignores null values during serialization
  • Handles nullable enums as strings
  • Parses floating-point numbers as decimals for precision

Basic Serialization

using Plinth.Serialization;

public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string? Email { get; set; }
}

var user = new User { Id = Guid.NewGuid(), Name = "John Doe" };

// Serialize to JSON string
var json = JsonUtil.SerializeObject(user);
// Result: {"Id":"...","Name":"John Doe"}
// Note: Email is null and not included in output

// Deserialize from JSON string
var deserializedUser = JsonUtil.DeserializeObject<User>(json);

File Operations

// Deserialize from file
var config = JsonUtil.DeserializeObjectFromFile<AppConfig>("config.json");

// Try deserialize with error handling
if (JsonUtil.TryDeserializeObjectFromFile<AppConfig>("config.json", out var result))
{
    // Use result
}
else
{
    // Handle failure
}

Custom Settings

You can customize serialization settings for specific calls:

var json = JsonUtil.SerializeObject(user, settings =>
{
    settings.Formatting = Newtonsoft.Json.Formatting.Indented;
    settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
});

Deep Cloning

// Create a deep clone using JSON serialization
var userCopy = JsonUtil.DeepClone(user);

Converting Deserialized Objects

Useful when working with dynamic JSON:

// Convert deserialized object to specific type
var obj = JsonUtil.DeserializeObject<object>(json);
var userId = JsonUtil.ConvertDeserializedObject<Guid>(obj, Guid.Empty);

// Try convert with null check
if (JsonUtil.TryConvertDeserializedObject<Guid>(obj, out var id))
{
    // Use id
}

XmlUtil

XmlUtil provides utilities for XML serialization using .NET's XmlSerializer.

Basic Serialization

using Plinth.Serialization;

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Year { get; set; }
}

var book = new Book
{
    Title = "Sample Book",
    Author = "John Doe",
    Year = 2024
};

// Serialize to XML string with default settings (includes namespaces, declaration, indented)
var xml = XmlUtil.Serialize(book);

// Serialize with custom settings
var xml = XmlUtil.Serialize(book,
    XmlUtil.SerializerSettings.OmitNamespaces |
    XmlUtil.SerializerSettings.OmitXmlDeclaration);

Serializer Settings

Available flags:

  • Default - Includes namespaces, XML declaration, and indentation
  • OmitNamespaces - Excludes XML namespaces from output
  • OmitXmlDeclaration - Excludes <?xml version="1.0"?> declaration
  • NoIndent - Outputs XML without indentation

Deserialization

// Deserialize from XML string
var book = XmlUtil.Deserialize<Book>(xml);

// Deserialize from file
var book = XmlUtil.DeserializeFromFile<Book>("book.xml");

// Deserialize from stream
using var stream = File.OpenRead("book.xml");
var book = XmlUtil.DeserializeFromStream<Book>(stream);

Stream Serialization

// Serialize to stream (useful for large files or network streams)
using var stream = File.Create("output.xml");
XmlUtil.Serialize(book, stream, XmlUtil.SerializerSettings.Default);

XML Validation

Validate XML against XSD schemas:

var xml = File.ReadAllText("data.xml");
var errors = XmlUtil.Validate(xml, "schema1.xsd", "schema2.xsd");

if (errors.Count == 0)
{
    Console.WriteLine("XML is valid");
}
else
{
    foreach (var error in errors)
    {
        Console.WriteLine($"Validation error: {error}");
    }
}

Custom Converters

JsonNetStringNullableEnumConverter

Automatically included in JsonUtil, this converter serializes nullable enums as strings instead of integers.

public enum Status { Active, Inactive, Pending }

public class Item
{
    public Status? CurrentStatus { get; set; }
}

// Serializes as: {"CurrentStatus":"Active"} instead of {"CurrentStatus":0}

Contract Resolvers

CamelCase Property Names:

using Plinth.Serialization.JsonNet;

var settings = new JsonSerializerSettings
{
    ContractResolver = new JsonNetCamelCasePropertyNamesContractResolver()
};

// Properties like "FirstName" will serialize as "firstName"

Underscore Property Names:

using Plinth.Serialization.JsonNet;

var settings = new JsonSerializerSettings
{
    ContractResolver = new JsonNetUnderscorePropertyNamesContractResolver()
};

// Properties like "FirstName" will serialize as "first_name"

Best Practices

  1. Use JsonUtil for Consistency - JsonUtil applies Plinth's standard settings across your application
  2. Null Handling - Remember that JsonUtil ignores null values by default to reduce payload size
  3. XML Namespaces - Use OmitNamespaces when namespaces aren't required for cleaner XML
  4. File Operations - Use TryDeserialize methods for better error handling when reading files
  5. Deep Cloning - Only use DeepClone for simple objects; complex object graphs may require custom cloning logic
Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on Plinth.Serialization:

Package Downloads
Plinth.Security

Security, Cryptography, and Token Utilities

Plinth.HttpApiClient

HTTP Api Client framework built on MS HttpClient

Plinth.AspNetCore

Plinth ASP.NET Core Services Utilities

Plinth.Database.MSSql

Framework for database transactions and connection to MS Sql Server

Plinth.Logging.NLog

Plinth NLog Utilities

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.8.0 614 11/13/2025
1.8.0-b211.72089fd9 241 11/12/2025
1.7.4 3,051 8/6/2025
1.7.3 486 8/2/2025
1.7.2 4,661 3/16/2025
1.7.1 1,590 12/12/2024
1.7.0 4,769 11/12/2024
1.6.6 1,452 11/8/2024
1.6.5 5,149 8/31/2024
1.6.4 1,005 8/2/2024
1.6.3 11,685 5/15/2024
1.6.2 1,183 2/16/2024
1.6.1 9,529 1/5/2024
1.6.0 1,794 11/30/2023
1.5.10-b186.aca976b4 103 11/30/2023
1.5.9 5,155 11/29/2023
1.5.9-b174.64153841 496 11/23/2023
1.5.9-b172.dfc6e7bd 103 11/17/2023
1.5.9-b171.4e2b92e2 161 11/4/2023
1.5.8 16,779 10/23/2023
1.5.7 10,436 7/31/2023
1.5.6 15,401 7/13/2023
1.5.5 1,097 6/29/2023
1.5.4 2,666 3/7/2023
1.5.3 1,849 3/3/2023
1.5.2 2,750 1/11/2023
1.5.2-b92.7c961f5f 203 1/11/2023
1.5.0 2,897 11/9/2022
1.5.0-b88.7a7c20cd 197 11/9/2022
1.4.7 18,302 10/20/2022
1.4.6 5,665 10/17/2022
1.4.5 6,028 10/1/2022
1.4.4 11,244 8/16/2022
1.4.3 6,336 8/2/2022
1.4.2 6,158 7/19/2022
1.4.2-b80.7fdbfd04 227 7/19/2022
1.4.2-b74.acaf86f5 221 6/15/2022
1.4.1 6,354 6/13/2022
1.4.0 6,125 6/6/2022
1.3.8 8,232 4/12/2022
1.3.7 6,148 3/21/2022
1.3.6 6,092 3/17/2022
1.3.6-b67.ca5053f3 245 3/16/2022
1.3.6-b66.4a9683e6 235 3/16/2022
1.3.5 6,185 2/23/2022
1.3.4 6,816 1/20/2022
1.3.3 3,808 12/29/2021
1.3.2 3,349 12/11/2021
1.3.1 3,461 11/12/2021
1.3.0 3,519 11/8/2021
1.2.3 4,879 9/22/2021
1.2.2 3,906 8/20/2021
1.2.1 4,324 8/5/2021
1.2.0 12,924 8/1/2021
1.2.0-b37.a54030b9 270 6/24/2021
1.1.6 10,209 3/22/2021
1.1.5 4,060 3/9/2021
1.1.4 5,061 2/27/2021
1.1.3 3,561 2/17/2021
1.1.2 3,525 2/12/2021
1.1.1 3,873 2/1/2021
1.1.0 3,568 12/16/2020
1.1.0-b27.b66c309b 414 11/15/2020
1.0.12 6,179 10/18/2020
1.0.11 3,731 10/6/2020
1.0.10 3,807 9/30/2020
1.0.9 3,637 9/29/2020
1.0.8 3,623 9/26/2020
1.0.7 3,602 9/19/2020
1.0.6 3,588 9/3/2020
1.0.5 3,660 9/2/2020
1.0.4 3,802 9/1/2020
1.0.3 3,501 9/1/2020
1.0.2 3,504 8/29/2020
1.0.1 3,509 8/29/2020
1.0.0 3,536 8/29/2020
1.0.0-b1.c22f563d 373 8/28/2020

net10.0 support