diff --git a/bin/dune b/bin/dune index 3c6532c2..adb396ec 100644 --- a/bin/dune +++ b/bin/dune @@ -1,5 +1,6 @@ (executable (name ydump) (public_name ydump) + (package yojson) (flags (-safe-string)) (libraries yojson)) diff --git a/bin/ydump.ml b/bin/ydump.ml index 0565456e..613f18fa 100644 --- a/bin/ydump.ml +++ b/bin/ydump.ml @@ -81,7 +81,7 @@ let cat sort output_biniou std compact streaming in_file out_file = else let write_one oc x = - output_string oc (Bi_io.string_of_tree (Yojson_biniou.biniou_of_json x)) + output_string oc (Yojson.Safe.to_string x) in polycat write_one streaming in_file out_file diff --git a/lib/common.ml b/lib/common.ml index 935b2c8b..df94ac11 100644 --- a/lib/common.ml +++ b/lib/common.ml @@ -19,7 +19,7 @@ let create_in_param ?(len = 256) () = { let utf8_of_code buf x = - let add = Bi_outbuf.add_char in + let add = Buffer.add_char in (* Straight <= doesn't work with signed 31-bit ints *) let maxbits n x = x lsr n = 0 in @@ -80,7 +80,7 @@ let is_object_or_array x = type lexer_state = { - buf : Bi_outbuf.t; + buf : Buffer.t; (* Buffer used to accumulate substrings *) mutable lnum : int; @@ -97,7 +97,7 @@ type lexer_state = { module Lexer_state = struct type t = lexer_state = { - buf : Bi_outbuf.t; + buf : Buffer.t; mutable lnum : int; mutable bol : int; mutable fname : string option; @@ -107,7 +107,7 @@ end let init_lexer ?buf ?fname ?(lnum = 1) () = let buf = match buf with - None -> Bi_outbuf.create 256 + None -> Buffer.create 256 | Some buf -> buf in { diff --git a/lib/common.mli b/lib/common.mli index 415b3630..28e90aec 100644 --- a/lib/common.mli +++ b/lib/common.mli @@ -5,7 +5,7 @@ exception Json_error of string val json_error : string -> 'a type lexer_state = { - buf : Bi_outbuf.t; + buf : Buffer.t; (** Buffer used to accumulate substrings *) mutable lnum : int; @@ -22,7 +22,7 @@ type lexer_state = { module Lexer_state : sig type t = lexer_state = { - buf : Bi_outbuf.t; + buf : Buffer.t; mutable lnum : int; mutable bol : int; mutable fname : string option; @@ -30,7 +30,7 @@ sig end val init_lexer : - ?buf: Bi_outbuf.t -> + ?buf: Buffer.t -> ?fname: string -> ?lnum: int -> unit -> lexer_state diff --git a/lib/dune b/lib/dune index 5eaecea4..6b817071 100644 --- a/lib/dune +++ b/lib/dune @@ -33,8 +33,6 @@ (library (name yojson) (public_name yojson) - (modules yojson yojson_biniou) - (wrapped false) - (synopsis "JSON parsing and printing (successor of json-wheel)") - (flags (-safe-string)) - (libraries biniou)) + (modules yojson) + (synopsis "JSON parsing and printing") + (flags (-safe-string))) diff --git a/lib/read.mli b/lib/read.mli index 31dd1b96..b1c2c9fa 100644 --- a/lib/read.mli +++ b/lib/read.mli @@ -12,7 +12,7 @@ exception Finally of exn * exn (** Exception describing a failure in both finalizer and parsing. *) val from_string : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t @@ -25,7 +25,7 @@ val from_string : *) val from_channel : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> in_channel -> t @@ -33,7 +33,7 @@ val from_channel : See [from_string] for the meaning of the optional arguments. *) val from_file : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t @@ -42,7 +42,7 @@ val from_file : type lexer_state = Lexer_state.t = { - buf : Bi_outbuf.t; + buf : Buffer.t; mutable lnum : int; mutable bol : int; mutable fname : string option; @@ -52,7 +52,7 @@ type lexer_state = Lexer_state.t = { *) val init_lexer : - ?buf: Bi_outbuf.t -> + ?buf: Buffer.t -> ?fname: string -> ?lnum: int -> unit -> lexer_state @@ -72,7 +72,7 @@ val from_lexbuf : the end of the JSON value and the end of the input. *) val stream_from_string : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t Stream.t @@ -81,7 +81,7 @@ val stream_from_string : See [from_string] for the meaning of the optional arguments. *) val stream_from_channel : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fin:(unit -> unit) -> ?fname:string -> ?lnum:int -> @@ -97,7 +97,7 @@ val stream_from_channel : See [from_string] for the meaning of the other optional arguments. *) val stream_from_file : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> t Stream.t @@ -124,7 +124,7 @@ type json_line = [ `Json of t | `Exn of exn ] (** The type of values resulting from a parsing attempt of a JSON value. *) val linestream_from_channel : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fin:(unit -> unit) -> ?fname:string -> ?lnum:int -> @@ -138,7 +138,7 @@ val linestream_from_channel : See [from_string] for the meaning of the other optional arguments. *) val linestream_from_file : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> string -> json_line Stream.t diff --git a/lib/read.mll b/lib/read.mll index 5bdaf837..3e7a4d2f 100644 --- a/lib/read.mll +++ b/lib/read.mll @@ -27,7 +27,7 @@ (* see description in common.mli *) type lexer_state = Lexer_state.t = { - buf : Bi_outbuf.t; + buf : Buffer.t; mutable lnum : int; mutable bol : int; mutable fname : string option; @@ -147,7 +147,7 @@ let add_lexeme buf lexbuf = let len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in - Bi_outbuf.add_subbytes buf lexbuf.lex_buffer lexbuf.lex_start_pos len + Buffer.add_subbytes buf lexbuf.lex_buffer lexbuf.lex_start_pos len let map_lexeme f lexbuf = let len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in @@ -211,7 +211,7 @@ rule read_json v = parse } | '"' { #ifdef STRING - Bi_outbuf.clear v.buf; + Buffer.clear v.buf; `String (finish_string v lexbuf) #elif defined STRINGLIT `Stringlit (finish_stringlit v lexbuf) @@ -308,7 +308,7 @@ rule read_json v = parse and finish_string v = parse - '"' { Bi_outbuf.contents v.buf } + '"' { Buffer.contents v.buf } | '\\' { finish_escaped_char v lexbuf; finish_string v lexbuf } | [^ '"' '\\']+ { add_lexeme v.buf lexbuf; @@ -317,7 +317,7 @@ and finish_string v = parse and map_string v f = parse '"' { let b = v.buf in - f (Bytes.to_string b.Bi_outbuf.o_s) 0 b.Bi_outbuf.o_len } + f (Buffer.contents b) 0 (Buffer.length b) } | '\\' { finish_escaped_char v lexbuf; map_string v f lexbuf } | [^ '"' '\\']+ { add_lexeme v.buf lexbuf; @@ -327,12 +327,12 @@ and map_string v f = parse and finish_escaped_char v = parse '"' | '\\' - | '/' as c { Bi_outbuf.add_char v.buf c } - | 'b' { Bi_outbuf.add_char v.buf '\b' } - | 'f' { Bi_outbuf.add_char v.buf '\012' } - | 'n' { Bi_outbuf.add_char v.buf '\n' } - | 'r' { Bi_outbuf.add_char v.buf '\r' } - | 't' { Bi_outbuf.add_char v.buf '\t' } + | '/' as c { Buffer.add_char v.buf c } + | 'b' { Buffer.add_char v.buf '\b' } + | 'f' { Buffer.add_char v.buf '\012' } + | 'n' { Buffer.add_char v.buf '\n' } + | 'r' { Buffer.add_char v.buf '\r' } + | 't' { Buffer.add_char v.buf '\t' } | 'u' (hex as a) (hex as b) (hex as c) (hex as d) { let x = (hex a lsl 12) lor (hex b lsl 8) lor (hex c lsl 4) lor hex d @@ -398,7 +398,7 @@ and read_comma v = parse and start_any_variant v = parse '<' { `Edgy_bracket } - | '"' { Bi_outbuf.clear v.buf; + | '"' { Buffer.clear v.buf; `Double_quote } | '[' { `Square_bracket } | _ { long_error "Expected '<', '\"' or '[' but found" v lexbuf } @@ -454,7 +454,7 @@ and read_int v = parse with Int_overflow -> lexer_error "Int overflow" v lexbuf } | '"' { (* Support for double-quoted "ints" *) - Bi_outbuf.clear v.buf; + Buffer.clear v.buf; let s = finish_string v lexbuf in try (* Any OCaml-compliant int will pass, @@ -475,7 +475,7 @@ and read_int32 v = parse with _ -> lexer_error "Int32 overflow" v lexbuf } | '"' { (* Support for double-quoted "ints" *) - Bi_outbuf.clear v.buf; + Buffer.clear v.buf; let s = finish_string v lexbuf in try (* Any OCaml-compliant int will pass, @@ -496,7 +496,7 @@ and read_int64 v = parse with _ -> lexer_error "Int32 overflow" v lexbuf } | '"' { (* Support for double-quoted "ints" *) - Bi_outbuf.clear v.buf; + Buffer.clear v.buf; let s = finish_string v lexbuf in try (* Any OCaml-compliant int will pass, @@ -517,7 +517,7 @@ and read_number v = parse | "Infinity" { infinity } | "-Infinity" { neg_infinity } | number { float_of_string (lexeme lexbuf) } - | '"' { Bi_outbuf.clear v.buf; + | '"' { Buffer.clear v.buf; let s = finish_string v lexbuf in try (* Any OCaml-compliant float will pass, @@ -539,13 +539,13 @@ and read_number v = parse | eof { custom_error "Unexpected end of input" v lexbuf } and read_string v = parse - '"' { Bi_outbuf.clear v.buf; + '"' { Buffer.clear v.buf; finish_string v lexbuf } | _ { long_error "Expected '\"' but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } and read_ident v = parse - '"' { Bi_outbuf.clear v.buf; + '"' { Buffer.clear v.buf; finish_string v lexbuf } | ident as s { s } @@ -553,7 +553,7 @@ and read_ident v = parse | eof { custom_error "Unexpected end of input" v lexbuf } and map_ident v f = parse - '"' { Bi_outbuf.clear v.buf; + '"' { Buffer.clear v.buf; map_string v f lexbuf } | ident { map_lexeme f lexbuf } @@ -871,7 +871,7 @@ and buffer_json v = parse | '"' { finish_buffer_stringlit v lexbuf } | '{' { try - Bi_outbuf.add_char v.buf '{'; + Buffer.add_char v.buf '{'; buffer_space v lexbuf; buffer_object_end v lexbuf; buffer_ident v lexbuf; @@ -895,7 +895,7 @@ and buffer_json v = parse } | '[' { try - Bi_outbuf.add_char v.buf '['; + Buffer.add_char v.buf '['; buffer_space v lexbuf; buffer_array_end v lexbuf; buffer_json v lexbuf; @@ -913,7 +913,7 @@ and buffer_json v = parse | '(' { #ifdef TUPLE try - Bi_outbuf.add_char v.buf '('; + Buffer.add_char v.buf '('; buffer_space v lexbuf; buffer_tuple_end v lexbuf; buffer_json v lexbuf; @@ -933,7 +933,7 @@ and buffer_json v = parse | '<' { #ifdef VARIANT - Bi_outbuf.add_char v.buf '<'; + Buffer.add_char v.buf '<'; buffer_space v lexbuf; buffer_ident v lexbuf; buffer_space v lexbuf; @@ -944,10 +944,10 @@ and buffer_json v = parse } | "//"[^'\n']* { add_lexeme v.buf lexbuf; buffer_json v lexbuf } - | "/*" { Bi_outbuf.add_string v.buf "/*"; + | "/*" { Buffer.add_string v.buf "/*"; finish_buffer_comment v lexbuf; buffer_json v lexbuf } - | "\n" { Bi_outbuf.add_char v.buf '\n'; + | "\n" { Buffer.add_char v.buf '\n'; newline v lexbuf; buffer_json v lexbuf } | space { add_lexeme v.buf lexbuf; buffer_json v lexbuf } @@ -958,18 +958,18 @@ and buffer_json v = parse and finish_buffer_stringlit v = parse ( '\\' (['"' '\\' '/' 'b' 'f' 'n' 'r' 't'] | 'u' hex hex hex hex) | [^'"' '\\'] )* '"' - { Bi_outbuf.add_char v.buf '"'; + { Buffer.add_char v.buf '"'; add_lexeme v.buf lexbuf } | _ { long_error "Invalid string literal" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } and finish_buffer_variant v = parse - ':' { Bi_outbuf.add_char v.buf ':'; + ':' { Buffer.add_char v.buf ':'; buffer_json v lexbuf; buffer_space v lexbuf; buffer_gt v lexbuf } - | '>' { Bi_outbuf.add_char v.buf '>' } + | '>' { Buffer.add_char v.buf '>' } | _ { long_error "Expected ':' or '>' but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } @@ -985,11 +985,11 @@ and buffer_space v = parse newline v lexbuf; buffer_space v lexbuf } | "/*" { - Bi_outbuf.add_string v.buf "/*"; + Buffer.add_string v.buf "/*"; finish_buffer_comment v lexbuf; buffer_space v lexbuf } | '\n' { - Bi_outbuf.add_char v.buf '\n'; + Buffer.add_char v.buf '\n'; newline v lexbuf; buffer_space v lexbuf } | [' ' '\t' '\r']+ { @@ -999,52 +999,52 @@ and buffer_space v = parse and buffer_object_end v = parse '}' { - Bi_outbuf.add_char v.buf '}'; + Buffer.add_char v.buf '}'; raise End_of_object } | "" { () } and buffer_object_sep v = parse - ',' { Bi_outbuf.add_char v.buf ',' } - | '}' { Bi_outbuf.add_char v.buf '}'; raise End_of_object } + ',' { Buffer.add_char v.buf ',' } + | '}' { Buffer.add_char v.buf '}'; raise End_of_object } | _ { long_error "Expected ',' or '}' but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } and buffer_array_end v = parse - ']' { Bi_outbuf.add_char v.buf ']'; raise End_of_array } + ']' { Buffer.add_char v.buf ']'; raise End_of_array } | "" { () } and buffer_array_sep v = parse - ',' { Bi_outbuf.add_char v.buf ',' } - | ']' { Bi_outbuf.add_char v.buf ']'; raise End_of_array } + ',' { Buffer.add_char v.buf ',' } + | ']' { Buffer.add_char v.buf ']'; raise End_of_array } | _ { long_error "Expected ',' or ']' but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } and buffer_tuple_end v = parse ')' { - Bi_outbuf.add_char v.buf ')'; + Buffer.add_char v.buf ')'; raise End_of_tuple } | "" { () } and buffer_tuple_sep v = parse - ',' { Bi_outbuf.add_char v.buf ',' } - | ')' { Bi_outbuf.add_char v.buf ')'; raise End_of_tuple } + ',' { Buffer.add_char v.buf ',' } + | ')' { Buffer.add_char v.buf ')'; raise End_of_tuple } | _ { long_error "Expected ',' or ')' but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } and buffer_colon v = parse - ':' { Bi_outbuf.add_char v.buf ':' } + ':' { Buffer.add_char v.buf ':' } | _ { long_error "Expected ':' but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } and buffer_gt v = parse - '>' { Bi_outbuf.add_char v.buf '>' } + '>' { Buffer.add_char v.buf '>' } | _ { long_error "Expected '>' but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } and finish_buffer_comment v = parse - | "*/" { Bi_outbuf.add_string v.buf "*/" } + | "*/" { Buffer.add_string v.buf "*/" } | eof { long_error "Unterminated comment" v lexbuf } - | '\n' { Bi_outbuf.add_char v.buf '\n'; + | '\n' { Buffer.add_char v.buf '\n'; newline v lexbuf; finish_buffer_comment v lexbuf } | _ { add_lexeme v.buf lexbuf; finish_buffer_comment v lexbuf } @@ -1184,7 +1184,7 @@ and junk = parse ?buf ?(fin = fun () -> ()) ?fname ?lnum:(lnum0 = 1) ic = let buf = match buf with - None -> Some (Bi_outbuf.create 256) + None -> Some (Buffer.create 256) | Some _ -> buf in let f i = diff --git a/lib/write.ml b/lib/write.ml index 30fc4ad1..4692cfbd 100644 --- a/lib/write.ml +++ b/lib/write.ml @@ -7,22 +7,20 @@ let hex n = ) let write_special src start stop ob str = - Bi_outbuf.add_substring ob src !start (stop - !start); - Bi_outbuf.add_string ob str; + Buffer.add_substring ob src !start (stop - !start); + Buffer.add_string ob str; start := stop + 1 let write_control_char src start stop ob c = - Bi_outbuf.add_substring ob src !start (stop - !start); - let i = Bi_outbuf.alloc ob 6 in - let dst = ob.o_s in - Bytes.blit_string "\\u00" 0 dst i 4; - Bytes.set dst (i+4) (hex (Char.code c lsr 4)); - Bytes.set dst (i+5) (hex (Char.code c land 0xf)); + Buffer.add_substring ob src !start (stop - !start); + Buffer.add_string ob "\\00"; + Buffer.add_char ob (hex (Char.code c lsr 4)); + Buffer.add_char ob (hex (Char.code c land 0xf)); start := stop + 1 let finish_string src start ob = try - Bi_outbuf.add_substring ob src !start (String.length src - !start) + Buffer.add_substring ob src !start (String.length src - !start) with exc -> Printf.eprintf "src=%S start=%i len=%i\n%!" src !start (String.length src - !start); @@ -46,14 +44,14 @@ let write_string_body ob s = finish_string s start ob let write_string ob s = - Bi_outbuf.add_char ob '"'; + Buffer.add_char ob '"'; write_string_body ob s; - Bi_outbuf.add_char ob '"' + Buffer.add_char ob '"' let json_string_of_string s = - let ob = Bi_outbuf.create 10 in + let ob = Buffer.create 10 in write_string ob s; - Bi_outbuf.contents ob + Buffer.contents ob let test_string () = let s = Bytes.create 256 in @@ -64,10 +62,10 @@ let test_string () = let write_null ob () = - Bi_outbuf.add_string ob "null" + Buffer.add_string ob "null" let write_bool ob x = - Bi_outbuf.add_string ob (if x then "true" else "false") + Buffer.add_string ob (if x then "true" else "false") let max_digits = @@ -78,26 +76,22 @@ let max_digits = let dec n = Char.chr (n + 48) -let rec write_digits s pos x = - if x = 0 then pos +let rec write_digits s x = + if x = 0 then () else let d = x mod 10 in - let pos = write_digits s pos (x / 10) in - Bytes.set s pos (dec (abs d)); - pos + 1 + write_digits s (x / 10); + Buffer.add_char s (dec (abs d)) let write_int ob x = - Bi_outbuf.extend ob max_digits; if x > 0 then - ob.o_len <- write_digits ob.o_s ob.o_len x + write_digits ob x else if x < 0 then ( - let s = ob.o_s in - let pos = ob.o_len in - Bytes.set s pos '-'; - ob.o_len <- write_digits s (pos + 1) x + Buffer.add_char ob '-'; + write_digits ob x ) else - Bi_outbuf.add_char ob '0' + Buffer.add_char ob '0' let json_string_of_int i = @@ -130,30 +124,30 @@ let float_needs_period s = let write_float_fast ob x = match classify_float x with FP_nan -> - Bi_outbuf.add_string ob "NaN" + Buffer.add_string ob "NaN" | FP_infinite -> - Bi_outbuf.add_string ob (if x > 0. then "Infinity" else "-Infinity") + Buffer.add_string ob (if x > 0. then "Infinity" else "-Infinity") | _ -> let s = Printf.sprintf "%.17g" x in - Bi_outbuf.add_string ob s; + Buffer.add_string ob s; if float_needs_period s then - Bi_outbuf.add_string ob ".0" + Buffer.add_string ob ".0" let write_float ob x = match classify_float x with FP_nan -> - Bi_outbuf.add_string ob "NaN" + Buffer.add_string ob "NaN" | FP_infinite -> - Bi_outbuf.add_string ob (if x > 0. then "Infinity" else "-Infinity") + Buffer.add_string ob (if x > 0. then "Infinity" else "-Infinity") | _ -> let s1 = Printf.sprintf "%.16g" x in let s = if float_of_string s1 = x then s1 else Printf.sprintf "%.17g" x in - Bi_outbuf.add_string ob s; + Buffer.add_string ob s; if float_needs_period s then - Bi_outbuf.add_string ob ".0" + Buffer.add_string ob ".0" let write_normal_float_prec significant_figures ob x = let open Printf in @@ -177,23 +171,23 @@ let write_normal_float_prec significant_figures ob x = | 16 -> sprintf "%.16g" x | _ -> sprintf "%.17g" x in - Bi_outbuf.add_string ob s; + Buffer.add_string ob s; if float_needs_period s then - Bi_outbuf.add_string ob ".0" + Buffer.add_string ob ".0" let write_float_prec significant_figures ob x = match classify_float x with FP_nan -> - Bi_outbuf.add_string ob "NaN" + Buffer.add_string ob "NaN" | FP_infinite -> - Bi_outbuf.add_string ob (if x > 0. then "Infinity" else "-Infinity") + Buffer.add_string ob (if x > 0. then "Infinity" else "-Infinity") | _ -> write_normal_float_prec significant_figures ob x let json_string_of_float x = - let ob = Bi_outbuf.create 20 in + let ob = Buffer.create 20 in write_float ob x; - Bi_outbuf.contents ob + Buffer.contents ob let write_std_float_fast ob x = @@ -208,9 +202,9 @@ let write_std_float_fast ob x = "-Infinity value not allowed in standard JSON") | _ -> let s = Printf.sprintf "%.17g" x in - Bi_outbuf.add_string ob s; + Buffer.add_string ob s; if float_needs_period s then - Bi_outbuf.add_string ob ".0" + Buffer.add_string ob ".0" let write_std_float ob x = match classify_float x with @@ -228,9 +222,9 @@ let write_std_float ob x = if float_of_string s1 = x then s1 else Printf.sprintf "%.17g" x in - Bi_outbuf.add_string ob s; + Buffer.add_string ob s; if float_needs_period s then - Bi_outbuf.add_string ob ".0" + Buffer.add_string ob ".0" let write_std_float_prec significant_figures ob x = match classify_float x with @@ -246,9 +240,9 @@ let write_std_float_prec significant_figures ob x = write_normal_float_prec significant_figures ob x let std_json_string_of_float x = - let ob = Bi_outbuf.create 20 in + let ob = Buffer.create 20 in write_std_float ob x; - Bi_outbuf.contents ob + Buffer.contents ob let test_float () = @@ -271,9 +265,9 @@ let test_float () = let () = test_float () *) -let write_intlit = Bi_outbuf.add_string -let write_floatlit = Bi_outbuf.add_string -let write_stringlit = Bi_outbuf.add_string +let write_intlit = Buffer.add_string +let write_floatlit = Buffer.add_string +let write_stringlit = Buffer.add_string let rec iter2_aux f_elt f_sep x = function [] -> () @@ -289,7 +283,7 @@ let iter2 f_elt f_sep x = function iter2_aux f_elt f_sep x l let f_sep ob = - Bi_outbuf.add_char ob ',' + Buffer.add_char ob ',' let rec write_json ob (x : t) = match x with @@ -299,19 +293,19 @@ let rec write_json ob (x : t) = | `Int i -> write_int ob i #endif #ifdef INTLIT - | `Intlit s -> Bi_outbuf.add_string ob s + | `Intlit s -> Buffer.add_string ob s #endif #ifdef FLOAT | `Float f -> write_float ob f #endif #ifdef FLOATLIT - | `Floatlit s -> Bi_outbuf.add_string ob s + | `Floatlit s -> Buffer.add_string ob s #endif #ifdef STRING | `String s -> write_string ob s #endif #ifdef STRINGLIT - | `Stringlit s -> Bi_outbuf.add_string ob s + | `Stringlit s -> Buffer.add_string ob s #endif | `Assoc l -> write_assoc ob l | `List l -> write_list ob l @@ -325,36 +319,36 @@ let rec write_json ob (x : t) = and write_assoc ob l = let f_elt ob (s, x) = write_string ob s; - Bi_outbuf.add_char ob ':'; + Buffer.add_char ob ':'; write_json ob x in - Bi_outbuf.add_char ob '{'; + Buffer.add_char ob '{'; iter2 f_elt f_sep ob l; - Bi_outbuf.add_char ob '}'; + Buffer.add_char ob '}'; and write_list ob l = - Bi_outbuf.add_char ob '['; + Buffer.add_char ob '['; iter2 write_json f_sep ob l; - Bi_outbuf.add_char ob ']' + Buffer.add_char ob ']' #ifdef TUPLE and write_tuple ob l = - Bi_outbuf.add_char ob '('; + Buffer.add_char ob '('; iter2 write_json f_sep ob l; - Bi_outbuf.add_char ob ')' + Buffer.add_char ob ')' #endif #ifdef VARIANT and write_variant ob s o = - Bi_outbuf.add_char ob '<'; + Buffer.add_char ob '<'; write_string ob s; (match o with None -> () | Some x -> - Bi_outbuf.add_char ob ':'; + Buffer.add_char ob ':'; write_json ob x ); - Bi_outbuf.add_char ob '>' + Buffer.add_char ob '>' #endif let write_t = write_json @@ -367,19 +361,19 @@ let rec write_std_json ob (x : t) = | `Int i -> write_int ob i #endif #ifdef INTLIT - | `Intlit s -> Bi_outbuf.add_string ob s + | `Intlit s -> Buffer.add_string ob s #endif #ifdef FLOAT | `Float f -> write_std_float ob f #endif #ifdef FLOATLIT - | `Floatlit s -> Bi_outbuf.add_string ob s + | `Floatlit s -> Buffer.add_string ob s #endif #ifdef STRING | `String s -> write_string ob s #endif #ifdef STRINGLIT - | `Stringlit s -> Bi_outbuf.add_string ob s + | `Stringlit s -> Buffer.add_string ob s #endif | `Assoc l -> write_std_assoc ob l | `List l -> write_std_list ob l @@ -393,33 +387,33 @@ let rec write_std_json ob (x : t) = and write_std_assoc ob l = let f_elt ob (s, x) = write_string ob s; - Bi_outbuf.add_char ob ':'; + Buffer.add_char ob ':'; write_std_json ob x in - Bi_outbuf.add_char ob '{'; + Buffer.add_char ob '{'; iter2 f_elt f_sep ob l; - Bi_outbuf.add_char ob '}'; + Buffer.add_char ob '}'; and write_std_list ob l = - Bi_outbuf.add_char ob '['; + Buffer.add_char ob '['; iter2 write_std_json f_sep ob l; - Bi_outbuf.add_char ob ']' + Buffer.add_char ob ']' and write_std_tuple ob l = - Bi_outbuf.add_char ob '['; + Buffer.add_char ob '['; iter2 write_std_json f_sep ob l; - Bi_outbuf.add_char ob ']' + Buffer.add_char ob ']' #ifdef VARIANT and write_std_variant ob s o = match o with None -> write_string ob s | Some x -> - Bi_outbuf.add_char ob '['; + Buffer.add_char ob '['; write_string ob s; - Bi_outbuf.add_char ob ','; + Buffer.add_char ob ','; write_std_json ob x; - Bi_outbuf.add_char ob ']' + Buffer.add_char ob ']' #endif @@ -437,33 +431,31 @@ let to_outbuf ?(std = false) ob x = let to_string ?buf ?(len = 256) ?std x = let ob = match buf with - None -> Bi_outbuf.create len + None -> Buffer.create len | Some ob -> - Bi_outbuf.clear ob; + Buffer.clear ob; ob in to_outbuf ?std ob x; - let s = Bi_outbuf.contents ob in - Bi_outbuf.clear ob; + let s = Buffer.contents ob in + Buffer.clear ob; s -let to_channel ?buf ?len ?std oc x = +let to_channel ?buf ?(len=4096) ?std oc x = let ob = match buf with - None -> Bi_outbuf.create_channel_writer ?len oc + None -> Buffer.create len | Some ob -> ob in - to_outbuf ?std ob x; - Bi_outbuf.flush_channel_writer ob + to_outbuf ?std ob x -let to_output ?buf ?len ?std out x = +let to_output ?buf ?(len=4096) ?std out x = let ob = match buf with - None -> Bi_outbuf.create_output_writer ?len out + None -> Buffer.create len | Some ob -> ob in - to_outbuf ?std ob x; - Bi_outbuf.flush_output_writer ob + to_outbuf ?std ob x let to_file ?len ?std file x = let oc = open_out file in @@ -480,24 +472,23 @@ let stream_to_outbuf ?std ob st = let stream_to_string ?buf ?(len = 256) ?std st = let ob = match buf with - None -> Bi_outbuf.create len + None -> Buffer.create len | Some ob -> - Bi_outbuf.clear ob; + Buffer.clear ob; ob in stream_to_outbuf ?std ob st; - let s = Bi_outbuf.contents ob in - Bi_outbuf.clear ob; + let s = Buffer.contents ob in + Buffer.clear ob; s -let stream_to_channel ?buf ?len ?std oc st = +let stream_to_channel ?buf ?(len=2096) ?std oc st = let ob = match buf with - None -> Bi_outbuf.create_channel_writer ?len oc + None -> Buffer.create len | Some ob -> ob in - stream_to_outbuf ?std ob st; - Bi_outbuf.flush_channel_writer ob + stream_to_outbuf ?std ob st let stream_to_file ?len ?std file st = let oc = open_out file in diff --git a/lib/write.mli b/lib/write.mli index b8515a8b..4b0970f7 100644 --- a/lib/write.mli +++ b/lib/write.mli @@ -1,13 +1,13 @@ (** {2 JSON writers} *) val to_string : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?len:int -> ?std:bool -> t -> string (** Write a compact JSON value to a string. @param buf allows to reuse an existing buffer created with - [Bi_outbuf.create]. The buffer is cleared of all contents + [Buffer.create]. The buffer is cleared of all contents before starting and right before returning. @param len initial length of the output buffer. @param std use only standard JSON syntax, @@ -18,13 +18,13 @@ val to_string : *) val to_channel : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?len:int -> ?std:bool -> out_channel -> t -> unit (** Write a compact JSON value to a channel. @param buf allows to reuse an existing buffer created with - [Bi_outbuf.create_channel_writer] on the same channel. + [Buffer.create] on the same channel. [buf] is flushed right before [to_channel] returns but the [out_channel] is not flushed automatically. @@ -32,13 +32,13 @@ val to_channel : See [to_string] for the role of the other optional arguments. *) val to_output : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?len:int -> ?std:bool -> < output : string -> int -> int -> int; .. > -> t -> unit (** Write a compact JSON value to an OO channel. @param buf allows to reuse an existing buffer created with - [Bi_outbuf.create_output_writer] on the same channel. + [Buffer.add_channel] on the same channel. [buf] is flushed right before [to_output] returns but the channel itself is not flushed automatically. @@ -54,12 +54,12 @@ val to_file : val to_outbuf : ?std:bool -> - Bi_outbuf.t -> t -> unit + Buffer.t -> t -> unit (** Write a compact JSON value to an existing buffer. See [to_string] for the role of the optional argument. *) val stream_to_string : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?len:int -> ?std:bool -> t Stream.t -> string @@ -68,7 +68,7 @@ val stream_to_string : See [to_string] for the role of the optional arguments. *) val stream_to_channel : - ?buf:Bi_outbuf.t -> + ?buf:Buffer.t -> ?len:int -> ?std:bool -> out_channel -> t Stream.t -> unit @@ -86,7 +86,7 @@ val stream_to_file : val stream_to_outbuf : ?std:bool -> - Bi_outbuf.t -> + Buffer.t -> t Stream.t -> unit (** Write a newline-separated sequence of compact one-line JSON values to an existing buffer. @@ -107,46 +107,46 @@ val sort : t -> t (**/**) (* begin undocumented section *) -val write_null : Bi_outbuf.t -> unit -> unit -val write_bool : Bi_outbuf.t -> bool -> unit +val write_null : Buffer.t -> unit -> unit +val write_bool : Buffer.t -> bool -> unit #ifdef INT -val write_int : Bi_outbuf.t -> int -> unit +val write_int : Buffer.t -> int -> unit #endif #ifdef FLOAT -val write_float : Bi_outbuf.t -> float -> unit -val write_std_float : Bi_outbuf.t -> float -> unit -val write_float_fast : Bi_outbuf.t -> float -> unit -val write_std_float_fast : Bi_outbuf.t -> float -> unit -val write_float_prec : int -> Bi_outbuf.t -> float -> unit -val write_std_float_prec : int -> Bi_outbuf.t -> float -> unit +val write_float : Buffer.t -> float -> unit +val write_std_float : Buffer.t -> float -> unit +val write_float_fast : Buffer.t -> float -> unit +val write_std_float_fast : Buffer.t -> float -> unit +val write_float_prec : int -> Buffer.t -> float -> unit +val write_std_float_prec : int -> Buffer.t -> float -> unit #endif #ifdef STRING -val write_string : Bi_outbuf.t -> string -> unit +val write_string : Buffer.t -> string -> unit #endif #ifdef INTLIT -val write_intlit : Bi_outbuf.t -> string -> unit +val write_intlit : Buffer.t -> string -> unit #endif #ifdef FLOATLIT -val write_floatlit : Bi_outbuf.t -> string -> unit +val write_floatlit : Buffer.t -> string -> unit #endif #ifdef STRINGLIT -val write_stringlit : Bi_outbuf.t -> string -> unit +val write_stringlit : Buffer.t -> string -> unit #endif -val write_assoc : Bi_outbuf.t -> (string * t) list -> unit -val write_list : Bi_outbuf.t -> t list -> unit +val write_assoc : Buffer.t -> (string * t) list -> unit +val write_list : Buffer.t -> t list -> unit #ifdef TUPLE -val write_tuple : Bi_outbuf.t -> t list -> unit -val write_std_tuple : Bi_outbuf.t -> t list -> unit +val write_tuple : Buffer.t -> t list -> unit +val write_std_tuple : Buffer.t -> t list -> unit #endif #ifdef VARIANT -val write_variant : Bi_outbuf.t -> string -> t option -> unit -val write_std_variant : Bi_outbuf.t -> string -> t option -> unit +val write_variant : Buffer.t -> string -> t option -> unit +val write_std_variant : Buffer.t -> string -> t option -> unit #endif -val write_json : Bi_outbuf.t -> t -> unit -val write_std_json : Bi_outbuf.t -> t -> unit +val write_json : Buffer.t -> t -> unit +val write_std_json : Buffer.t -> t -> unit (* end undocumented section *) (**/**) diff --git a/yojson-biniou.opam b/yojson-biniou.opam new file mode 100644 index 00000000..194ab742 --- /dev/null +++ b/yojson-biniou.opam @@ -0,0 +1,23 @@ +opam-version: "2.0" +maintainer: "nathan@cryptosense.com" +authors: ["Martin Jambon"] +homepage: "https://github.com/ocaml-community/yojson" +bug-reports: "https://github.com/ocaml-community/yojson/issues" +dev-repo: "git+https://github.com/ocaml-community/yojson.git" +doc: "https://ocaml-community.github.io/yojson/" +build: [ + ["dune" "subst"] {pinned} + ["dune" "build" "-p" name "-j" jobs] +] +depends: [ + "ocaml" {>= "4.02.3"} + "dune" {build} + "yojson" {= version} + "biniou" {>= "1.2.0"} +] +synopsis: + "Yojson-biniou provides Biniou support for Yojson" +description: """ +Yojson-biniou provides Biniou support for Yojson. + +It used to be part of the main Yojson package but was spun off.""" diff --git a/yojson-biniou/lib/dune b/yojson-biniou/lib/dune new file mode 100644 index 00000000..34c52942 --- /dev/null +++ b/yojson-biniou/lib/dune @@ -0,0 +1,6 @@ +(library + (name yojson_biniou) + (public_name yojson-biniou) + (synopsis "Biniou support for Yojson") + (flags (-safe-string)) + (libraries yojson biniou)) diff --git a/lib/yojson_biniou.ml b/yojson-biniou/lib/yojson_biniou.ml similarity index 100% rename from lib/yojson_biniou.ml rename to yojson-biniou/lib/yojson_biniou.ml diff --git a/lib/yojson_biniou.mli b/yojson-biniou/lib/yojson_biniou.mli similarity index 100% rename from lib/yojson_biniou.mli rename to yojson-biniou/lib/yojson_biniou.mli diff --git a/yojson.opam b/yojson.opam index 46b5ceaa..408b50a9 100644 --- a/yojson.opam +++ b/yojson.opam @@ -1,5 +1,5 @@ opam-version: "2.0" -maintainer: "martin@mjambon.com" +maintainer: "nathan@cryptosense.com" authors: ["Martin Jambon"] homepage: "https://github.com/ocaml-community/yojson" bug-reports: "https://github.com/ocaml-community/yojson/issues" @@ -14,7 +14,6 @@ depends: [ "ocaml" {>= "4.02.3"} "dune" "cppo" {build} - "biniou" {>= "1.2.0"} "alcotest" {with-test & >= "0.8.5"} ] synopsis: