Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Add the package to your `Package.swift`:

```swift
dependencies: [
.package(url: "https://github.com/reidchatham/JSON.swift", from: "1.0.0"),
.package(url: "https://github.com/rchatham/JSON.swift", from: "1.0.0"),
],
targets: [
.target(name: "MyTarget", dependencies: [
Expand Down Expand Up @@ -115,6 +115,40 @@ let doubles = json.compactMap(\.doubleValue)

---

## Ordered, bounded JSON

Use `OrderedJSON` when a protocol depends on the order of object members or
needs to reject duplicate keys. Unlike `JSON.object([String: JSON])`, objects
are arrays of `OrderedJSONMember`; numbers retain their original spelling.
Parsing and serialization both enforce configurable resource limits and throw
on duplicate decoded keys, malformed UTF-8/escapes, invalid numbers, or a
limit violation.

```swift
import JSON

let limits = OrderedJSONLimits(
maximumBodyBytes: 1_048_576,
maximumDepth: 32,
maximumNodes: 10_000,
maximumStringBytes: 65_536,
maximumContainerEntries: 256
)
let value = try OrderedJSONParser(limits: limits).parse(
Data(#"{"second":2,"first":1}"#.utf8)
)
guard case .object(let members) = value else { fatalError("Expected an object") }
print(members.map(\.key)) // ["second", "first"]
let bytes = try OrderedJSONSerializer(limits: limits).serialize(value)
```

The caller must cap incoming bytes **before buffering** an untrusted request;
`maximumBodyBytes` validates the `Data` passed to `parse`, not a network stream.
The existing `JSON` API remains the simpler choice when member order and raw
numeric lexemes do not matter.

---

## JSONSchema

Define the shape of JSON values for validation, LLM structured output, and documentation.
Expand Down
Loading
Loading