diff --git a/Project.toml b/Project.toml index eb026ee..38c73c5 100644 --- a/Project.toml +++ b/Project.toml @@ -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" \ No newline at end of file +JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" diff --git a/ext/ColorsExt.jl b/ext/ColorsExt.jl index 0b97177..9538fa1 100644 --- a/ext/ColorsExt.jl +++ b/ext/ColorsExt.jl @@ -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 \ No newline at end of file diff --git a/ext/JSON2Ext.jl b/ext/JSON2Ext.jl new file mode 100644 index 0000000..9402c9f --- /dev/null +++ b/ext/JSON2Ext.jl @@ -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 \ No newline at end of file diff --git a/ext/JSON3Ext.jl b/ext/JSON3Ext.jl index 4ea2171..82d264d 100644 --- a/ext/JSON3Ext.jl +++ b/ext/JSON3Ext.jl @@ -1,11 +1,9 @@ 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...) @@ -13,10 +11,10 @@ const HasFields = PlotlyBase.HasFields # 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 \ No newline at end of file diff --git a/src/json.jl b/src/json.jl index 83d6629..fee81f8 100644 --- a/src/json.jl +++ b/src/json.jl @@ -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 diff --git a/test/Project.toml b/test/Project.toml index 0364274..dde1a95 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -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" diff --git a/test/colors.jl b/test/colors.jl new file mode 100644 index 0000000..91e1597 --- /dev/null +++ b/test/colors.jl @@ -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 \ No newline at end of file