DEV Community

Khem Raj Rai
Khem Raj Rai

Posted on

5 JSON-to-C# conversion mistakes generated models can hide

JSON-to-C# generators are useful for removing boilerplate, but their output is a starting point - not a schema.

Before shipping generated models, I review five things:

1. Missing property vs explicit null

A missing JSON property and a property whose value is null can mean different things. The generated C# model should reflect whether the field is required, nullable, or optional in your application.

2. Integers that may outgrow Int32

A sample value of 42 does not prove the field will always fit in int. Identifiers, counters, and timestamps may need long, decimal, or even string.

3. Strings with stronger domain types

Values such as ISO timestamps, UUIDs, and money often arrive as strings. Consider DateTimeOffset, Guid, or decimal when the contract supports it.

4. Empty arrays reveal no element type

[] gives a converter no evidence about the collection's contents. Add a representative item before generating, or review the inferred fallback type.

5. Record vs class semantics

Records are useful for value-oriented data transfer models. Classes are often a better fit when identity, mutation, or framework behavior matters.

I built DevCrate's JSON to C# converter to make the first pass fast and private. Conversion happens locally in the browser, so the JSON is not sent to a conversion API:

https://devcrate.org/tools/json-to-csharp/

Generated code still deserves a human review.

Top comments (0)