@@ -7,6 +7,7 @@ using Observables: @map
7
7
export showtable
8
8
9
9
const ag_grid_imports = []
10
+ const js_max_safe_int = Int128 (2 ^ 53 - 1 )
10
11
11
12
function __init__ ()
12
13
version = readchomp (joinpath (@__DIR__ , " .." , " ag-grid.version" ))
@@ -252,32 +253,28 @@ end
252
253
# directly write JSON instead of allocating temporary dicts etc
253
254
function table2json (schema, rows, types; requested = nothing )
254
255
io = IOBuffer ()
255
- print (io, ' [' )
256
+ rowwriter = JSON. Writer. CompactContext (io)
257
+ JSON. begin_array (rowwriter)
258
+ ser = JSON. StandardSerialization ()
256
259
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))
272
273
end
273
- skip (io, - 1 )
274
- print (io, ' }' )
275
- print (io, ' ,' )
276
274
end
275
+ JSON. end_object (columnwriter)
277
276
end
278
- skip (io, - 1 )
279
- print (io, ' ]' )
280
-
277
+ JSON. end_array (rowwriter)
281
278
String (take! (io))
282
279
end
283
280
end
0 commit comments