Skip to content

Commit ac1e1c9

Browse files
authored
Implement JSONX.JSONText (#394)
1 parent a2aa9ed commit ac1e1c9

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

vendor/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ A simple, no-dependency JSON parser that can be vendored (copied/pasted) into ot
1111

1212
### Writing
1313
- `JSONX.json(value)` - Convert a Julia value to JSON string
14+
- `JSONX.JSONText(str)` - `str` will be treated as raw JSON and inserted
15+
directly into the output.
1416

1517
### Supported Types
1618

vendor/jsonx.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ function parse_number(str::String, pos::Int, len::Int)
263263
end
264264

265265
# JSON writing functionality
266+
267+
struct JSONText
268+
text::String
269+
end
270+
266271
function write_json(io::IO, value)
267272
if value === nothing || value === missing
268273
print(io, "null")
@@ -271,6 +276,8 @@ function write_json(io::IO, value)
271276
elseif value isa Number
272277
value isa Complex && throw(ArgumentError("Cannot serialize Complex numbers to JSON"))
273278
print(io, value)
279+
elseif value isa JSONText
280+
print(io, value.text)
274281
elseif value isa AbstractString
275282
write_string(io, value)
276283
elseif value isa AbstractVector || value isa AbstractSet || value isa Tuple

vendor/test.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ include("jsonx.jl")
109109
@test JSONX.json((1, 2, 3)) == "[1,2,3]"
110110
# Note: NamedTuple order is not guaranteed, so we parse and compare
111111
@test JSONX.parse(JSONX.json((a=1, b=2))) == Dict("a" => 1.0, "b" => 2.0)
112+
# Test JSONText
113+
@test JSONX.json(JSONX.JSONText("{\"x\": invalid json}")) == "{\"x\": invalid json}"
112114
end
113115

114116
@testset "AbstractSet Support" begin

0 commit comments

Comments
 (0)