Skip to content

fix: (.NET) Improve json De/serialization #1138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Fellmonkey
Copy link
Contributor

@Fellmonkey Fellmonkey commented Aug 2, 2025

What does this PR do?

This PR refactors the JSON serialization and deserialization logic in the .NET SDK template to improve robustness and fix critical issues:
Key Changes:

  1. Fixed infinite recursion bug in ObjectToInferredTypesConverter: Replaced the problematic approach of using JsonSerializer.Deserialize recursively within the converter itself, which caused StackOverflowException for nested objects and arrays.

  2. Improved JSON type inference: Switched from JsonTokenType to JsonElement.ValueKind for more accurate and reliable type detection, providing better handling of all JSON value types including null and undefined values.

  3. Eliminates the risk of leaking JsonElement instances into the resulting object graph, simplifying model deserialization and removing the need for special handling of JsonElement in generated code.

  4. Streamlined model deserialization: Simplified the generated model deserialization logic by removing special handling for JsonElement objects and standardizing type conversions, making the generated code more readable and maintainable.

  5. Enhanced error handling: Added proper error handling with descriptive exceptions for unsupported JSON value kinds.

Test Plan

Testing the ObjectToInferredTypesConverter fix:

  • Create a test with nested JSON objects and arrays to verify no StackOverflowException occurs
  • Test deserialization of various JSON types (strings, numbers, booleans, null, objects, arrays)
  • Test edge cases with deeply nested structures

Testing model deserialization changes:

  • Generate SDK models with the updated template
  • Test deserialization of models with:
    • Array properties containing primitives and objects
    • Optional and required properties
    • Nested model objects
    • Various data types (string, integer, number, boolean)

Related PRs and Issues

This PR addresses potential runtime crashes and improves the overall reliability of JSON handling in generated .NET SDKs. The changes are particularly important for applications that work with complex nested JSON structures from API responses.

Related to issues with incorrect type mapping, JsonElement leakage, and runtime errors during deserialization of complex/nested JSON structures.

Have you read the Contributing Guidelines on issues?

YES

Replaces switch on JsonTokenType with a recursive method using JsonElement.ValueKind for more robust and accurate type inference. This improves handling of nested objects and arrays, and unifies the logic for converting JSON values to .NET types.
Simplifies and standardizes the deserialization of model properties from dictionaries, removing special handling for JsonElement and streamlining array and primitive type conversions. This improves code readability and maintainability in generated model classes.
@Fellmonkey Fellmonkey changed the title fix: (.NET) Improve json serialization fix: (.NET) Improve json De/serialization Aug 2, 2025
@Fellmonkey Fellmonkey mentioned this pull request Aug 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant