Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New article - Migrate from Newtonsoft.Json #16225

Merged
merged 76 commits into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
db7bdad
initial draft
tdykstra Nov 27, 2019
c9e7a21
fix links
tdykstra Dec 12, 2019
88cd245
fix link
tdykstra Dec 13, 2019
0f60978
corrections
tdykstra Dec 13, 2019
3c634dc
add new doc to toc
tdykstra Dec 13, 2019
d994f56
add'l notes from ahson email
tdykstra Dec 13, 2019
6d4216c
fix issue numbers
tdykstra Dec 13, 2019
2594e49
comment out roadmap link
tdykstra Dec 13, 2019
823b581
incorporate info from roadmap
tdykstra Dec 15, 2019
7dd85d6
more info from roadmap
tdykstra Dec 15, 2019
3a2ab3d
address thraka feedback
tdykstra Dec 17, 2019
c546273
Newtonsoft --> Json.NET
tdykstra Dec 17, 2019
07cc159
omit 'the current release of'
tdykstra Dec 17, 2019
20f1095
omit 'by design'
tdykstra Dec 17, 2019
41019bc
link to exact hash instead of branch
tdykstra Dec 17, 2019
0665118
Json.NET --> Newtonsoft.Json
tdykstra Dec 17, 2019
7243deb
scrub additional resources sections
tdykstra Dec 17, 2019
163ed4e
rearrange several h2 sections
tdykstra Dec 17, 2019
b5bd3e7
add (marshal/unmarshal) to headings
tdykstra Dec 19, 2019
7771245
update dates
tdykstra Dec 20, 2019
a1eae47
ahson feedback, restore code to converters doc
tdykstra Dec 24, 2019
b023d84
move 3 converters back to converter doc
tdykstra Dec 24, 2019
55d99cc
additional note from comments in 36639
tdykstra Dec 24, 2019
a78c5ac
minor clarification
tdykstra Dec 26, 2019
9c331ff
remove links to issues
tdykstra Dec 27, 2019
618aca8
add links to s.t.j.serialization
tdykstra Dec 27, 2019
84a65bc
remove extraneous words
tdykstra Dec 27, 2019
e4a27a1
remove extraneous words
tdykstra Dec 27, 2019
6b279eb
4.6.1 --> 4.7.2
tdykstra Dec 27, 2019
2feef06
Update docs/standard/serialization/system-text-json-converters-how-to.md
tdykstra Dec 27, 2019
99619d1
ahson feedback section 1
tdykstra Dec 27, 2019
ec3d07c
fix h2->h3, tweak heading text
tdykstra Dec 27, 2019
1919f9d
layomi feedback
tdykstra Dec 27, 2019
0f9a494
reapply change lost earlier
tdykstra Dec 27, 2019
16b73b3
remove dbnull comments
tdykstra Dec 28, 2019
4618b0c
Apply suggestions from code review
tdykstra Dec 31, 2019
1bcb4f3
Apply suggestion from code review
tdykstra Dec 31, 2019
66dbd62
ahson feedback
tdykstra Dec 31, 2019
805f425
Merge branch 'jnmigrate' of https://github.com/tdykstra/dotnet-docs i…
tdykstra Dec 31, 2019
6fc6377
make registration links more specific
tdykstra Dec 31, 2019
57fb760
add converters api link
tdykstra Jan 1, 2020
35434dd
Apply suggestions from code review
tdykstra Jan 1, 2020
93d7564
add note to null-to-nonnullable section
tdykstra Jan 1, 2020
2610c4a
Update docs/standard/serialization/system-text-json-migrate-from-newt…
tdykstra Jan 1, 2020
957f976
Apply suggestions from code review
tdykstra Jan 1, 2020
9fc486a
fix link
tdykstra Jan 1, 2020
bc30ca2
acrolinx and feedback
tdykstra Jan 1, 2020
9a057bb
add workaround notes
tdykstra Jan 1, 2020
93194b2
first occurrrences of STJ --> xref link
tdykstra Jan 6, 2020
2273cf4
more api ref links
tdykstra Jan 6, 2020
1c34eb5
Merge branch 'master' into jnmigrate
tdykstra Jan 6, 2020
0890e2e
strings in quotes, nonstring values, polymorphism
tdykstra Jan 7, 2020
5a0b857
corrections to polymorphic sections
tdykstra Jan 7, 2020
b721d7b
address feedback
tdykstra Jan 7, 2020
f6b0a55
more feedback
tdykstra Jan 7, 2020
26930e8
minor wording change
tdykstra Jan 7, 2020
b68ba5e
misc feedback
tdykstra Jan 8, 2020
80ed897
misc feedback
tdykstra Jan 8, 2020
c2191e6
revise polymorphic interface example
tdykstra Jan 8, 2020
43a657a
misc feedback
tdykstra Jan 8, 2020
7b53d91
fix links
tdykstra Jan 8, 2020
fd05648
standardize links to source code
tdykstra Jan 8, 2020
303d8e5
shorten valuetextequals example
tdykstra Jan 8, 2020
63e02ab
misc feedback
tdykstra Jan 8, 2020
65be137
misc feedback
tdykstra Jan 9, 2020
72a8a34
valuetuple
tdykstra Jan 10, 2020
50b3574
invitation to share workaround code
tdykstra Jan 10, 2020
fd61261
feedback
tdykstra Jan 10, 2020
942e7c2
proofread pass
tdykstra Jan 10, 2020
f6d6623
update ms.date
tdykstra Jan 10, 2020
2fa28ee
tools --> APIs
tdykstra Jan 10, 2020
27d4fd4
feedback
tdykstra Jan 10, 2020
55ee7b7
immutablestack
tdykstra Jan 10, 2020
b3eb4f4
clarify when to clone
tdykstra Jan 10, 2020
2470d9e
revert change already made by 16615
tdykstra Jan 11, 2020
30277d7
Update docs/standard/serialization/system-text-json-migrate-from-newt…
tdykstra Jan 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions docs/standard/serialization/system-text-json-converters-how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "How to write custom converters for JSON serialization - .NET"
author: tdykstra
ms.author: tdykstra
ms.date: "10/16/2019"
ms.date: "12/20/2019"
helpviewer_keywords:
- "JSON serialization"
- "serializing objects"
Expand All @@ -11,7 +11,7 @@ helpviewer_keywords:
- "converters"
---

# How to write custom converters for JSON serialization in .NET
# How to write custom converters for JSON serialization (marshalling) in .NET

This article shows how to create custom converters for the JSON serialization classes that are provided in the <xref:System.Text.Json> namespace. For an introduction to `System.Text.Json`, see [How to serialize and deserialize JSON in .NET](system-text-json-how-to.md).

Expand All @@ -20,7 +20,7 @@ A *converter* is a class that converts an object or a value to and from JSON. Th
* To override the default behavior of a built-in converter. For example, you might want `DateTime` values to be represented by mm/dd/yyyy format instead of the default ISO 8601-1:2019 format.
* To support a custom value type. For example, a `PhoneNumber` struct.

You can also write custom converters to extend `System.Text.Json` with functionality not included in the current release. The following scenarios are covered later in this article:
You can also write custom converters to customize or extend `System.Text.Json` with functionality not included in the current release. The following scenarios are covered later in this article:
tdykstra marked this conversation as resolved.
Show resolved Hide resolved

* [Deserialize inferred types to Object properties](#deserialize-inferred-types-to-object-properties).
tdykstra marked this conversation as resolved.
Show resolved Hide resolved
* [Support Dictionary with non-string key](#support-dictionary-with-non-string-key).
Expand Down Expand Up @@ -65,9 +65,9 @@ The following steps explain how to create a converter by following the basic pat
* Create a class that derives from <xref:System.Text.Json.Serialization.JsonConverter%601> where `T` is the type to be serialized and deserialized.
* Override the `Read` method to deserialize the incoming JSON and convert it to type `T`. Use the <xref:System.Text.Json.Utf8JsonReader> that is passed to the method to read the JSON.
* Override the `Write` method to serialize the incoming object of type `T`. Use the <xref:System.Text.Json.Utf8JsonWriter> that is passed to the method to write the JSON.
* Override the `CanConvert` method only if necessary. The default implementation returns `true` when the type to convert is type `T`. Therefore, converters that support only type `T` don't need to override this method. For an example of a converter that does need to override this method, see the [polymorphic deserialization](#support-polymorphic-deserialization) section later in this article.
* Override the `CanConvert` method only if necessary. The default implementation returns `true` when the type to convert is of type `T`. Therefore, converters that support only type `T` don't need to override this method. For an example of a converter that does need to override this method, see the [polymorphic deserialization](#support-polymorphic-deserialization) section later in this article.

You can refer to the [built-in converters source code](https://github.com/dotnet/corefx/tree/master/src/System.Text.Json/src/System/Text/Json/Serialization/Converters/) as reference implementations for writing custom converters.
You can refer to the [built-in converters source code](https://github.com/dotnet/runtime/tree/f77914d4c9045a01b3a91b63c28805f24850c274/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/) as reference implementations for writing custom converters.
tdykstra marked this conversation as resolved.
Show resolved Hide resolved

## Steps to follow the factory pattern

Expand Down Expand Up @@ -183,7 +183,8 @@ Type inference can be inaccurate. If the deserializer parses a JSON number that
For scenarios that require type inference, the following code shows a custom converter for `Object` properties. The code converts:

* `true` and `false` to `Boolean`
* Numbers to `long` or `double`
* Numbers without a decimal to `long`
* Numbers with a decimal to `double`
* Dates to `DateTime`
* Strings to `string`
* Everything else to `JsonElement`
Expand Down Expand Up @@ -242,7 +243,7 @@ The JSON output from serialization looks like the following example:
}
```

The [unit tests folder](https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/tests/Serialization/) in the `System.Text.Json.Serialization` namespace has more examples of custom converters that handle non-string-key dictionaries.
The [unit tests folder](https://github.com/dotnet/corefx/blob/f77914d4c9045a01b3a91b63c28805f24850c274/src/System.Text.Json/tests/Serialization/) in the `System.Text.Json.Serialization` namespace has more examples of custom converters that handle non-string-key dictionaries.

### Support polymorphic deserialization
tdykstra marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -279,22 +280,25 @@ The converter can deserialize JSON that was created by using the same converter

## Other custom converter samples

The [unit tests folder](https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/tests/Serialization/) in the `System.Text.Json.Serialization` source code includes other custom converter samples, such as:
The [Migrate from Newtonsoft.Json to System.Text.Json](system-text-json-migrate-from-newtonsoft-how-to.md) article contains additional samples of custom converters.

The [unit tests folder](https://github.com/dotnet/runtime/blob/f77914d4c9045a01b3a91b63c28805f24850c274/src/libraries/System.Text.Json/tests/Serialization/) in the `System.Text.Json.Serialization` source code includes other custom converter samples, such as:

* `Int32` converter that converts null to 0 on deserialize
tdykstra marked this conversation as resolved.
Show resolved Hide resolved
* `Int32` converter that allows both string and number values on deserialize
* `Enum` converter
* `List<T>` converter that accepts external data
* `Long[]` converter that works with a comma-delimited list of numbers

If you need to make a converter that modifies the behavior of an existing built-in converter, you can get [the source code of the existing converter](https://github.com/dotnet/runtime/tree/f77914d4c9045a01b3a91b63c28805f24850c274/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters) to serve as a starting point for customization.

## Additional resources

* [Source code for built-in converters](https://github.com/dotnet/runtime/tree/f77914d4c9045a01b3a91b63c28805f24850c274/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/)
* [DateTime and DateTimeOffset support in System.Text.Json](../datetime/system-text-json-support.md)
* [System.Text.Json overview](system-text-json-overview.md)
* [System.Text.Json API reference](xref:System.Text.Json)
* [How to use System.Text.Json](system-text-json-how-to.md)
* [Source code for built-in converters](https://github.com/dotnet/corefx/tree/master/src/System.Text.Json/src/System/Text/Json/Serialization/Converters/)
* GitHub issues related to custom converters for `System.Text.Json`
* [36639 Introducing custom converters](https://github.com/dotnet/corefx/issues/36639)
* [38713 About deserializing to Object](https://github.com/dotnet/corefx/issues/38713)
* [40120 About non-string-key dictionaries](https://github.com/dotnet/corefx/issues/40120)
* [37787 About polymorphic deserialization](https://github.com/dotnet/corefx/issues/37787)
* [How to migrate from Newtonsoft.Json](system-text-json-migrate-from-newtonsoft-how-to.md)
* [System.Text.Json API reference](xref:System.Text.Json)
tdykstra marked this conversation as resolved.
Show resolved Hide resolved
* [System.Text.Json.Serialization API reference](xref:System.Text.Json.Serialization)
<!-- * [System.Text.Json roadmap](https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/roadmap/README.md)-->
23 changes: 12 additions & 11 deletions docs/standard/serialization/system-text-json-how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
title: "How to serialize and deserialize JSON using C# - .NET"
author: tdykstra
ms.author: tdykstra
ms.date: "09/16/2019"
ms.date: "12/20/2019"
helpviewer_keywords:
- "JSON serialization"
- "serializing objects"
- "serialization"
- "objects, serializing"
---

# How to serialize and deserialize JSON in .NET
# How to serialize and deserialize (marshal and unmarshal) JSON in .NET

This article shows how to use the <xref:System.Text.Json> namespace to serialize and deserialize to and from JavaScript Object Notation (JSON).

Expand Down Expand Up @@ -106,7 +106,7 @@ Serializing to UTF-8 is about 5-10% faster than using the string-based methods.
* The [default encoder](xref:System.Text.Encodings.Web.JavaScriptEncoder.Default) escapes non-ASCII characters, HTML-sensitive characters within the ASCII-range, and characters that must be escaped according to [the RFC 8259 JSON spec](https://tools.ietf.org/html/rfc8259#section-7).
* By default, JSON is minified. You can [pretty-print the JSON](#serialize-to-formatted-json).
* By default, casing of JSON names matches the .NET names. You can [customize JSON name casing](#customize-json-names-and-values).
* Circular references are detected and exceptions thrown. For more information, see [issue 38579 on circular references](https://github.com/dotnet/corefx/issues/38579) in the dotnet/corefx repository on GitHub.
* Circular references are detected and exceptions thrown.
* Currently, fields are excluded.

Supported types include:
Expand All @@ -115,7 +115,7 @@ Supported types include:
* User-defined [Plain Old CLR Objects (POCOs)](https://stackoverflow.com/questions/250001/poco-definition).
* One-dimensional and jagged arrays (`ArrayName[][]`).
* `Dictionary<string,TValue>` where `TValue` is `object`, `JsonElement`, or a POCO.
* Collections from the following namespaces. For more information, see the [issue on collection support](https://github.com/dotnet/corefx/issues/36643) in the dotnet/corefx repository on GitHub.
* Collections from the following namespaces.
* <xref:System.Collections>
* <xref:System.Collections.Generic>
* <xref:System.Collections.Immutable>
Expand Down Expand Up @@ -151,13 +151,13 @@ To deserialize from UTF-8, call a <xref:System.Text.Json.JsonSerializer.Deserial
* By default, property name matching is case-sensitive. You can [specify case-insensitivity](#case-insensitive-property-matching).
* If the JSON contains a value for a read-only property, the value is ignored and no exception is thrown.
* Deserialization to reference types without a parameterless constructor isn't supported.
* Deserialization to immutable objects or read-only properties isn't supported. For more information, see GitHub [issue 38569 on immutable object support](https://github.com/dotnet/corefx/issues/38569) and [issue 38163 on read-only property support](https://github.com/dotnet/corefx/issues/38163) in the dotnet/corefx repository on GitHub.
* Deserialization to immutable objects or read-only properties isn't supported.
* By default, enums are supported as numbers. You can [serialize enum names as strings](#enums-as-strings).
* Fields aren't supported.
* By default, comments or trailing commas in the JSON throw exceptions. You can [allow comments and trailing commas](#allow-comments-and-trailing-commas).
* The [default maximum depth](xref:System.Text.Json.JsonReaderOptions.MaxDepth) is 64.

You can [implement custom converters](system-text-json-converters-how-to.md) to provide functionality that isn't supported by the built-in converters.
You can [implement custom converters](system-text-json-converters-how-to.md) to provide functionality that isn't supported by the built-in converters. For more information, see [How to migrate from Newtonsoft.Json to System.Text.Json](system-text-json-migrate-from-newtonsoft-how-to.md).
tdykstra marked this conversation as resolved.
Show resolved Hide resolved

## Serialize to formatted JSON

Expand Down Expand Up @@ -498,7 +498,7 @@ In the preceding example scenario, both approaches cause the `WindSpeed` propert
}
```

For information about polymorphic deserialization, see [Support polymorphic deserialization](system-text-json-converters-how-to.md#support-polymorphic-deserialization).
This section shows how to do **serialization**. For information about polymorphic **deserialization**, see [How to migrate from Newtonsoft.Json to System.Text.Json](system-text-json-migrate-from-newtonsoft-how-to.md#polymorphic-deserialization).

## Allow comments and trailing commas

Expand Down Expand Up @@ -623,7 +623,7 @@ To change this behavior, set <xref:System.Text.Json.JsonSerializerOptions.Ignore

With this option, the `Summary` property of the `WeatherForecastWithDefault` object is the default value "No summary" after deserialization.

Null values in the JSON are ignored only if they are valid. Null values for non-nullable value types cause exceptions. For more information, see [issue 40922 on non-nullable value types](https://github.com/dotnet/corefx/issues/40922) in the dotnet/corefx repository on GitHub.
Null values in the JSON are ignored only if they are valid. Null values for non-nullable value types cause exceptions.

## Utf8JsonReader, Utf8JsonWriter, and JsonDocument

Expand Down Expand Up @@ -712,7 +712,8 @@ Here's a JSON sample that the preceding code can read. The resulting summary mes
## Additional resources

* [System.Text.Json overview](system-text-json-overview.md)
* [System.Text.Json API reference](xref:System.Text.Json)
* [Write custom converters for System.Text.Json](system-text-json-converters-how-to.md)
* [How to write custom converters](system-text-json-converters-how-to.md)
* [How to migrate from Newtonsoft.Json](system-text-json-migrate-from-newtonsoft-how-to.md)
* [DateTime and DateTimeOffset support in System.Text.Json](../datetime/system-text-json-support.md)
* [GitHub issues in the dotnet/corefx repository labeled json-functionality-doc](https://github.com/dotnet/corefx/labels/json-functionality-doc)
* [System.Text.Json API reference](xref:System.Text.Json)
<!-- * [System.Text.Json roadmap](https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/roadmap/README.md)-->
Loading