Skip to content

Commit 006c4e3

Browse files
authored
Merge pull request #65 from baumgold/imprecise_big_numbers
#64 fix display precision of big numbers
2 parents 48ca384 + 5dd10f6 commit 006c4e3

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "TableView"
22
uuid = "40c74d1a-b44c-5b06-a7c1-6cbea58ea978"
3-
version = "0.6.4"
3+
version = "0.6.5"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/TableView.jl

+19-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using Observables: @map
77
export showtable
88

99
const ag_grid_imports = []
10+
const js_max_safe_int = Int128(2^53-1)
1011

1112
function __init__()
1213
version = readchomp(joinpath(@__DIR__, "..", "ag-grid.version"))
@@ -252,32 +253,28 @@ end
252253
# directly write JSON instead of allocating temporary dicts etc
253254
function table2json(schema, rows, types; requested = nothing)
254255
io = IOBuffer()
255-
print(io, '[')
256+
rowwriter = JSON.Writer.CompactContext(io)
257+
JSON.begin_array(rowwriter)
258+
ser = JSON.StandardSerialization()
256259
for (i, row) in enumerate(rows)
257-
if requested == nothing || first(requested) <= i <= last(requested)
258-
print(io, '{')
259-
Tables.eachcolumn(schema, row) do val, ind, name
260-
JSON.print(io, name)
261-
print(io, ':')
262-
if val isa Number && isfinite(val)
263-
JSON.print(io, val)
264-
elseif val === nothing
265-
JSON.print(io, repr(nothing))
266-
elseif val === missing
267-
JSON.print(io, repr(missing))
268-
else
269-
JSON.print(io, sprint(print, val))
270-
end
271-
print(io, ',')
260+
if requested != nothing && (i < first(requested) || i > last(requested))
261+
continue
262+
end
263+
JSON.delimit(rowwriter)
264+
columnwriter = JSON.Writer.CompactContext(io)
265+
JSON.begin_object(columnwriter)
266+
Tables.eachcolumn(schema, row) do val, ind, name
267+
if val isa Real && isfinite(val) && -js_max_safe_int < trunc(Int128, val) < js_max_safe_int
268+
JSON.show_pair(columnwriter, ser, name, val)
269+
elseif val === nothing || val === missing
270+
JSON.show_pair(columnwriter, ser, name, repr(val))
271+
else
272+
JSON.show_pair(columnwriter, ser, name, sprint(print, val))
272273
end
273-
skip(io, -1)
274-
print(io, '}')
275-
print(io, ',')
276274
end
275+
JSON.end_object(columnwriter)
277276
end
278-
skip(io, -1)
279-
print(io, ']')
280-
277+
JSON.end_array(rowwriter)
281278
String(take!(io))
282279
end
283280
end

test/runtests.jl

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ end
3232
@test firstrow["d"] == 0
3333
@test firstrow["e"] == "test_missing"
3434
end
35+
@testset "large integers" begin
36+
rows = Tables.table([2^52 2^53 2^54])
37+
names = [:a, :b, :c]
38+
types = [Int64 for _ in 1:3]
39+
json = TableView.table2json(Tables.Schema(names, types), rows, types)
40+
firstrow = JSON.parse(json)[1]
41+
@test firstrow["a"] == 4503599627370496
42+
@test firstrow["b"] == "9007199254740992"
43+
@test firstrow["c"] == "18014398509481984"
44+
end
3545
@testset "normal array" begin
3646
array = rand(10, 10)
3747
@test showtable(array) isa WebIO.Scope

0 commit comments

Comments
 (0)