Skip to content

Commit

Permalink
fix json for Layout and Colorant, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Helmut Hänsel committed Mar 15, 2024
1 parent 76ffe57 commit 37486dd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ IJuliaExt = "IJulia"
JSON3Ext = "JSON3"

[extras]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Dates", "DelimitedFiles", "Test", "DataFrames"]
test = ["Colors", "DataFrames", "DelimitedFiles", "JSON3", "Dates", "Test"]

[weakdeps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
2 changes: 1 addition & 1 deletion ext/ColorsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module ColorsExt
isdefined(Base, :get_extension) ? (using Colors) : (using ..Colors)
using PlotlyBase

_json_lower(a::Colors.Colorant) = string("#", Colors.hex(a))
PlotlyBase._json_lower(a::Colorant) = "#$(hex(a, :auto))"

end
14 changes: 14 additions & 0 deletions ext/JSON2Ext.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module JSON2Ext

isdefined(Base, :get_extension) ? (using JSON2) : (using ..JSON2)

import PlotlyBase: Plot, JSON

function JSON2.write(io::IO, p::Plot)
# data = PlotlyBase._json_lower(p)
# pop!(data, :config, nothing)
println("hi")
print(io, json(p))
end

end
8 changes: 3 additions & 5 deletions ext/JSON3Ext.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
module JSON3Ext

isdefined(Base, :get_extension) ? (using JSON3) : (using ..JSON3)
isdefined(Base, :get_extension) ? (using JSON) : (using ..JSON)
using PlotlyBase

import PlotlyBase: Plot, HasFields, PlotConfig, JSON.json
const StructTypes = JSON3.StructTypes
const HasFields = PlotlyBase.HasFields

# StructTypes.StructType(::Type{T}) where T <: HasFields = StructTypes.DictType()
# StructTypes.construct(::Type{T}, x::AbstractDict; kw...) where T <: HasFields = T(_symbol_dict(x); kw...)
# StructTypes.StructType(::Type{T}) where T <: Union{Template} = StructTypes.Struct()
# StructTypes.StructType(::Type{T}) where T <: Union{Plot,PlotConfig} = StructTypes.Mutable()
# StructTypes.omitempties(::Type{PlotConfig}) = fieldnames(PlotConfig)

StructTypes.StructType(::Type{<:PlotlyBase.Plot}) = JSON3.RawType()
StructTypes.StructType(::Type{<:Plot}) = JSON3.RawType()
StructTypes.StructType(::Type{<:HasFields}) = JSON3.RawType()
StructTypes.StructType(::Type{PlotConfig}) = JSON3.RawType()

JSON3.rawbytes(x::Union{PlotlyBase.Plot,HasFields,PlotConfig}) = codeunits(JSON.json(x))
JSON3.rawbytes(x::Union{Plot, HasFields, PlotConfig}) = codeunits(json(x))

end
2 changes: 1 addition & 1 deletion src/json.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -------------------------------- #
# Custom JSON output for our types #
# -------------------------------- #
JSON.lower(a::HasFields) = a.fields
JSON.lower(a::HasFields) = _json_lower(a)
JSON.lower(c::Cycler) = c.vals
_json_lower(x) = JSON.lower(x)
_json_lower(x::Union{Bool,String,Number,Nothing,Missing}) = x
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand Down
6 changes: 6 additions & 0 deletions test/colors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Colors
using PlotlyBase.JSON
@testset "Colors" begin
@test JSON.parse(json(Layout(colortest = RGBA(1, 0.8, 0.6, 0.2))))["colortest"] == "#FFCC9933"
@test JSON.parse(json(Template(data = attr(scatter = [attr(marker_color = colorant"blue")]))))["data"]["scatter"][1]["marker"]["color"] == "#0000FF"
end

0 comments on commit 37486dd

Please sign in to comment.