Skip to content

Commit 9f30c72

Browse files
authored
Update documentation (#411)
* add README badges to link the documentations * use `julia-repl` syntax highlighting * tables should be left-aligned! * add `@ref`s to the documentation * we don't need `@contents` blocks except for top page * sort sections * guides -> Guides
1 parent c8f6f79 commit 9f30c72

File tree

6 files changed

+40
-37
lines changed

6 files changed

+40
-37
lines changed

README.md

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

33
A Julia package for reading and writing JSON data.
44

5+
[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliaio.github.io/JSON.jl/stable)
6+
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://juliaio.github.io/JSON.jl/dev)
57
[![Build Status](https://github.com/JuliaIO/JSON.jl/workflows/CI/badge.svg)](https://github.com/JuliaIO/JSON.jl/actions/workflows/CI.yml?query=branch%3Amaster)
68
[![codecov.io](http://codecov.io/github/JuliaIO/JSON.jl/coverage.svg?branch=master)](http://codecov.io/github/JuliaIO/JSON.jl?branch=master)
79

docs/make.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
using Documenter, JSON
22

3-
makedocs(modules = [JSON], sitename = "JSON.jl")
3+
makedocs(
4+
modules = [JSON],
5+
sitename = "JSON.jl",
6+
pages = [
7+
"Home" => "index.md",
8+
"JSON Writing" => "writing.md",
9+
"JSON Reading" => "reading.md",
10+
"Migration Guides" => "migrate.md",
11+
"API Reference" => "reference.md",
12+
],
13+
)
414

515
deploydocs(repo = "github.com/JuliaIO/JSON.jl.git", push_preview = true)

docs/src/migrate.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Migration guides
1+
# Migration Guides
22

33
This guide provides an overview of how to migrate your code from either the pre-1.0 JSON.jl package to the 1.0 release or from JSON3.jl. The 1.0 release introduces several improvements and changes, particularly in how JSON is read and written, leveraging StructUtils.jl for customization and extensibility. Below, we outline the key differences and provide step-by-step instructions for updating your code.
44

@@ -7,7 +7,7 @@ This guide provides an overview of how to migrate your code from either the pre-
77
## Migration guide from pre-1.0 -> 1.0
88

99
### Writing JSON
10-
- `JSON.json`
10+
- [`JSON.json`](@ref)
1111
- What stayed the same:
1212
- Produces a compact String by default
1313
- Can automatically serialize basic structs in a sensible way
@@ -33,7 +33,7 @@ This guide provides an overview of how to migrate your code from either the pre-
3333
- Utilizing multiple dispatch to combine `JSON.print` and `JSON.json` and provide convenience for writing to files
3434
- Most opened issues over the last few years were about providing more controls around writing JSON without having to completely implement a custom serializer
3535
- More consistency with `JSON.parse` keyword args with `allownan` and `jsonlines`
36-
- `JSON.print`
36+
- [`JSON.print`](@ref)
3737
- What stayed the same:
3838
- Technically still defined for backwards compatibility, but just calls `JSON.json` under the hood
3939
- Why the changes:
@@ -55,12 +55,12 @@ This guide provides an overview of how to migrate your code from either the pre-
5555
- There was often confusion about whether a custom Serialization or StructuralContext was needed and what intefaces were then required to implement
5656
- The need to customize separators, delimiters, and indentation, while powerful, can be accomplished much simpler via keyword arguments or is not necessary at all (i.e. JSON.jl shouldn't be too concerned with how to produce anything that isn't JSON)
5757
- Instead of overloading show_string/show_element/show_key/show_pair/show_json, `lower` can be used to accomplish any requirements of "overloading" how values are serialized; the addition of "styles" also allows customizing for non-owned types instead of needing a custom context + `show_json` method
58-
- `JSONText`
58+
- [`JSONText`](@ref)
5959
- What changed:
6060
- Nothing; `JSONText` can still be used to have a JSON-formatted string be written as-is when serializing
6161

6262
### Reading JSON
63-
- `JSON.parse` / `JSON.parsefile`
63+
- [`JSON.parse`](@ref) / [`JSON.parsefile`](@ref)
6464
- What stayed the same:
6565
- These functions take the same JSON input arguments (String, IO, or filename for `parsefile`)
6666
- The `dicttype`, `allownan`, and `null` keyword arguments all remain and implement the same functionality
@@ -75,7 +75,7 @@ This guide provides an overview of how to migrate your code from either the pre-
7575
- The `inttype` keyword argument is rare among other JSON libraries and doesn't serve a strong purpose; memory gains from possibly using smaller ints is minimal and leads to more error-prone code via overflows by trying to force integers into non-standard small types
7676
- For the `allownan` default value change, there are many benchmarks/JSON-accuracy checking test suites that enforce adherance to the specification; following the specification by default is recommended and common across language JSON libraries
7777
- Mmapping is an internal detail that most users shouldn't worry about anyway, and it can be done transparently without any outside affect to the user
78-
- `JSONText`
78+
- [`JSONText`](@ref)
7979
- `JSONText` can now also be used while parsing, as a field type of a struct or directly to return the raw JSON (similar to how writing with `JSONText` works)
8080

8181
## Migration guide for JSON3.jl

docs/src/reading.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,39 @@ This guide to reading JSON in the JSON.jl package aims to:
44
- Provide a comprehensive overview of the JSON reading process.
55
- Explain the various options and configurations available for reading JSON data.
66
- Offer practical examples to illustrate the usage of different functions and options.
7-
8-
```@contents
9-
```
107

118
## Core JSON Parsing - `JSON.lazy` and `JSON.LazyValue`
129

1310
There are several "entrypoints" to reading JSON in JSON.jl, including:
14-
- `JSON.parse`/`JSON.parse!`
15-
- `JSON.parsefile`/`JSON.parsefile!`
16-
- `JSON.lazy`/`JSON.lazyfile`
17-
- `JSON.isvalidjson`
11+
- [`JSON.parse`](@ref)/`JSON.parse!`
12+
- [`JSON.parsefile`](@ref)/[`JSON.parsefile!`](@ref)
13+
- [`JSON.lazy`](@ref)/[`JSON.lazyfile`](@ref)
14+
- [`JSON.isvalidjson`](@ref)
1815

1916
These functions are all built to accept the same kinds of JSON inputs:
2017

2118
| Accepted `json` sources | Notes |
22-
|--------------------------------------------|---------------------------------------------------|
19+
|:-------------------------------------------|:--------------------------------------------------|
2320
| `AbstractString` | UTF‑8; UTF‑8‑BOM handled automatically |
2421
| `AbstractVector{UInt8}` | zero‑copy if already bytes |
2522
| `IO`, `IOStream`, `Base.AbstractCmd` | stream fully read into a byte vector |
2623

2724
The core JSON parsing machinery is hence built around having an `AbstractVector{UInt8}` or `AbstractString` JSON input where individual bytes can be parsed to identify JSON structure, validate syntax, and ultimately produce Julia-level values.
2825

29-
Each entrypoint function first calls `JSON.lazy`, which will consume the JSON input until the type of the next JSON value can be identified (`{` for objects, `[` for arrays, `"` for strings, `t` for true, `f` for false, `n` for null, and `-` or a digit for numbers). `JSON.lazy` returns a `JSON.LazyValue`, which wraps the JSON input buffer (`AbstractVector{UInt8}` or `AbstractString`), and marks the byte position the value starts at, the type of the value, and any keyword arguments that were provided that may affect parsing. Currently supported parsing-specific keyword arguments to `JSON.lazy` (and thus all other entrypoint functions) include:
26+
Each entrypoint function first calls [`JSON.lazy`](@ref), which will consume the JSON input until the type of the next JSON value can be identified (`{` for objects, `[` for arrays, `"` for strings, `t` for true, `f` for false, `n` for null, and `-` or a digit for numbers). [`JSON.lazy`](@ref) returns a [`JSON.LazyValue`](@ref), which wraps the JSON input buffer (`AbstractVector{UInt8}` or `AbstractString`), and marks the byte position the value starts at, the type of the value, and any keyword arguments that were provided that may affect parsing. Currently supported parsing-specific keyword arguments to [`JSON.lazy`](@ref) (and thus all other entrypoint functions) include:
3027

3128
- `allownan::Bool = false`: whether "special" float values shoudl be allowed while parsing (`NaN`, `Inf`, `-Inf`); these values are specifically _not allowed_ in the JSON spec, but many JSON libraries allow reading/writing
3229
- `ninf::String = "-Infinity"`: the string that will be used to parse `-Inf` if `allownan=true`
3330
- `inf::String = "Infinity"`: the string that will be used to parse `Inf` if `allownan=true`
3431
- `nan::String = "NaN"`: the string that will be sued to parse `NaN` if `allownan=true`
35-
- `jsonlines::Bool = false`: whether the JSON input should be treated as an implicit array, with newlines separating individual JSON elements with no leading `'['` or trailing `']'` characters. Common in logging or streaming workflows. Defaults to `true` when used with `JSON.parsefile` and the filename extension is `.jsonl` or `ndjson`. Note this ensures that parsing will _always_ return an array at the root-level.
32+
- `jsonlines::Bool = false`: whether the JSON input should be treated as an implicit array, with newlines separating individual JSON elements with no leading `'['` or trailing `']'` characters. Common in logging or streaming workflows. Defaults to `true` when used with [`JSON.parsefile`](@ref) and the filename extension is `.jsonl` or `ndjson`. Note this ensures that parsing will _always_ return an array at the root-level.
3633
- Materialization-specific keyword arguments (i.e. they affect materialization, but not parsing)
3734
- `dicttype = JSON.Object{String, Any}`: type to parse JSON objects as by default (recursively)
3835
- `null = nothing`: value to return for JSON `null` value
3936

40-
So what can we do with a `JSON.LazyValue`?
37+
So what can we do with a [`JSON.LazyValue`](@ref)?
4138

42-
```julia
39+
```julia-repl
4340
julia> x = JSON.lazy("{\"a\": 1, \"b\": null, \"c\": true, \"d\": false, \"e\": \"\", \"f\": [1,2,3], \"g\": {\"h\":{\"i\":\"foo\"}}}")
4441
LazyObject{String} with 7 entries:
4542
"a" => JSON.LazyValue(1)
@@ -55,7 +52,7 @@ Note that for convenience at the REPL, special `show` overloads enable displayin
5552
`LazyValue`s support convenient syntax for both _navigating_ their structure and _materializing_, with an aim
5653
to support lazy workflows. Examples include:
5754

58-
```julia
55+
```julia-repl
5956
# convenient "get" syntax on lazy objects
6057
julia> x.a
6158
JSON.LazyValue(1)
@@ -115,7 +112,7 @@ Ok, but at some point, we _do_ actually need Julia values to operate on, so let'
115112

116113
In the `LazyValue` syntax example, it was shown that empty `getindex` will result in a "default" materialization of a `LazyValue`:
117114

118-
```julia
115+
```julia-repl
119116
julia> x[]
120117
JSON.Object{String, Any} with 7 entries:
121118
"a" => 1
@@ -127,10 +124,10 @@ JSON.Object{String, Any} with 7 entries:
127124
"g" => Object{String, Any}("h"=>Object{String, Any}("i"=>"foo"))
128125
```
129126

130-
Under the hood, this `getindex` call is really calling `JSON.parse(lazyvalue)`. `JSON.parse` can also be called as a main entrypoint function with all the same input types as `JSON.lazy`. This form of `parse` is referred to as "untyped parsing" or "untyped materialization". It allocates and _materializes_ the raw JSON values into appropriate "default" Julia-level values. In particular:
127+
Under the hood, this `getindex` call is really calling `JSON.parse(lazyvalue)`. [`JSON.parse`](@ref) can also be called as a main entrypoint function with all the same input types as [`JSON.lazy`](@ref). This form of `parse` is referred to as "untyped parsing" or "untyped materialization". It allocates and _materializes_ the raw JSON values into appropriate "default" Julia-level values. In particular:
131128

132129
| JSON construct | Default Julia value |
133-
|----------------|---------------------------------------------------------------------------|
130+
|:---------------|:--------------------------------------------------------------------------|
134131
| object | `JSON.Object{String,Any}` (order‑preserving drop-in replacement for Dict) |
135132
| array | `Vector{Any}` |
136133
| string | `String` |
@@ -145,7 +142,7 @@ Because `Object` uses a linked-list implementation, key lookups are `O(n)`, perf
145142

146143
## `JSON.parse` - Typed materialization
147144

148-
While untyped materialization is convenient for quick exploration, one of the most powerful features of JSON.jl is its ability to directly parse JSON into concrete Julia types. This is done by providing a type as the second argument to `JSON.parse` and opens up a world of type-safe JSON parsing with minimal boilerplate.
145+
While untyped materialization is convenient for quick exploration, one of the most powerful features of JSON.jl is its ability to directly parse JSON into concrete Julia types. This is done by providing a type as the second argument to [`JSON.parse`](@ref) and opens up a world of type-safe JSON parsing with minimal boilerplate.
149146

150147
### Basic usage with structs
151148

docs/src/reference.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# API Reference
22

3-
```@contents
4-
```
5-
63
```@autodocs
74
Modules = [JSON]
85
```

docs/src/writing.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ This guide to writing JSON in the JSON.jl package aims to:
55
- Explain the various options and configurations available for writing JSON data.
66
- Offer practical examples to illustrate the usage of different functions and options.
77

8-
```@contents
9-
```
10-
118
## Core JSON Serialization - `JSON.json`
129

13-
The main entrypoint for serializing Julia values to JSON in JSON.jl is the `JSON.json` function. This function offers flexible output options:
10+
The main entrypoint for serializing Julia values to JSON in JSON.jl is the [`JSON.json`](@ref) function. This function offers flexible output options:
1411

1512
```julia
1613
# Serialize to a String
@@ -23,10 +20,10 @@ JSON.json(io::IO, x) -> IO
2320
JSON.json(file_name::String, x) -> String
2421
```
2522

26-
The `JSON.json` function accepts a wide range of Julia types and transforms them into their JSON representation by knowing how to serialize a core set of types:
23+
The [`JSON.json`](@ref) function accepts a wide range of Julia types and transforms them into their JSON representation by knowing how to serialize a core set of types:
2724

2825
| Julia type | JSON representation |
29-
|------------------------------------|------------------------------------------|
26+
|:-----------------------------------|:------------------------------------------|
3027
| `Nothing` | `null` |
3128
| `Bool` | `true` or `false` |
3229
| `Number` | Numeric value (integer or floating point) |
@@ -40,11 +37,11 @@ For values that don't fall into one of the above categories, `JSON.lower` will b
4037

4138
## Customizing JSON Output
4239

43-
`JSON.json` supports numerous keyword arguments to control how data is serialized:
40+
[`JSON.json`](@ref) supports numerous keyword arguments to control how data is serialized:
4441

4542
### Pretty Printing
4643

47-
By default, `JSON.json` produces compact JSON without extra whitespace. For human-readable output:
44+
By default, [`JSON.json`](@ref) produces compact JSON without extra whitespace. For human-readable output:
4845

4946
```julia
5047
# Boolean flag for default pretty printing (2-space indent)
@@ -355,7 +352,7 @@ JSON.json(config)
355352

356353
## Handling Circular References
357354

358-
`JSON.json` automatically detects circular references to prevent infinite recursion:
355+
[`JSON.json`](@ref) automatically detects circular references to prevent infinite recursion:
359356

360357
```julia
361358
mutable struct Node
@@ -374,7 +371,7 @@ JSON.json(node; omit_null=false)
374371

375372
## Custom Dictionary Key Serialization
376373

377-
For dictionaries with non-string keys, `JSON.json` has a few default `lowerkey` definitions to convert keys to strings:
374+
For dictionaries with non-string keys, [`JSON.json`](@ref) has a few default `lowerkey` definitions to convert keys to strings:
378375

379376
```julia
380377
# Integer keys

0 commit comments

Comments
 (0)