Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 2, 2025

This PR adds comprehensive documentation for migrating from Newtonsoft.Json's JsonObject(Title = "title") attribute to System.Text.Json.

Problem

The JsonObjectAttribute.Title property in Newtonsoft.Json is used to set the title in JSON schema generation, but System.Text.Json has no direct equivalent. Users migrating from Newtonsoft.Json needed guidance on how to achieve the same functionality.

Solution

Added migration guidance that explains:

  1. No direct equivalent: System.Text.Json doesn't have a direct equivalent to JsonObjectAttribute.Title
  2. Modern solution: Starting in .NET 9, users can achieve the same result using JsonSchemaExporter with TransformSchemaNode
  3. OpenAPI context: Acknowledged that schema title customization is often needed for OpenAPI scenarios

Changes Made

Migration Guide Updates

  • Extended the existing JsonObjectAttribute section in migrate-from-newtonsoft.md to include Title property guidance
  • Added code example showing how to use TransformSchemaNode to set schema title
  • Referenced the schema exporter documentation for more comprehensive information

Schema Exporter Documentation

  • Added a new example in extract-schema.md demonstrating how to set custom schema titles
  • Created JsonObjectTitleExample.cs with working code that shows the TransformSchemaNode pattern

Example Code

The solution demonstrates how to transform schema nodes to add title metadata:

JsonSchemaExporterOptions exporterOptions = new()
{
    TransformSchemaNode = (context, schema) =>
    {
        // Only set title for the root type (not properties)
        if (context.PropertyInfo is null && context.TypeInfo.Type == typeof(Person))
        {
            if (schema is not JsonObject jObj)
            {
                // Handle Boolean schema case
                JsonValueKind valueKind = schema.GetValueKind();
                schema = jObj = new JsonObject();
                if (valueKind is JsonValueKind.False)
                {
                    jObj.Add("not", true);
                }
            }
            
            // Set the title in the schema
            jObj.Insert(0, "title", "PersonTitle");
        }
        
        return schema;
    }
};

This approach provides the same functionality as Newtonsoft.Json's JsonObject(Title = "PersonTitle") but uses System.Text.Json's modern schema generation capabilities.

Fixes #45862.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add guidance for migration from Newtonsoft Attribute JsonObject Property Title Add migration guidance for Newtonsoft.Json JsonObject Title property Sep 2, 2025
@Copilot Copilot AI requested a review from gewarren September 2, 2025 16:00
Copilot finished work on behalf of gewarren September 2, 2025 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add guidance for migration from Newtonsoft Attribute JsonObject Property Title
2 participants