NullValueHandling = NullValueHandling.Ignore, ContractResolver = new DefaultContractResolver NamingStrategy = new SnakeCaseNamingStrategy() , Formatting = Formatting.Indented ; string json = JsonConvert.SerializeObject(myObject, settings); Need to serialize a DateTime as a Unix timestamp? Map an enum to a string instead of an int? Handle a polymorphic type hierarchy where the base class doesn’t know its children? JsonConverter is your answer.
dotnet add package Newtonsoft.Json Or via Package Manager Console:
TypeNameHandling = TypeNameHandling.Auto ; (Warning: Only use this for trusted data—it's a security risk if you deserialize untrusted JSON.) Newtonsoft.Json is not the fastest library anymore. Microsoft's System.Text.Json is significantly faster, allocates less memory, and is more modern (using Utf8JsonReader and Utf8JsonWriter ). Benchmarks typically show System.Text.Json being 20-50% faster for serialization and 30-80% faster for deserialization. newtonsoft json dll
Microsoft’s System.Text.Json is the future for high-performance, modern cloud-native apps. But Newtonsoft.Json is the reliable, duct-tape-and-ingenuity library that still holds vast swaths of the .NET ecosystem together. It didn't just solve JSON serialization—it defined it.
In the sprawling universe of .NET development, few third-party libraries have achieved the ubiquity and lasting influence of Newtonsoft.Json (also known as Json.NET). For over a decade, it has been the default, instinctive choice for handling JSON—whether you were building a tiny console app, a massive enterprise web API, or a cross-platform mobile backend with Xamarin. NullValueHandling = NullValueHandling
Migration tools like System.Text.Json 's source generator can help, but many teams have simply decided: if it ain't broke, don't fix it. Newtonsoft.Json has had its share of CVEs. Most stem from using TypeNameHandling on untrusted input—a classic deserialization vulnerability that can lead to remote code execution.
| Feature | Newtonsoft.Json | System.Text.Json | |---------|----------------|------------------| | Default property name casing | Preserved | camelCase | | Non-public members | Can serialize with opt-in | Not supported | | Dictionary with non-string keys | Serializes as JSON object | Throws or requires converter | | Cyclic references | ReferenceLoopHandling.Ignore | Not supported | | DateTime handling | ISO by default | Strict ISO (no legacy formats) | JsonConverter is your answer
var settings = new JsonSerializerSettings