Skip to content

Commit 0738a72

Browse files
author
Ben Baumgold
committed
#57 enable custom string representation of missing and nothing values
1 parent faaa7e0 commit 0738a72

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/TableView.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ function table2json(schema, rows, types; requested = nothing)
263263
if val isa Number && isfinite(val)
264264
JSON.print(io, val)
265265
elseif val === nothing
266-
JSON.print(io, "nothing")
266+
JSON.print(io, repr(nothing))
267267
elseif val === missing
268-
JSON.print(io, "missing")
268+
JSON.print(io, repr(missing))
269269
else
270270
JSON.print(io, sprint(print, val))
271271
end

test/runtests.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ end
2020
@test showtable(nttable) isa WebIO.Scope
2121
end
2222
@testset "inf and nan serializing" begin
23-
rows = Tables.table([NaN Inf -Inf 0])
24-
names = [:a, :b, :c, :d]
25-
types = [Float64 for _ in 1:4]
23+
rows = Tables.table([NaN Inf -Inf 0 missing nothing])
24+
names = [:a, :b, :c, :d, :e, :f]
25+
types = vcat([Float64 for _ in 1:4], [Missing, Nothing])
26+
Base.show(io::IO, x::Missing) = print(io, "test_missing")
27+
Base.show(io::IO, x::Nothing) = print(io, "test_nothing")
2628
json = TableView.table2json(Tables.Schema(names, types), rows, types)
2729
firstrow = JSON.parse(json)[1]
2830
@test firstrow["a"] == "NaN"
2931
@test firstrow["b"] == "Inf"
3032
@test firstrow["c"] == "-Inf"
3133
@test firstrow["d"] == 0
34+
@test firstrow["e"] == "test_missing"
35+
@test firstrow["f"] == "test_nothing"
3236
end
3337
@testset "normal array" begin
3438
array = rand(10, 10)

0 commit comments

Comments
 (0)