Skip to content

Commit

Permalink
Merge pull request #206 from JuliaIO/fw/lower-set
Browse files Browse the repository at this point in the history
Lower `AbstractSet` through `collect`
  • Loading branch information
TotalVerb authored May 16, 2017
2 parents 4f301b0 + 570412b commit 6578984
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ 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"))
lower(x::Real) = Float64(x)
lower(x::Base.AbstractSet) = collect(x)

"""
Abstract supertype of all JSON and JSON-like structural writer contexts.
Expand Down
5 changes: 5 additions & 0 deletions test/standard-serializer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6578984

Please sign in to comment.