Skip to content

Commit

Permalink
Merge pull request #65 from baumgold/imprecise_big_numbers
Browse files Browse the repository at this point in the history
#64 fix display precision of big numbers
  • Loading branch information
pfitzseb authored Mar 16, 2021
2 parents 48ca384 + 5dd10f6 commit 006c4e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "TableView"
uuid = "40c74d1a-b44c-5b06-a7c1-6cbea58ea978"
version = "0.6.4"
version = "0.6.5"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
41 changes: 19 additions & 22 deletions src/TableView.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Observables: @map
export showtable

const ag_grid_imports = []
const js_max_safe_int = Int128(2^53-1)

function __init__()
version = readchomp(joinpath(@__DIR__, "..", "ag-grid.version"))
Expand Down Expand Up @@ -252,32 +253,28 @@ end
# directly write JSON instead of allocating temporary dicts etc
function table2json(schema, rows, types; requested = nothing)
io = IOBuffer()
print(io, '[')
rowwriter = JSON.Writer.CompactContext(io)
JSON.begin_array(rowwriter)
ser = JSON.StandardSerialization()
for (i, row) in enumerate(rows)
if requested == nothing || first(requested) <= i <= last(requested)
print(io, '{')
Tables.eachcolumn(schema, row) do val, ind, name
JSON.print(io, name)
print(io, ':')
if val isa Number && isfinite(val)
JSON.print(io, val)
elseif val === nothing
JSON.print(io, repr(nothing))
elseif val === missing
JSON.print(io, repr(missing))
else
JSON.print(io, sprint(print, val))
end
print(io, ',')
if requested != nothing && (i < first(requested) || i > last(requested))
continue
end
JSON.delimit(rowwriter)
columnwriter = JSON.Writer.CompactContext(io)
JSON.begin_object(columnwriter)
Tables.eachcolumn(schema, row) do val, ind, name
if val isa Real && isfinite(val) && -js_max_safe_int < trunc(Int128, val) < js_max_safe_int
JSON.show_pair(columnwriter, ser, name, val)
elseif val === nothing || val === missing
JSON.show_pair(columnwriter, ser, name, repr(val))
else
JSON.show_pair(columnwriter, ser, name, sprint(print, val))
end
skip(io, -1)
print(io, '}')
print(io, ',')
end
JSON.end_object(columnwriter)
end
skip(io, -1)
print(io, ']')

JSON.end_array(rowwriter)
String(take!(io))
end
end
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ end
@test firstrow["d"] == 0
@test firstrow["e"] == "test_missing"
end
@testset "large integers" begin
rows = Tables.table([2^52 2^53 2^54])
names = [:a, :b, :c]
types = [Int64 for _ in 1:3]
json = TableView.table2json(Tables.Schema(names, types), rows, types)
firstrow = JSON.parse(json)[1]
@test firstrow["a"] == 4503599627370496
@test firstrow["b"] == "9007199254740992"
@test firstrow["c"] == "18014398509481984"
end
@testset "normal array" begin
array = rand(10, 10)
@test showtable(array) isa WebIO.Scope
Expand Down

0 comments on commit 006c4e3

Please sign in to comment.