Skip to content

Commit 842de60

Browse files
committed
Add new uber-simple vendor/jsonx.jl for true no-dependency JSON functionality that can be directly vendored in
1 parent a86467d commit 842de60

File tree

5 files changed

+788
-0
lines changed

5 files changed

+788
-0
lines changed

.github/workflows/vendor-tests.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Vendor Tests
2+
3+
on:
4+
push:
5+
paths:
6+
- 'vendor/**'
7+
pull_request:
8+
paths:
9+
- 'vendor/**'
10+
11+
jobs:
12+
test:
13+
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }}
14+
runs-on: ${{ matrix.os }}
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
version:
19+
- '1' # automatically expands to the latest stable 1.x release of Julia
20+
- 'min'
21+
- 'pre'
22+
os:
23+
- ubuntu-latest
24+
- windows-latest
25+
arch:
26+
- x64
27+
include:
28+
- os: macOS-latest
29+
arch: aarch64
30+
version: 1
31+
32+
steps:
33+
- name: Checkout code
34+
uses: actions/checkout@v4
35+
36+
- name: Setup Julia
37+
uses: julia-actions/setup-julia@v2
38+
with:
39+
version: ${{ matrix.version }}
40+
arch: ${{ matrix.arch }}
41+
42+
- name: Cache Julia packages
43+
uses: julia-actions/cache@v2
44+
45+
- name: Run vendor tests
46+
run: |
47+
cd vendor
48+
julia test.jl

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ JSON.isvalidjson(json)
7171
JSON.json("test.json", j)
7272
```
7373

74+
## Vendor Directory
75+
76+
This package includes a `vendor/` directory containing a simplified, no-dependency JSON parser (`JSONX`) that can be vendored (copied) into other projects. See the [vendor README](vendor/README.md) for details.
77+
7478
## Contributing and Questions
7579

7680
Contributions are very welcome, as are feature requests and suggestions. Please open an

vendor/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# JSONX - Simple JSON Parser
2+
3+
A simple, no-dependency JSON parser that can be vendored (copied/pasted) into other packages.
4+
5+
## Features
6+
7+
### Parsing
8+
- `JSONX.parse(json_str::String)` - Parse a JSON string
9+
- `JSONX.parse(bytes::AbstractVector{UInt8})` - Parse JSON from byte array
10+
- `JSONX.parsefile(filename::String)` - Parse JSON from a file
11+
12+
### Writing
13+
- `JSONX.json(value)` - Convert a Julia value to JSON string
14+
15+
### Supported Types
16+
17+
**Reading (JSON → Julia):**
18+
- `null``nothing`
19+
- `true`/`false``Bool`
20+
- Numbers → `Float64` (all numbers are parsed as Float64)
21+
- Strings → `String` (with full Unicode support)
22+
- Arrays → `Vector{Any}`
23+
- Objects → `Dict{String, Any}`
24+
25+
**Writing (Julia → JSON):**
26+
- `nothing`/`missing``null`
27+
- `Bool``true`/`false`
28+
- `Number` → JSON number
29+
- `AbstractString` → JSON string
30+
- `AbstractVector`/`AbstractSet`/`Tuple` → JSON array
31+
- `AbstractDict`/`NamedTuple` → JSON object
32+
- `Symbol`/`Enum` → JSON string
33+
34+
### Unicode Support
35+
36+
JSONX includes full Unicode support:
37+
- Proper Unicode escape sequence parsing (`\uXXXX`)
38+
- UTF-16 surrogate pair handling
39+
- Lone surrogate handling
40+
- All standard JSON escape sequences (`\"`, `\\`, `\/`, `\b`, `\f`, `\n`, `\r`, `\t`)
41+
42+
## Usage
43+
44+
```julia
45+
using JSONX
46+
47+
# Parse JSON
48+
data = JSONX.parse("{\"name\":\"John\",\"age\":30}")
49+
# Returns: Dict("name" => "John", "age" => 30.0)
50+
51+
# Parse from bytes
52+
bytes = Vector{UInt8}("{\"key\":\"value\"}")
53+
data = JSONX.parse(bytes)
54+
55+
# Parse from file
56+
data = JSONX.parsefile("data.json")
57+
58+
# Write JSON
59+
json_str = JSONX.json(Dict("a" => 1, "b" => 2))
60+
# Returns: "{\"a\":1,\"b\":2}"
61+
62+
# Unicode examples
63+
JSONX.parse("\"Hello 世界! 🌍\"") # Full Unicode support
64+
JSONX.parse("\"\\u0048\\u0065\\u006C\\u006C\\u006F\"") # Unicode escapes
65+
```
66+
67+
## Error Handling
68+
69+
JSONX provides detailed error messages for invalid JSON:
70+
- Unexpected end of input
71+
- Invalid escape sequences
72+
- Malformed Unicode escapes
73+
- Trailing commas
74+
- Control characters in strings
75+
- Invalid number formats
76+
77+
## Limitations
78+
79+
Compared to the full JSON.jl package, JSONX is intentionally simplified:
80+
81+
- **No integer parsing**: All numbers are parsed as Float64
82+
- **No custom type parsing**: Only returns basic Julia types
83+
- **No configuration options**: Uses fixed defaults
84+
- **No streaming**: Loads entire input into memory
85+
- **No pretty printing**: Output is compact only
86+
- **No schema validation**: Basic JSON validation only
87+
- **No performance optimizations**: Simple, readable implementation
88+
89+
## Implementation Notes
90+
91+
- **No dependencies**: Uses only Base Julia functionality
92+
- **Byte-level processing**: Uses `codeunit` for accurate string handling
93+
- **Memory efficient**: Avoids unnecessary string concatenation
94+
- **Error robust**: Comprehensive error checking and reporting
95+
96+
Note: Functions are not exported, so use `JSONX.parse` and `JSONX.json` with the module prefix.

0 commit comments

Comments
 (0)