Skip to content

Commit ba4834d

Browse files
author
Ben Baumgold
committed
#64 fix display precision of big numbers
1 parent d3e3cd7 commit ba4834d

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/TableView.jl

Lines changed: 17 additions & 22 deletions
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 = 2^53-1
1011

1112
function __init__()
1213
version = readchomp(joinpath(@__DIR__, "..", "ag-grid.version"))
@@ -252,32 +253,26 @@ 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 Number && isfinite(val) && -js_max_safe_int < val < js_max_safe_int
268+
JSON.show_pair(columnwriter, ser, name, val)
269+
else
270+
JSON.show_pair(columnwriter, ser, name, repr(MIME("text/plain"), val))
272271
end
273-
skip(io, -1)
274-
print(io, '}')
275-
print(io, ',')
276272
end
273+
JSON.end_object(columnwriter)
277274
end
278-
skip(io, -1)
279-
print(io, ']')
280-
275+
JSON.end_array(rowwriter)
281276
String(take!(io))
282277
end
283278
end

0 commit comments

Comments
 (0)