JSON to C#: Differences, Edge Cases & Pitfalls

Generate C# classes from JSON with PascalCase properties and JsonPropertyName mappings.

Turning a JSON sample into C# classes emits PascalCase properties with attributes mapping the original keys. Review the output by hand: numbers default to double and destroy big integers, empty arrays only produce placeholder object[], and nullability must match the real contract.

Format Differences

AspectJSONC# type definition
TypingWeak; resolved at runtimeStrong; checked at compile time
NamingcamelCase / snake_case mixedPascalCase properties plus mapping attributes
null handlingnull is just a valueValue types need Nullable<T> or the ? suffix
ArraysMixed element types allowedStrongly typed collections (List<T>, T[])
Number precisionLarge integers may be float-damagedlong / decimal hold them exactly
DatesString or epoch numberDateTime / DateTimeOffset with clear semantics
AbsenceMissing field and null are indistinguishableNullable reference types make it explicit
Runtime costCheap to parseDeserialisation allocates objects

Edge Cases

  • Inferring every number as double damages integers beyond 2^53 — change that property to long or decimal by hand.
  • One field that is a number in some objects and a string in others breaks inference; emit object or handle both.
  • Keys with hyphens or dots cannot be identifiers, so mapping attributes are the only bridge.
  • An empty array gives no element type, producing object[] or List<object> that must be replaced manually.

Common Pitfalls

  • Deleting the generated mapping attributes leaves every field at its default after deserialisation, with no error.
  • Big integers inferred as double zero out the last digit of an order number, discovered only at the database write.
  • Empty arrays infer object[], which compiles but forces casts everywhere downstream.
  • A field with inconsistent types across samples generates a type matching only one of them, throwing at runtime.

Related Tools