News

.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 at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube