Skip to content

Commit 7fc7b2f

Browse files
committed
Clarify C# and JSON Schema docs
1 parent eb01d94 commit 7fc7b2f

10 files changed

Lines changed: 339 additions & 286 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ When benchmark numbers move, profile before changing the runtime. The repo now i
230230
- Copy from [How to model common contract patterns](docs/HOW_TO_MODEL_COMMON_CONTRACT_PATTERNS.md).
231231
- Use [Configuration contracts guide](docs/CONFIG_CONTRACTS.md) for versioned config shapes.
232232
- Use [How to export JSON Schema](docs/HOW_TO_EXPORT_JSON_SCHEMA.md) and [JSON Schema support reference](docs/JSON_SCHEMA_SUPPORT.md) for schema interchange.
233-
- Use [How to import existing C# contracts](docs/HOW_TO_IMPORT_CSHARP_CONTRACTS.md) and [C# attribute bridge design](docs/CSHARP_ATTRIBUTE_BRIDGE.md) for the bridge/facade story.
233+
- Use [How to import existing C# contracts](docs/HOW_TO_IMPORT_CSHARP_CONTRACTS.md) for the bridge/facade story.
234234
- Browse the [API docs](https://adz.github.io/CodecMapper/).
235235

236236
## Benchmarks

docs/CSHARP_ATTRIBUTE_BRIDGE.md

Lines changed: 0 additions & 189 deletions
This file was deleted.

docs/GETTING_STARTED.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ This tutorial is learning-oriented: it introduces the main schema DSL, how to re
66

77
The portable core in `CodecMapper` is intended to stay usable from Native AOT and Fable-oriented targets. The separate `CodecMapper.Bridge` assembly is `.NET`-only because it imports CLR serializer metadata through reflection.
88

9+
## Choose the right starting path
10+
11+
Start from the contract you already have:
12+
13+
- F# contract you control: author a `Schema<'T>` directly and compile it.
14+
- New C# class you control: use `CSharpSchema` for setter-bound classes, then compile the result like any other schema.
15+
- Existing C# contract already annotated for `System.Text.Json`, `Newtonsoft.Json`, or `DataContract`: import it through `CodecMapper.Bridge`.
16+
- External JSON Schema document: keep that separate from authored schemas and use the JSON Schema import path only for receive-side `JsonValue` contracts.
17+
18+
That split matters because `CodecMapper` has two different extension stories:
19+
20+
- authored schemas: normal `Schema<'T>` values that compile into fast typed codecs
21+
- imported contracts: bridge or JSON Schema flows that help you interoperate with contracts you do not want to rewrite immediately
22+
923
## Core shape
1024

1125
The stable schema DSL is:
@@ -90,6 +104,46 @@ let personSchema =
90104
let jsonCodec = Json.codec personSchema
91105
```
92106

107+
## Starting from C#
108+
109+
If you are using `CodecMapper` from a C#-heavy codebase, there are two intended paths.
110+
111+
For new setter-bound classes, author the schema directly with `CSharpSchema`:
112+
113+
```csharp
114+
using CodecMapper;
115+
using CodecMapper.Bridge;
116+
117+
public sealed class User
118+
{
119+
public int Id { get; set; }
120+
public string Name { get; set; } = "";
121+
}
122+
123+
var schema =
124+
CSharpSchema.Record(() => new User())
125+
.Field("id", value => value.Id, (value, field) => value.Id = field)
126+
.Field("name", value => value.Name, (value, field) => value.Name = field)
127+
.Build();
128+
129+
var codec = CSharpSchema.Json(schema);
130+
var json = codec.Serialize(new User { Id = 1, Name = "Ada" });
131+
```
132+
133+
For existing attributed contracts, keep the existing annotations and import the contract instead:
134+
135+
```fsharp
136+
open CodecMapper
137+
open CodecMapper.Bridge
138+
139+
let schema =
140+
SystemTextJson.import<MyCompany.Contracts.User> BridgeOptions.defaults
141+
142+
let codec = Json.compile schema
143+
```
144+
145+
Use the direct C# schema path when you own the contract and want `CodecMapper` to be the source of truth. Use the bridge when an existing serializer contract is already established and you need an incremental migration path.
146+
93147
If you want a shorter qualified style without opening the module for the whole file, use a module alias:
94148

95149
```fsharp
@@ -374,5 +428,6 @@ Still out of scope:
374428
- Use `Schema.tryMap` when decode needs validation.
375429
- Reach for `Schema.nonEmptyString`, `Schema.trimmedString`, `Schema.positiveInt`, and `Schema.nonEmptyList` when those boundary rules are part of the contract.
376430
- Keep schemas in one place so JSON and XML stay aligned.
377-
- Use [How To Export JSON Schema](HOW_TO_EXPORT_JSON_SCHEMA.md) when you need external schema documents.
378-
- Use [C# Attribute Bridge Design](CSHARP_ATTRIBUTE_BRIDGE.md) when you are importing existing C# contracts instead of authoring schemas directly.
431+
- Treat JSON Schema as a separate integration surface: export it from authored schemas, or import it for receive-side `JsonValue` contracts.
432+
- Use [How To Export JSON Schema](HOW_TO_EXPORT_JSON_SCHEMA.md) when you need external schema documents from authored contracts.
433+
- Use [How To Import Existing C# Contracts](HOW_TO_IMPORT_CSHARP_CONTRACTS.md) when you are starting from an existing C# serializer contract.

docs/HOW_TO_EXPORT_JSON_SCHEMA.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
Use `JsonSchema.generate` when you want a JSON Schema document for the JSON wire contract already described by a `Schema<'T>`.
44

5+
This is the authored-schema path. It is separate from JSON Schema import:
6+
7+
- export starts from your typed `Schema<'T>`
8+
- import starts from an external JSON Schema document and returns `Schema<JsonValue>`
9+
10+
Keep those two workflows separate when you design your integration boundary.
11+
512
## Export a schema
613

714
```fsharp
@@ -101,6 +108,8 @@ let value = Json.deserialize codec """{"id":42,"name":"Ada"}"""
101108

102109
This path preserves the incoming JSON shape as `JsonValue`. It enforces the supported structural subset and leaves unsupported branch-heavy features on the raw JSON fallback path.
103110

111+
This is not a round-trip back into a typed authored schema. It is a receive-side integration boundary for external schema-owned contracts.
112+
104113
If you need to know what was enforced, use `JsonSchema.importWithReport`:
105114

106115
```fsharp

0 commit comments

Comments
 (0)