Pdf Converter Software Free Download !FULL! For Windows 7

1 view
Skip to first unread message

Mathew Letter

unread,
Jan 18, 2024, 8:54:04 AM1/18/24
to rasfolkpropus

This article shows how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace. For an introduction to System.Text.Json, see How to serialize and deserialize JSON in .NET.

A converter is a class that converts an object or a value to and from JSON. The System.Text.Json namespace has built-in converters for most primitive types that map to JavaScript primitives. You can write custom converters:

pdf converter software free download for windows 7


DOWNLOAD ★★★ https://t.co/zKhxAlDxT4



There are two patterns for creating a custom converter: the basic pattern and the factory pattern. The factory pattern is for converters that handle type Enum or open generics. The basic pattern is for non-generic and closed generic types. For example, converters for the following types require the factory pattern:

The basic pattern creates a class that can handle one type. The factory pattern creates a class that determines, at run time, which specific type is required and dynamically creates the appropriate converter.

The following code shows a custom converter that works with Dictionary. The code follows the factory pattern because the first generic type parameter is Enum and the second is open. The CanConvert method returns true only for a Dictionary with two generic parameters, the first of which is an Enum type. The inner converter gets an existing converter to handle whichever type is provided at run time for TValue.

The factory pattern is required for open generics because the code to convert an object to and from a string isn't the same for all types. A converter for an open generic type (List, for example) has to create a converter for a closed generic type (List, for example) behind the scenes. Code must be written to handle each closed-generic type that the converter can handle.

If your converter is converting a JSON object, the Utf8JsonReader will be positioned on the begin object token when the Read method begins. You must then read through all the tokens in that object and exit the method with the reader positioned on the corresponding end object token. If you read beyond the end of the object, or if you stop before reaching the corresponding end token, you get a JsonException exception indicating that:

For an example, see the preceding factory pattern sample converter. The Read method starts by verifying that the reader is positioned on a start object token. It reads until it finds that it is positioned on the next end object token. It stops on the next end object token because there are no intervening start object tokens that would indicate an object within the object. The same rule about begin token and end token applies if you are converting an array. For an example, see the Stack sample converter later in this article.

The [JsonConverter] attribute on the struct registers the custom converter as the default for properties of type Temperature. The converter is automatically used on the TemperatureCelsius property of the following type when you serialize or deserialize it:

The example shows the converter code and a WeatherForecast class with object properties. The Main method deserializes a JSON string into a WeatherForecast instance, first without using the converter, and then using the converter. The console output shows that without the converter, the run-time type for the Date property is JsonElement; with the converter, the run-time type is DateTime.

.NET 7 provides support for both polymorphic serialization and deserialization. However, in previous .NET versions, there was limited polymorphic serialization support and no support for deserialization. If you're using .NET 6 or an earlier version, deserialization requires a custom converter.

Suppose, for example, you have a Person abstract base class, with Employee and Customer derived classes. Polymorphic deserialization means that at design time you can specify Person as the deserialization target, and Customer and Employee objects in the JSON are correctly deserialized at run time. During deserialization, you have to find clues that identify the required type in the JSON. The kinds of clues available vary with each scenario. For example, a discriminator property might be available or you might have to rely on the presence or absence of a particular property. The current release of System.Text.Json doesn't provide attributes to specify how to handle polymorphic deserialization scenarios, so custom converters are required.

The following code shows a base class, two derived classes, and a custom converter for them. The converter uses a discriminator property to do polymorphic deserialization. The type discriminator isn't in the class definitions but is created during serialization and is read during deserialization.

The converter code in the preceding example reads and writes each property manually. An alternative is to call Deserialize or Serialize to do some of the work. For an example, see this StackOverflow post.

A disadvantage of this method is you can't pass in the original options instance that registers the converter to Deserialize. Doing so would cause a stack overflow, as explained in Required properties. The following example shows a Read method that uses this alternative:

By default, the built-in JsonStringEnumConverter can serialize and deserialize string values for enums. It works without a specified naming policy or with the CamelCase naming policy. It doesn't support other naming policies, such as snake case. For information about custom converter code that can support round-tripping to and from enum string values while using a snake case naming policy, see GitHub issue dotnet/runtime #31619. Alternatively, upgrade to .NET 7 or later versions, which provide built-in support for applying naming policies when round-tripping to and from enum string values.

In some scenarios, you might want to use the default system converter in a custom converter. To do that, get the system converter from the JsonSerializerOptions.Default property, as shown in the following example:

This null-handling behavior is primarily to optimize performance by skipping an extra call to the converter. In addition, it avoids forcing converters for nullable types to check for null at the start of every Read and Write method override.

df19127ead
Reply all
Reply to author
Forward
0 new messages