Skip to content

Commit

Permalink
Merge pull request #227 from GenieFramework/hh-jsontext
Browse files Browse the repository at this point in the history
interoperability for Stipple.JSONText and JSON.JSONText
  • Loading branch information
hhaensel authored Oct 17, 2023
2 parents f34daea + 1dbb1a8 commit 252b37f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
16 changes: 10 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,43 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[weakdeps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"

[extensions]
StippleDataFramesExt = "DataFrames"
StippleOffsetArraysExt = "OffsetArrays"
StippleJSONExt = "JSON"

[compat]
DataFrames = "1"
FilePathsBase = "0.9"
Genie = "5.18"
GenieSession = "1"
GenieSessionFileSession = "1"
JSON3 = "1.9"
JSON = "0.20, 0.21"
MacroTools = "0.5"
Mixers = "0.1.2"
Observables = "0.3, 0.4, 0.5"
OffsetArrays = "1"
OrderedCollections = "1"
Parameters = "0.12"
Reexport = "1"
Requires = "1"
StructTypes = "1.8"
julia = "1.6"
OffsetArrays = "1"
DataFrames = "1"
Tables = "1"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "DataFrames", "OffsetArrays"]
test = ["Test", "DataFrames", "JSON", "OffsetArrays"]
10 changes: 10 additions & 0 deletions ext/StippleJSON.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# garantee interoperability of different JSONTText definitions of JSON3 and JSON

JSON.JSONText(json::Stipple.JSONText) = JSON.JSONText(json.s)
JSON.show_json(io::JSON.Writer.CompactContext, s::JSON.Writer.CS, json::Stipple.JSONText) = write(io, json.s)
JSON.Writer.lower(json::Stipple.JSONText) = json.s

Stipple.JSONText(json::JSON.JSONText) = Stipple.JSONText(json.s)
@inline StructTypes.StructType(::Type{JSON.JSONText}) = JSON3.RawType()
@inline StructTypes.construct(::Type{JSON.JSONText}, json::JSON3.RawValue) = JSONText(string(json))
@inline JSON3.rawbytes(json::JSON.JSONText) = codeunits(json.s)
20 changes: 20 additions & 0 deletions ext/StippleJSONExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module StippleJSONExt

using Stipple
using JSON

# garantee interoperability of different JSONTText definitions in Stipple and JSON
# for both JSON3 and JSON
# Note that `lower` for Stipple.JSONText is not defined as parse(json.s), because that would require
# pure proper JSON. For transmissions of bindings, though, we need to allow to pass object names.

JSON.JSONText(json::Stipple.JSONText) = JSON.JSONText(json.s)
JSON.show_json(io::JSON.Writer.CompactContext, s::JSON.Writer.CS, json::Stipple.JSONText) = write(io, json.s)
JSON.Writer.lower(json::Stipple.JSONText) = json.s

Stipple.JSONText(json::JSON.JSONText) = Stipple.JSONText(json.s)
@inline StructTypes.StructType(::Type{JSON.JSONText}) = JSON3.RawType()
@inline StructTypes.construct(::Type{JSON.JSONText}, json::JSON3.RawValue) = JSONText(string(json))
@inline JSON3.rawbytes(json::JSON.JSONText) = codeunits(json.s)

end
10 changes: 9 additions & 1 deletion src/Stipple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ existing Vue.js libraries.
"""
module Stipple

const PRECOMPILE = Ref(false)

"""
@using_except(expr)
Expand Down Expand Up @@ -216,6 +218,12 @@ function __init__()
include(joinpath(@__DIR__, "..", "ext", "StippleDataFrames.jl"))
# Core.eval(@__MODULE__, Meta.parse(join(jl, ';')).args[3])
end

@require JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" begin
# evaluate the code of the extension without the surrounding module
include(joinpath(@__DIR__, "..", "ext", "StippleJSON.jl"))
# Core.eval(@__MODULE__, Meta.parse(join(jl, ';')).args[3])
end
end
end

Expand Down Expand Up @@ -443,7 +451,7 @@ function init(::Type{M};
# add a timer that checks if the model is outdated and if so prepare the model to be garbage collected
LAST_ACTIVITY[Symbol(getchannel(model))] = now()

Timer(setup_purge_checker(model), PURGE_CHECK_DELAY[], interval = PURGE_CHECK_DELAY[])
PRECOMPILE[] || Timer(setup_purge_checker(model), PURGE_CHECK_DELAY[], interval = PURGE_CHECK_DELAY[])

if is_channels_webtransport()
Genie.Assets.channels_subscribe(channel)
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,15 @@ end
@test cell(sm = 9) == "<div class=\"st-col col col-sm-9\"></div>"

@test cell(col = -1, sm = 9) == "<div class=\"st-col col-sm-9\"></div>"
end

@testset "Compatibility of JSONText between JSON3 and JSON" begin
using JSON
using Stipple
jt1 = JSON.JSONText("json text 1")
jt2 = Stipple.JSONText("json text 2")
@test JSON.json(jt1) == "json text 1"
@test Stipple.json(jt1) == "json text 1"
@test JSON.json(jt2) == "json text 2"
@test Stipple.json(jt2) == "json text 2"
end

0 comments on commit 252b37f

Please sign in to comment.