From cbdc0498e893f7beb6c7602de1bc82bed4673e05 Mon Sep 17 00:00:00 2001 From: hhaensel Date: Thu, 22 Jan 2026 14:20:09 +0100 Subject: [PATCH 1/4] add JSONText methods for Symbol and JSONText --- src/JSON.jl | 2 ++ test/json.jl | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/JSON.jl b/src/JSON.jl index fcc30e0..046c0ce 100644 --- a/src/JSON.jl +++ b/src/JSON.jl @@ -70,6 +70,8 @@ x = JSON.parse("[1,2,3]", JSONText) struct JSONText value::String end +JSON.JSONText(js::Symbol) = JSONText(String(js)) +JSON.JSONText(js::JSONText) = js include("lazy.jl") include("parse.jl") diff --git a/test/json.jl b/test/json.jl index 8725b43..07d2e06 100644 --- a/test/json.jl +++ b/test/json.jl @@ -107,7 +107,7 @@ end arr[3] = "b" @test JSON.json(arr) == "[\"a\",null,\"b\"]" # test custom struct writing - # defined in the test/struct.jl file + # defined in the test/parse.jl file a = A(1, 2, 3, 4) @test JSON.json(a) == "{\"a\":1,\"b\":2,\"c\":3,\"d\":4}" x = LotsOfFields("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35") @@ -256,6 +256,8 @@ end @test JSON.json(Dict(Point(1, 2) => "hi")) == "{\"1_2\":\"hi\"}" x = JSONText("[1,2,3]") @test JSON.json(x) == "[1,2,3]" + @test JSONText(x) == x + @test x == JSONText(Symbol("[1,2,3]")) @test JSON.json((a=1, b=nothing)) == "{\"a\":1,\"b\":null}" @test JSON.json((a=1, b=nothing); omit_null=true) == "{\"a\":1}" @test JSON.json((a=1, b=nothing); omit_null=false) == "{\"a\":1,\"b\":null}" From a6250a93181492f8a4392e4c4dd6fdaf01dcd65b Mon Sep 17 00:00:00 2001 From: hhaensel Date: Thu, 12 Feb 2026 11:40:34 +0100 Subject: [PATCH 2/4] remove JSONText for Symbols --- src/JSON.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/JSON.jl b/src/JSON.jl index 046c0ce..652f334 100644 --- a/src/JSON.jl +++ b/src/JSON.jl @@ -70,7 +70,6 @@ x = JSON.parse("[1,2,3]", JSONText) struct JSONText value::String end -JSON.JSONText(js::Symbol) = JSONText(String(js)) JSON.JSONText(js::JSONText) = js include("lazy.jl") From c770cc52b11295dc01eec80254014e2e77938fe5 Mon Sep 17 00:00:00 2001 From: hhaensel Date: Thu, 12 Feb 2026 11:42:39 +0100 Subject: [PATCH 3/4] add and publicize `@js_str` macro --- src/JSON.jl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/JSON.jl b/src/JSON.jl index 652f334..c57ca06 100644 --- a/src/JSON.jl +++ b/src/JSON.jl @@ -72,6 +72,34 @@ struct JSONText end JSON.JSONText(js::JSONText) = js +VERSION >= v"1.11" && eval(Meta.parse("public @js_str")) + +""" + js" -> JSONText + +Construct a JSONText. String interpolation is supported via the `i` flag. +### Examples +``` +js\"\"\"alert("Hello World")\"\"\" +# JSONText("alert(\"Hello World\")") + +js\"\"\"alert("1 + 2 == \$(1 + 2)")\"\"\"i +# JSONText("alert(\"1 + 2 == 3\")") +``` +""" +macro js_str(s) + :( JSONText($(esc(s))) ) +end + +macro js_str(s, flags) + flags == "i" || @warn "Only 'i' flag currently supported (string interpolation)." + if 'i' in flags + :( JSONText($(esc(Meta.parse("\"$s\"")))) ) + else + :( JSONText($(esc(s))) ) + end +end + include("lazy.jl") include("parse.jl") include("write.jl") From c39cb6803cf468a28743da03eed452f41473ac44 Mon Sep 17 00:00:00 2001 From: hhaensel Date: Wed, 25 Feb 2026 07:58:43 +0100 Subject: [PATCH 4/4] remove JSONText(Symbol(...)) from tests --- test/json.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/json.jl b/test/json.jl index 07d2e06..f4341d1 100644 --- a/test/json.jl +++ b/test/json.jl @@ -257,7 +257,6 @@ end x = JSONText("[1,2,3]") @test JSON.json(x) == "[1,2,3]" @test JSONText(x) == x - @test x == JSONText(Symbol("[1,2,3]")) @test JSON.json((a=1, b=nothing)) == "{\"a\":1,\"b\":null}" @test JSON.json((a=1, b=nothing); omit_null=true) == "{\"a\":1}" @test JSON.json((a=1, b=nothing); omit_null=false) == "{\"a\":1,\"b\":null}"