agentleFS
LeanMeanCleanAppsLeanMeanCleanApps/CleanWebApi/.agents/skills/json-serialization/SKILL.md

json-serialization

Skill4 starsChanged 10 days agoMITView on GitHub

How this Web API reads and writes JSON with System.Text.Json - the one set of options in UseApiDefaults, applied to both the controllers and the HTTP JSON options, and how tests use the same options. Use when adding a request or response type, changing how JSON looks, or when a value serialises differently than expected.

4 other projects use a version of this, 1 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

6 rules. 0 are common, 0 are distinctive, and 6 are very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Every request and response goes through System.Text.Json with one set of options, defined once in UseApiDefaults() …

    line 12
  2. Very specific:

    Add a test in tests/Presentation.Tests.Unit/Serialization/JsonSerializerOptionsExtensionsTests.cs for any setting you change, and an …

    line 36
  3. Very specific:

    Never let a response type refer back to its parent.

    line 37
  4. Very specific:

    Add ReferenceHandler.IgnoreCycles or Preserve to hide a cycle.

    line 57
  5. Very specific:

    Use Newtonsoft.Json or AddNewtonsoftJson().

    line 58
  6. Very specific:

    Use [JsonPropertyName] to rename one property.

    line 59

“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

JSON serialization (Web API)

The rule

Every request and response goes through System.Text.Json with one set of options, defined once in UseApiDefaults() (src/Presentation/Serialization/JsonSerializerOptionsExtensions.cs):

SettingEffect
PropertyNamingPolicy = CamelCaseaverageStars, not AverageStars
PropertyNameCaseInsensitive = trueRequest bodies are read whatever the case of their property names
DefaultIgnoreCondition = WhenWritingNullNull properties are left out of responses
NumberHandling = StrictNumbers must be JSON numbers. "5" for an int is refused with a 400, and OpenAPI shows integer, not integer | string
JsonStringEnumConverter(CamelCase)Enums are written and read as camelCase strings

ASP.NET Core has two sets of JSON options, and startup applies UseApiDefaults() to both in ProgramExtensions.ConfigureBuilder:

OptionsUsed by
MVC JsonOptions (AddControllers().AddJsonOptions)Controller request bodies and responses, and the problem details [ApiController] writes
HTTP JsonOptions (Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>)Problem details written outside a controller, such as a 404 for an unknown route

Controllers serialise with reflection, so there's no source-generated context to keep up to date. The integration tests build their options with the same UseApiDefaults(), so they read responses exactly as a client would.

Responses are trees: lists return summaries, and an entity holds only the flat records it needs. There's no cycle handling, so a cycle fails loudly instead of being quietly cut.

Steps

  1. Change how JSON looks only in UseApiDefaults(). Both option sets pick it up.
  2. Add a test in tests/Presentation.Tests.Unit/Serialization/JsonSerializerOptionsExtensionsTests.cs for any setting you change, and an integration test if a client would see the difference.
  3. Never let a response type refer back to its parent. Return a flat record instead (ReviewAuthor, ReviewedMovie).

Example

// src/Presentation/Extensions/ProgramExtensions.cs
_ = builder.Services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.UseApiDefaults());

_ = builder.Services.Configure<JsonOptions>(options => options.SerializerOptions.UseApiDefaults());
// tests/Presentation.Tests.Integration/Extensions/JsonSerializerExtensions.cs
private static readonly JsonSerializerOptions Options = new JsonSerializerOptions().UseApiDefaults();

Don't

  • Set serializer options anywhere but UseApiDefaults(), including in tests.
  • Configure one set of JSON options and not the other.
  • Add ReferenceHandler.IgnoreCycles or Preserve to hide a cycle. Change the response shape.
  • Use Newtonsoft.Json or AddNewtonsoftJson().
  • Use [JsonPropertyName] to rename one property. The naming policy names them all.

Check

dotnet test tests/Presentation.Tests.Unit --filter "FullyQualifiedName~Serialization"
dotnet test tests/Presentation.Tests.Integration

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.