Add JSON Schema → C# records source generator#16
Draft
Copilot wants to merge 3 commits into
Draft
Conversation
Co-authored-by: Kralizek <2458439+Kralizek@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add library for generating class hierarchy from Json schema
Add JSON Schema → C# records source generator
Mar 14, 2026
Kralizek
requested changes
Mar 14, 2026
Contributor
Kralizek
left a comment
There was a problem hiding this comment.
Instead of records with primary constructors, use classic properties with get; init; accessors. Also decorate them with attributes to match the name and the description if available.
…e and Description attributes Co-authored-by: Kralizek <2458439+Kralizek@users.noreply.github.com>
Author
Addressed in c9dd4b8 — see the reply above for the full details and a sample of the new generated output. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces a new Roslyn
IIncrementalGeneratorlibrary that reads.schema.jsonadditional files and emits a#nullable enableC# record hierarchy, with namespacing derived from the file's directory path.Generator library (
src/JsonSchema)JsonSchemaGenerator–IIncrementalGenerator; picks up*.schema.jsonAdditionalFiles, derives namespace fromRootNamespace+ subdirectory path, derives type name from file name or schematitleSchemaParser– hand-written recursive descent JSON parser; no external dependencies, safe to run in any Roslyn host; handlestypeas string or array (["string","null"]→ nullable)CodeEmitter– emits#nullable enablerecords withget; init;property accessors; required properties → non-nullable (with= default!;for reference types), optional → nullable; nestedobjectproperties → additional sibling records named{Parent}{Prop}; each property is decorated with[JsonPropertyName(...)]and, when available,[Description(...)]Type mapping
stringstringstringdate-timeglobal::System.DateTimeOffsetstringdate/timeDateOnly/TimeOnlystringuuid/guidglobal::System.Guidintegerint64int/longnumberfloatdouble/floatbooleanboolarrayT[]object+additionalPropertiesDictionary<string, T>object+propertiesrecordProperty attributes
Each generated property is decorated with:
[global::System.Text.Json.Serialization.JsonPropertyName("originalName")]— always present, preserves the original JSON key casing for serialisation round-trips[global::System.ComponentModel.Description("...")]— emitted only when the JSON Schema property has adescriptionfieldExample
Given
Models/Person.schema.json:{ "type": "object", "required": ["id", "name"], "properties": { "id": { "type": "string", "format": "uuid" }, "name": { "type": "string", "description": "Full display name" }, "age": { "type": "integer" }, "address": { "type": "object", "required": ["street"], "properties": { "street": { "type": "string" }, "city": { "type": "string" } } } } }Emits:
Other changes
global.json– updated SDK from6.0.100(unavailable) to8.0.100withlatestMinorrollforwardtests/Tests.JsonSchema– 49 unit tests covering parser, emitter, and the Roslyn generator driverOriginal prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.