.NET 7 Preview 6 Adds JSON Contract Customization

The sixth preview of .NET 7 improves type converter functionality, updates System.Formats.Tar APIs, adds constraints to .NET template authoring, boosts CodeGen performance and allows for JSON contract customization.

The latter enhancement gives developers more control over what and how types get serialized or deserialized when working with JSON contracts.

"In certain situations developers serializing or deserializing JSON find that they don't want to or cannot change types because they either come from external library or it would greatly pollute the code but need to make some changes which influence serialization like removing property, changing how numbers get serialized, how object is created etc," said Microsoft's Jon Douglas, senior program manager, NuGet, in a July 12 announcement. "They frequently are forced to either write wrappers or custom converters which are not only a hassle but also make serialization slower."

Douglas explained how to opt in to customization, different types of customizations, how properties are customized and conditional serialization of properties. He also provided a code sample to detail how to ignore properties with a specific name or type.

Themes of .NET for v7 Milestone
[Click on image for larger view.] Themes of .NET for v7 Milestone (source: Microsoft).

In addition to JSON contract customization, following are capsule summarizations of some other Preview 6 highlights, following general guidance on the Themes of .NET site (pictured above):

  • Type converters: The team provided type converters for new primitive types including DateOnly, TimeOnly, Int128, UInt128 and Half.
  • System.Formats.Tar API updates: The team tweaked the System.Formats.Tar assembly that was introduced in an earlier preview in order to provide APIs for manipulating TAR archives. Changes were made to cover some special cases involving:
    • Global Extended Attributes specialized class -- a new class was added to describe a GEA entry.
    • Entry format, not archive format -- The team discovered that entries of different formats can be intermixed in a single TAR archive, so the TarFormat enum was renamed to TarEntryFormat.
    • Writing and reading changes -- The Format property was removed from TarReader because no archive is expected to have all its entries in a single format.
  • Template authoring: This was improved in three ways:
    • Constraints -- Introduced in this preview for .NET Templates, constraints help developers define the context in which templates are allowed, helping the template engine determine which templates it should show in commands such as dotnet new list. Preview 6 supports three kinds of constraints:
      • Operating System constraints limit templates based on the OS of the user.
      • Template Engine Host constraints limit templates based on which host is executing the Template Engine, which is usually the .NET CLI itself, or embedded scenarios such as the New Project Dialog in Visual Studio/Visual Studio for Mac.
      • Installed Workloads constraints require that the specified .NET SDK workload is installed before the template will become available.
    • Multi-choice parameters -- When working with choice parameters, developers can now specify more than one value in a single selection, similar to the way a Flags-style enum might be used. Some potential common examples of this type of parameter include:
      • Opting into multiple forms of authentication on the web template
      • Choosing multiple target platforms (ios, android, web) at once in the maui templates
      More information on this is available here.
    • Exit codes unification and reporting -- This release unifies exit codes reported by the Template Engine, which is expected to help users who rely on scripting in shells to enjoy a more consistent error-handling experience. Also, errors reported by the .NET CLI now include a link so developers can find detailed information about each exit code.
  • CodeGen: Here, the dev team was helped out with a bunch of community PRs. Changes affected dynamic PGO (profile-guided optimization), Arm64, and loop optimizations, along with other more general optimizations.

Breaking changes, meanwhile, are listed here by area and release with links to detailed explanations, while proposed breaking changes that are still under review can be seen here.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus


Subscribe on YouTube