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
<PackageReference Include="Plinth.Serialization" Version="1.8.0" />
<PackageVersion Include="Plinth.Serialization" Version="1.8.0" />
<PackageReference Include="Plinth.Serialization" />
paket add Plinth.Serialization --version 1.8.0
#r "nuget: Plinth.Serialization, 1.8.0"
#:package Plinth.Serialization@1.8.0
#addin nuget:?package=Plinth.Serialization&version=1.8.0
#tool nuget:?package=Plinth.Serialization&version=1.8.0
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 indentationOmitNamespaces- Excludes XML namespaces from outputOmitXmlDeclaration- Excludes<?xml version="1.0"?>declarationNoIndent- 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
- Use JsonUtil for Consistency - JsonUtil applies Plinth's standard settings across your application
- Null Handling - Remember that JsonUtil ignores null values by default to reduce payload size
- XML Namespaces - Use
OmitNamespaceswhen namespaces aren't required for cleaner XML - File Operations - Use
TryDeserializemethods for better error handling when reading files - Deep Cloning - Only use
DeepClonefor simple objects; complex object graphs may require custom cloning logic
| Product | Versions 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. |
-
net10.0
- Newtonsoft.Json (>= 13.0.4)
- Plinth.Common (>= 1.8.0)
-
net8.0
- Newtonsoft.Json (>= 13.0.4)
- Plinth.Common (>= 1.8.0)
- System.Text.Json (>= 10.0.0)
-
net9.0
- Newtonsoft.Json (>= 13.0.4)
- Plinth.Common (>= 1.8.0)
- System.Text.Json (>= 10.0.0)
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 | 0 | 11/13/2025 |
| 1.8.0-b211.72089fd9 | 38 | 11/12/2025 |
| 1.7.4 | 2,622 | 8/6/2025 |
| 1.7.3 | 478 | 8/2/2025 |
| 1.7.2 | 4,275 | 3/16/2025 |
| 1.7.1 | 1,578 | 12/12/2024 |
| 1.7.0 | 4,301 | 11/12/2024 |
| 1.6.6 | 1,446 | 11/8/2024 |
| 1.6.5 | 5,029 | 8/31/2024 |
| 1.6.4 | 1,000 | 8/2/2024 |
| 1.6.3 | 11,488 | 5/15/2024 |
| 1.6.2 | 1,174 | 2/16/2024 |
| 1.6.1 | 9,441 | 1/5/2024 |
| 1.6.0 | 1,789 | 11/30/2023 |
| 1.5.10-b186.aca976b4 | 99 | 11/30/2023 |
| 1.5.9 | 5,151 | 11/29/2023 |
| 1.5.9-b174.64153841 | 493 | 11/23/2023 |
| 1.5.9-b172.dfc6e7bd | 100 | 11/17/2023 |
| 1.5.9-b171.4e2b92e2 | 157 | 11/4/2023 |
| 1.5.8 | 15,978 | 10/23/2023 |
| 1.5.7 | 10,425 | 7/31/2023 |
| 1.5.6 | 15,388 | 7/13/2023 |
| 1.5.5 | 1,094 | 6/29/2023 |
| 1.5.4 | 2,663 | 3/7/2023 |
| 1.5.3 | 1,845 | 3/3/2023 |
| 1.5.2 | 2,746 | 1/11/2023 |
| 1.5.2-b92.7c961f5f | 198 | 1/11/2023 |
| 1.5.0 | 2,893 | 11/9/2022 |
| 1.5.0-b88.7a7c20cd | 191 | 11/9/2022 |
| 1.4.7 | 18,237 | 10/20/2022 |
| 1.4.6 | 5,659 | 10/17/2022 |
| 1.4.5 | 6,023 | 10/1/2022 |
| 1.4.4 | 11,241 | 8/16/2022 |
| 1.4.3 | 6,331 | 8/2/2022 |
| 1.4.2 | 6,154 | 7/19/2022 |
| 1.4.2-b80.7fdbfd04 | 222 | 7/19/2022 |
| 1.4.2-b74.acaf86f5 | 217 | 6/15/2022 |
| 1.4.1 | 6,348 | 6/13/2022 |
| 1.4.0 | 6,116 | 6/6/2022 |
| 1.3.8 | 8,226 | 4/12/2022 |
| 1.3.7 | 6,144 | 3/21/2022 |
| 1.3.6 | 6,088 | 3/17/2022 |
| 1.3.6-b67.ca5053f3 | 241 | 3/16/2022 |
| 1.3.6-b66.4a9683e6 | 230 | 3/16/2022 |
| 1.3.5 | 6,181 | 2/23/2022 |
| 1.3.4 | 6,811 | 1/20/2022 |
| 1.3.3 | 3,798 | 12/29/2021 |
| 1.3.2 | 3,344 | 12/11/2021 |
| 1.3.1 | 3,457 | 11/12/2021 |
| 1.3.0 | 3,516 | 11/8/2021 |
| 1.2.3 | 4,873 | 9/22/2021 |
| 1.2.2 | 3,900 | 8/20/2021 |
| 1.2.1 | 4,319 | 8/5/2021 |
| 1.2.0 | 12,920 | 8/1/2021 |
| 1.2.0-b37.a54030b9 | 266 | 6/24/2021 |
| 1.1.6 | 10,206 | 3/22/2021 |
| 1.1.5 | 4,055 | 3/9/2021 |
| 1.1.4 | 5,055 | 2/27/2021 |
| 1.1.3 | 3,555 | 2/17/2021 |
| 1.1.2 | 3,520 | 2/12/2021 |
| 1.1.1 | 3,868 | 2/1/2021 |
| 1.1.0 | 3,562 | 12/16/2020 |
| 1.1.0-b27.b66c309b | 407 | 11/15/2020 |
| 1.0.12 | 6,168 | 10/18/2020 |
| 1.0.11 | 3,720 | 10/6/2020 |
| 1.0.10 | 3,801 | 9/30/2020 |
| 1.0.9 | 3,628 | 9/29/2020 |
| 1.0.8 | 3,615 | 9/26/2020 |
| 1.0.7 | 3,594 | 9/19/2020 |
| 1.0.6 | 3,581 | 9/3/2020 |
| 1.0.5 | 3,652 | 9/2/2020 |
| 1.0.4 | 3,795 | 9/1/2020 |
| 1.0.3 | 3,494 | 9/1/2020 |
| 1.0.2 | 3,498 | 8/29/2020 |
| 1.0.1 | 3,501 | 8/29/2020 |
| 1.0.0 | 3,530 | 8/29/2020 |
| 1.0.0-b1.c22f563d | 366 | 8/28/2020 |
net10.0 support