Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
The JsonSerialization class in the DotNetTips.Spargine.Core assembly provides a powerful and efficient way to serialize objects to JSON and deserialize JSON back into objects. All methods are built on top of .NET’s high-performance System.Text.Json.JsonSerializer, ensuring modern, fast, allocation-efficient JSON handling for your applications.
This class goes beyond the basics by offering overloads that support JsonSerializerOptions and source-generated JsonTypeInfo, enabling even greater performance and flexibility — especially in high-throughput or large-scale scenarios.
Methods
Below is a complete overview of the methods available in JsonSerialization:
Deserialization
- Deserialize(string json)
Converts the specified JSON string into an object instance. - Deserialize<TResult>(string json, JsonSerializerOptions? options)
Deserializes the JSON string using the supplied JsonSerializerOptions for customized behavior such as naming, casing, or converters. - Deserialize<TResult>(string json, JsonTypeInfo<TResult> typeInfo)
Deserializes the JSON string using the provided JsonTypeInfo, enabling source-generated serialization for maximum performance. - DeserializeFromFile(FileInfo file)
Reads JSON content from the specified file and deserializes it into an object. If the file does not exist, an exception is thrown to ensure the issue is detected immediately.
JSON Comparison
- JsonEqual(JsonElement expected, JsonElement actual)
Performs a deep comparison between two JSON elements using JsonDocument.Parse, ensuring equality is based on structure and values — not just string formatting.
Loading Collections from JSON
- LoadCollectionFromJson(FileInfo file, int count)
Loads a collection of objects from a JSON array in a file. Includes overloads that leverage JsonTypeInfo for faster deserialization when working with known types. - LoadCollectionFromJson<T>(FileInfo file, int count)
Reads JSON content from the file and deserializes it into an array of strongly-typed objects.
Serialization
- Serialize(object obj)
Serializes an object into its JSON string representation using default settings. - Serialize(object obj, JsonSerializerOptions? options)
Serializes an object into JSON using the supplied JsonSerializerOptions. - Serialize<T>(T obj, JsonTypeInfo<T> typeInfo)
Serializes an object using source-generated JsonTypeInfo for even greater speed and lower allocations.
SerializeToFile(object obj, FileInfo file, JsonSerializerOptions? options)
Serializes an object and writes the JSON to a file. If the destination folder does not exist, it will be created automatically.
Performance and Validation
Every method in this class includes robust parameter validation, ensuring that invalid inputs trigger meaningful exceptions. This reduces runtime surprises and improves system reliability.
In addition, the methods in JsonSerialization have been carefully benchmarked and tuned for performance — making them well-suited for:
- High-load APIs
- Data processing services
- Cloud-scale applications
- Performance-sensitive .NET workloads
With support for both traditional and source-generated serialization, you can choose the balance of flexibility and speed that best fits your needs.
Summary
The JsonSerialization class in Spargine delivers a powerful, optimized, and developer-friendly way to handle JSON in .NET. By building on top of System.Text.Json — and enhancing it with validation, source-generated support, file helpers, and performance-focused design — this class helps you:
- Serialize objects quickly and safely
- Deserialize JSON with precision and flexibility
- Detect invalid data early
- Improve throughput and reduce memory allocations
- Simplify JSON handling across your applications
If you want JSON done right — fast, clean, and reliable — JsonSerialization is a rock-solid tool to add to your Spargine toolbox.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

