From 0690cfb3c445dffc7cf0938b10789bdb1a1ddfd0 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Fri, 12 May 2017 23:11:42 -0700 Subject: [PATCH 1/2] Replace lower on TimeType with show_json --- src/Writer.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Writer.jl b/src/Writer.jl index 3c3e434..de670d1 100644 --- a/src/Writer.jl +++ b/src/Writer.jl @@ -43,14 +43,13 @@ function lower(a) end end -lower(s::Base.Dates.TimeType) = string(s) - # To avoid allocating an intermediate string, we directly define `show_json` # for this type instead of lowering it to a string first (which would # allocate). However, the `show_json` method does call `lower` so as to allow # users to change the lowering of their `Enum` or even `AbstractString` # subtypes if necessary. -const IsPrintedAsString = Union{Char, Type, AbstractString, Enum, Symbol} +const IsPrintedAsString = Union{ + Dates.TimeType, Char, Type, AbstractString, Enum, Symbol} lower(x::IsPrintedAsString) = x lower(m::Module) = throw(ArgumentError("cannot serialize Module $m as JSON")) From 570412bb97fa008fe8d5872bbb375c777dd18de8 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Fri, 12 May 2017 23:26:09 -0700 Subject: [PATCH 2/2] Add support for serializing Set --- src/Writer.jl | 1 + test/standard-serializer.jl | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/Writer.jl b/src/Writer.jl index de670d1..48bc97a 100644 --- a/src/Writer.jl +++ b/src/Writer.jl @@ -54,6 +54,7 @@ lower(x::IsPrintedAsString) = x lower(m::Module) = throw(ArgumentError("cannot serialize Module $m as JSON")) lower(x::Real) = Float64(x) +lower(x::Base.AbstractSet) = collect(x) """ Abstract supertype of all JSON and JSON-like structural writer contexts. diff --git a/test/standard-serializer.jl b/test/standard-serializer.jl index c63eb8f..0e82176 100644 --- a/test/standard-serializer.jl +++ b/test/standard-serializer.jl @@ -57,3 +57,8 @@ end #Multidimensional arrays @test json([0 1; 2 0]) == "[[0,2],[1,0]]" end + +@testset "Sets" begin + @test json(Set()) == "[]" + @test json(Set([1, 2])) in ["[1,2]", "[2,1]"] +end