Plinth.Serialization 1.8.0-b211.72089fd9

Prefix Reserved
This is a prerelease version of Plinth.Serialization.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Plinth.Serialization --version 1.8.0-b211.72089fd9
                    
NuGet\Install-Package Plinth.Serialization -Version 1.8.0-b211.72089fd9
                    
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-b211.72089fd9" />
                    
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-b211.72089fd9" />
                    
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-b211.72089fd9
                    
#r "nuget: Plinth.Serialization, 1.8.0-b211.72089fd9"
                    
#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-b211.72089fd9
                    
#: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-b211.72089fd9&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Plinth.Serialization&version=1.8.0-b211.72089fd9&prerelease
                    
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 616 11/13/2025
1.8.0-b211.72089fd9 241 11/12/2025
1.7.4 3,127 8/6/2025
1.7.3 487 8/2/2025
1.7.2 4,747 3/16/2025
1.7.1 1,597 12/12/2024
1.7.0 4,853 11/12/2024
1.6.6 1,452 11/8/2024
1.6.5 5,182 8/31/2024
1.6.4 1,006 8/2/2024
1.6.3 11,730 5/15/2024
1.6.2 1,183 2/16/2024
1.6.1 9,547 1/5/2024
1.6.0 1,794 11/30/2023
1.5.10-b186.aca976b4 104 11/30/2023
1.5.9 5,155 11/29/2023
1.5.9-b174.64153841 497 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,923 10/23/2023
1.5.7 10,440 7/31/2023
1.5.6 15,402 7/13/2023
1.5.5 1,099 6/29/2023
1.5.4 2,668 3/7/2023
1.5.3 1,850 3/3/2023
1.5.2 2,753 1/11/2023
1.5.2-b92.7c961f5f 204 1/11/2023
1.5.0 2,899 11/9/2022
1.5.0-b88.7a7c20cd 198 11/9/2022
1.4.7 18,305 10/20/2022
1.4.6 5,666 10/17/2022
1.4.5 6,032 10/1/2022
1.4.4 11,246 8/16/2022
1.4.3 6,338 8/2/2022
1.4.2 6,160 7/19/2022
1.4.2-b80.7fdbfd04 230 7/19/2022
1.4.2-b74.acaf86f5 223 6/15/2022
1.4.1 6,357 6/13/2022
1.4.0 6,128 6/6/2022
1.3.8 8,232 4/12/2022
1.3.7 6,149 3/21/2022
1.3.6 6,093 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,187 2/23/2022
1.3.4 6,818 1/20/2022
1.3.3 3,816 12/29/2021
1.3.2 3,351 12/11/2021
1.3.1 3,464 11/12/2021
1.3.0 3,521 11/8/2021
1.2.3 4,882 9/22/2021
1.2.2 3,908 8/20/2021
1.2.1 4,326 8/5/2021
1.2.0 12,926 8/1/2021
1.2.0-b37.a54030b9 273 6/24/2021
1.1.6 10,214 3/22/2021
1.1.5 4,063 3/9/2021
1.1.4 5,065 2/27/2021
1.1.3 3,564 2/17/2021
1.1.2 3,528 2/12/2021
1.1.1 3,877 2/1/2021
1.1.0 3,572 12/16/2020
1.1.0-b27.b66c309b 419 11/15/2020
1.0.12 6,184 10/18/2020
1.0.11 3,737 10/6/2020
1.0.10 3,812 9/30/2020
1.0.9 3,641 9/29/2020
1.0.8 3,627 9/26/2020
1.0.7 3,606 9/19/2020
1.0.6 3,592 9/3/2020
1.0.5 3,664 9/2/2020
1.0.4 3,807 9/1/2020
1.0.3 3,505 9/1/2020
1.0.2 3,509 8/29/2020
1.0.1 3,513 8/29/2020
1.0.0 3,540 8/29/2020
1.0.0-b1.c22f563d 377 8/28/2020

net10.0 support