simdjson bindings with streaming support
at main 4.5 kB view raw
1module Json = Simdjsont_json 2 3let write_byte buf b = Buffer.add_char buf (Char.unsafe_chr b) 4 5let write_uint_body buf v = 6 if v < 24L then write_byte buf (Int64.to_int v) 7 else if v <= 0xFFL then ( 8 write_byte buf 24; 9 write_byte buf (Int64.to_int v)) 10 else if v <= 0xFFFFL then ( 11 write_byte buf 25; 12 let v_int = Int64.to_int v in 13 write_byte buf ((v_int lsr 8) land 0xFF); 14 write_byte buf (v_int land 0xFF)) 15 else if v <= 0xFFFFFFFFL then ( 16 write_byte buf 26; 17 write_byte buf (Int64.to_int (Int64.shift_right_logical v 24) land 0xFF); 18 write_byte buf (Int64.to_int (Int64.shift_right_logical v 16) land 0xFF); 19 write_byte buf (Int64.to_int (Int64.shift_right_logical v 8) land 0xFF); 20 write_byte buf (Int64.to_int v land 0xFF)) 21 else ( 22 write_byte buf 27; 23 write_byte buf (Int64.to_int (Int64.shift_right_logical v 56) land 0xFF); 24 write_byte buf (Int64.to_int (Int64.shift_right_logical v 48) land 0xFF); 25 write_byte buf (Int64.to_int (Int64.shift_right_logical v 40) land 0xFF); 26 write_byte buf (Int64.to_int (Int64.shift_right_logical v 32) land 0xFF); 27 write_byte buf (Int64.to_int (Int64.shift_right_logical v 24) land 0xFF); 28 write_byte buf (Int64.to_int (Int64.shift_right_logical v 16) land 0xFF); 29 write_byte buf (Int64.to_int (Int64.shift_right_logical v 8) land 0xFF); 30 write_byte buf (Int64.to_int v land 0xFF)) 31 32 33let write_head buf major v = 34 let major_bits = major lsl 5 in 35 if v < 24L then write_byte buf (major_bits lor Int64.to_int v) 36 else if v <= 0xFFL then ( 37 write_byte buf (major_bits lor 24); 38 write_byte buf (Int64.to_int v)) 39 else if v <= 0xFFFFL then ( 40 write_byte buf (major_bits lor 25); 41 let v_int = Int64.to_int v in 42 write_byte buf ((v_int lsr 8) land 0xFF); 43 write_byte buf (v_int land 0xFF)) 44 else if v <= 0xFFFFFFFFL then ( 45 write_byte buf (major_bits lor 26); 46 write_byte buf (Int64.to_int (Int64.shift_right_logical v 24) land 0xFF); 47 write_byte buf (Int64.to_int (Int64.shift_right_logical v 16) land 0xFF); 48 write_byte buf (Int64.to_int (Int64.shift_right_logical v 8) land 0xFF); 49 write_byte buf (Int64.to_int v land 0xFF)) 50 else ( 51 write_byte buf (major_bits lor 27); 52 write_byte buf (Int64.to_int (Int64.shift_right_logical v 56) land 0xFF); 53 write_byte buf (Int64.to_int (Int64.shift_right_logical v 48) land 0xFF); 54 write_byte buf (Int64.to_int (Int64.shift_right_logical v 40) land 0xFF); 55 write_byte buf (Int64.to_int (Int64.shift_right_logical v 32) land 0xFF); 56 write_byte buf (Int64.to_int (Int64.shift_right_logical v 24) land 0xFF); 57 write_byte buf (Int64.to_int (Int64.shift_right_logical v 16) land 0xFF); 58 write_byte buf (Int64.to_int (Int64.shift_right_logical v 8) land 0xFF); 59 write_byte buf (Int64.to_int v land 0xFF)) 60 61let rec write buf = function 62 | Json.Null -> write_byte buf 0xF6 63 | Json.Bool false -> write_byte buf 0xF4 64 | Json.Bool true -> write_byte buf 0xF5 65 | Json.Int i -> 66 if i >= 0L then write_head buf 0 i else write_head buf 1 (Int64.lognot i) 67 | Json.Float f -> 68 write_byte buf 0xFB; 69 let i = Int64.bits_of_float f in 70 write_byte buf (Int64.to_int (Int64.shift_right_logical i 56) land 0xFF); 71 write_byte buf (Int64.to_int (Int64.shift_right_logical i 48) land 0xFF); 72 write_byte buf (Int64.to_int (Int64.shift_right_logical i 40) land 0xFF); 73 write_byte buf (Int64.to_int (Int64.shift_right_logical i 32) land 0xFF); 74 write_byte buf (Int64.to_int (Int64.shift_right_logical i 24) land 0xFF); 75 write_byte buf (Int64.to_int (Int64.shift_right_logical i 16) land 0xFF); 76 write_byte buf (Int64.to_int (Int64.shift_right_logical i 8) land 0xFF); 77 write_byte buf (Int64.to_int i land 0xFF) 78 | Json.String s -> 79 let len = String.length s in 80 write_head buf 3 (Int64.of_int len); 81 Buffer.add_string buf s 82 | Json.Array items -> 83 let len = List.length items in 84 write_head buf 4 (Int64.of_int len); 85 List.iter (write buf) items 86 | Json.Object members -> 87 let len = List.length members in 88 write_head buf 5 (Int64.of_int len); 89 List.iter 90 (fun (k, v) -> 91 let klen = String.length k in 92 write_head buf 3 (Int64.of_int klen); 93 Buffer.add_string buf k; 94 write buf v) 95 members 96 97let to_buffer ?(capacity = 256) v = 98 let buf = Buffer.create capacity in 99 write buf v; 100 buf 101 102let to_string v = 103 let buf = Buffer.create 256 in 104 write buf v; 105 Buffer.contents buf