Posts about Serialization

Preserve number precision in JSON

October 18th 2024 .NET Serialization

Recently, I was troubleshooting some unexpected behavior of a REST service my team is maintaining: in its JSON response, the same numeric field sometimes included insignificant zeros after the decimal separator and sometimes it didn't. Further research revealed that the differences originated from another JSON document which served as the data source and manifested the same behavior. But we were still wondering why the insignificant zeros in the fractional part were preserved across deserializing the value and serializing it again.

Parsing JSON with variable field types

October 11th 2024 .NET Serialization

JSON serialization allows a lot of flexibility, sometimes even too much, e.g., a field that can be serialized into two different JSON types, depending on its contents. It could be serialized as string. Or, it could be serialized as a number. To make deserialization work, I had to implement a custom JsonConverter.

Combining multiple untyped JSONs

December 8th 2023 .NET Serialization

Usually, before manipulating JSON inputs, you first deserialize them into strongly typed objects that match their structure. However, that might not always be possible. Recently, I had to combine multiple JSON inputs into a single JSON array, without any knowledge of the input JSON structure.

Loading data from JSON in EF Core

September 15th 2023 EF Core Serialization

The navigation properties in Entity Framework Core models are not only useful for reading hierarchical data from database, but can also very much simplify importing hierarchical data from other sources like JSON files.

Priority of JSON converter attributes

July 14th 2023 .NET Serialization

After I learned about JsonStringEnumMemberConverter, I wanted to use it for an enum in a large project I was working with, but to my surprise it didn't seem to have any effect on the serialization.

Custom enum value name in System.Text.Json

June 23rd 2023 .NET Serialization

When serializing enums to JSON, it's usually better to serialize them as descriptive strings than incomprehensible magic numbers. Most of the time, it's good enough to simply use the names of enum members for this purpose. However, these must follow the rules for identifier names in C#, so they might be too restrictive if you need to use specific string values because of interoperability with other systems.

Truncated JSON response from web API

November 5th 2021 ASP.NET Core Serialization

An endpoint in my ASP.NET Core web API project suddenly started returning a truncated JSON response with a 200 response code. According to the logs, an exception was thrown, but for some reason the response code was not 500 as I would expect.

Polymorphism with New JSON Serialization

September 25th 2020 .NET Serialization

With the new System.Text.Json built into .NET Core, JSON serialization can now be performed without the Json.NET library. However, there are differences between the two libraries once you go beyond the basics. For example, support for serializing and deserializing polymorphic objects is limited in the new library.

XmlSerializer Constructor Performance Issues

June 12th 2011 Serialization .NET Framework

XmlSerializer is often a great choice for persisting objects or transmitting them over the wire, either by using default object serialization tailored only with attributes or by implementing the IXmlSerializable yourself. If you're not careful though, this might come at a significant performance cost.

XML Serialization Issues or Having Fun with TimeSpan and XmlIgnore

November 10th 2010 Serialization .NET Framework

Recently I tackled a seemingly simple task: XML serialization of a generic class used with a TimeSpan data type. It turned out that XmlSerializer doesn't serialize the TimeSpan structure at all.