diff --git a/CHANGES.md b/CHANGES.md index 1d37097..5ac9200 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,9 @@ ### Removed +- `Yojson.Raw` was removed to cut down on parser variants; users are encouraged + to use `Yosjon.Safe`. (#206, @Leonidas-from-XIV) + ### Security ## 3.0.0 diff --git a/lib/basic.cppo.ml b/lib/basic.cppo.ml index 59bc3fb..5949194 100644 --- a/lib/basic.cppo.ml +++ b/lib/basic.cppo.ml @@ -1,7 +1,3 @@ -#define INT -#define FLOAT -#define STRING - #include "type.ml" #include "write.ml" @@ -19,7 +15,3 @@ end module Util = struct #include "util.ml" end - -#undef INT -#undef FLOAT -#undef STRING diff --git a/lib/basic.cppo.mli b/lib/basic.cppo.mli index ed9b598..f7f6826 100644 --- a/lib/basic.cppo.mli +++ b/lib/basic.cppo.mli @@ -6,10 +6,6 @@ The main advantage of this module is its simplicity. *) -#define INT -#define FLOAT -#define STRING - #include "type.ml" #include "write.mli" @@ -24,7 +20,3 @@ module Util : sig #include "util.mli" end - -#undef INT -#undef FLOAT -#undef STRING diff --git a/lib/dune b/lib/dune index 7e06004..9f52c42 100644 --- a/lib/dune +++ b/lib/dune @@ -83,37 +83,10 @@ (action (run mucppo/mucppo.exe %{out} -o %{targets}))) -(rule - (targets raw.ml) - (deps - (:out raw.cppo.ml) - type.ml - write.ml - prettyprint.ml - monomorphic.ml - write2.ml - read.ml - util.ml) - (action - (run mucppo/mucppo.exe %{out} -o %{targets}))) - -(rule - (targets raw.mli) - (deps - (:out raw.cppo.mli) - type.ml - monomorphic.mli - write.mli - write2.mli - read.mli - util.mli) - (action - (run mucppo/mucppo.exe %{out} -o %{targets}))) - (library (name yojson) (public_name yojson) - (modules yojson t basic safe raw common codec lexer_utils) + (modules yojson t basic safe common codec lexer_utils) (synopsis "JSON parsing and printing") (flags (:standard -w -27-32))) diff --git a/lib/monomorphic.ml b/lib/monomorphic.ml index 912323b..447c3d3 100644 --- a/lib/monomorphic.ml +++ b/lib/monomorphic.ml @@ -5,42 +5,24 @@ let rec pp fmt = Format.fprintf fmt "`Bool (@["; Format.fprintf fmt "%B" x; Format.fprintf fmt "@])" -#ifdef INT | `Int x -> Format.fprintf fmt "`Int (@["; Format.fprintf fmt "%d" x; Format.fprintf fmt "@])" -#endif #ifdef INTLIT | `Intlit x -> Format.fprintf fmt "`Intlit (@["; Format.fprintf fmt "%S" x; Format.fprintf fmt "@])" #endif -#ifdef FLOAT | `Float x -> Format.fprintf fmt "`Float (@["; Format.fprintf fmt "%F" x; Format.fprintf fmt "@])" -#endif -#ifdef FLOATLIT - | `Floatlit x -> - Format.fprintf fmt "`Floatlit (@["; - Format.fprintf fmt "%S" x; - Format.fprintf fmt "@])" -#endif -#ifdef STRING | `String x -> Format.fprintf fmt "`String (@["; Format.fprintf fmt "%S" x; Format.fprintf fmt "@])" -#endif -#ifdef STRINGLIT - | `Stringlit x -> - Format.fprintf fmt "`Stringlit (@["; - Format.fprintf fmt "%S" x; - Format.fprintf fmt "@])" -#endif | `Assoc xs -> Format.fprintf fmt "`Assoc (@["; Format.fprintf fmt "@[<2>["; @@ -75,24 +57,12 @@ let rec equal a b = match a, b with | `Null, `Null -> true | `Bool a, `Bool b -> a = b -#ifdef INT | `Int a, `Int b -> a = b -#endif #ifdef INTLIT | `Intlit a, `Intlit b -> a = b #endif -#ifdef FLOAT | `Float a, `Float b -> a = b -#endif -#ifdef FLOATLIT - | `Floatlit a, `Floatlit b -> a = b -#endif -#ifdef STRING | `String a, `String b -> a = b -#endif -#ifdef STRINGLIT - | `Stringlit a, `Stringlit b -> a = b -#endif | `Assoc xs, `Assoc ys -> let compare_keys = fun (key, _) (key', _) -> String.compare key key' in let xs = List.stable_sort compare_keys xs in diff --git a/lib/prettyprint.ml b/lib/prettyprint.ml index e1c8c94..5382761 100644 --- a/lib/prettyprint.ml +++ b/lib/prettyprint.ml @@ -70,24 +70,12 @@ let rec format ~inside_box (out : Format.formatter) (x : t) : unit = match x with | `Null -> Format.pp_print_string out "null" | `Bool x -> Format.pp_print_bool out x -#ifdef INT | `Int x -> Format.pp_print_string out (json_string_of_int x) -#endif -#ifdef FLOAT | `Float x -> Format.pp_print_string out (json_string_of_float x) -#endif -#ifdef STRING | `String s -> Format.pp_print_string out (json_string_of_string s) -#endif #ifdef INTLIT | `Intlit s -> Format.pp_print_string out s -#endif -#ifdef FLOATLIT - | `Floatlit s -> Format.pp_print_string out s -#endif -#ifdef STRINGLIT - | `Stringlit s -> Format.pp_print_string out s #endif | `List [] -> Format.pp_print_string out "[]" | `List l -> diff --git a/lib/raw.cppo.ml b/lib/raw.cppo.ml deleted file mode 100644 index aa55154..0000000 --- a/lib/raw.cppo.ml +++ /dev/null @@ -1,25 +0,0 @@ -#define INTLIT -#define FLOATLIT -#define STRINGLIT - -#include "type.ml" - -#include "write.ml" - -module Pretty = struct - #include "prettyprint.ml" -end - -#include "monomorphic.ml" - -#include "write2.ml" - -#include "read.ml" - -module Util = struct - #include "util.ml" -end - -#undef INTLIT -#undef FLOATLIT -#undef STRINGLIT diff --git a/lib/raw.cppo.mli b/lib/raw.cppo.mli deleted file mode 100644 index da3bff4..0000000 --- a/lib/raw.cppo.mli +++ /dev/null @@ -1,27 +0,0 @@ -(** - Ints, floats and strings literals are systematically preserved using - [`Intlit], [`Floatlit] and [`Stringlit]. -*) - -#define INTLIT -#define FLOATLIT -#define STRINGLIT - -#include "type.ml" - -#include "monomorphic.mli" - -#include "write.mli" - -#include "write2.mli" - -#include "read.mli" - -(** This module provides combinators for extracting fields from JSON values. *) -module Util : sig - #include "util.mli" -end - -#undef INTLIT -#undef FLOATLIT -#undef STRINGLIT diff --git a/lib/read.mli b/lib/read.mli index ff6d44b..e022497 100644 --- a/lib/read.mli +++ b/lib/read.mli @@ -143,8 +143,6 @@ val map_ident : val read_lt : lexer_state -> Lexing.lexbuf -> unit val read_gt : lexer_state -> Lexing.lexbuf -> unit val read_comma : lexer_state -> Lexing.lexbuf -> unit -val finish_stringlit : lexer_state -> Lexing.lexbuf -> string -val finish_skip_stringlit : lexer_state -> Lexing.lexbuf -> unit val finish_escaped_char : lexer_state -> Lexing.lexbuf -> unit val finish_comment : lexer_state -> Lexing.lexbuf -> unit val read_space : lexer_state -> Lexing.lexbuf -> unit diff --git a/lib/read.mll b/lib/read.mll index e08b124..1e19d4a 100644 --- a/lib/read.mll +++ b/lib/read.mll @@ -98,10 +98,8 @@ !n let make_positive_int v lexbuf = - #ifdef INT try `Int (extract_positive_int lexbuf) with Int_overflow -> - #endif #ifdef INTLIT `Intlit (Lexing.lexeme lexbuf) #else @@ -125,10 +123,8 @@ !n let make_negative_int v lexbuf = - #ifdef INT try `Int (extract_negative_int lexbuf) with Int_overflow -> - #endif #ifdef INTLIT `Intlit (Lexing.lexeme lexbuf) #else @@ -169,45 +165,15 @@ rule read_json v = parse | "true" { `Bool true } | "false" { `Bool false } | "null" { `Null } - | "NaN" { - #ifdef FLOAT - `Float nan - #elif defined FLOATLIT - `Floatlit "NaN" - #endif - } - | "Infinity" { - #ifdef FLOAT - `Float infinity - #elif defined FLOATLIT - `Floatlit "Infinity" - #endif - } - | "-Infinity" { - #ifdef FLOAT - `Float neg_infinity - #elif defined FLOATLIT - `Floatlit "-Infinity" - #endif - } - | '"' { - #ifdef STRING - Buffer.clear v.buf; - `String (finish_string v lexbuf) - #elif defined STRINGLIT - `Stringlit (finish_stringlit v lexbuf) - #endif + | "NaN" { `Float nan } + | "Infinity" { `Float infinity } + | "-Infinity" { `Float neg_infinity } + | '"' { Buffer.clear v.buf; + `String (finish_string v lexbuf) } | positive_int { make_positive_int v lexbuf } | '-' positive_int { make_negative_int v lexbuf } - | float { - #ifdef FLOAT - `Float (float_of_string (Lexing.lexeme lexbuf)) - #elif defined FLOATLIT - `Floatlit (Lexing.lexeme lexbuf) - #endif - } - + | float { `Float (float_of_string (Lexing.lexeme lexbuf)) } | '{' { let acc = ref [] in try read_space v lexbuf; @@ -310,18 +276,6 @@ and finish_surrogate_pair v x = parse for code point beyond U+FFFF" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } -and finish_stringlit v = parse - ( '\\' (['"' '\\' '/' 'b' 'f' 'n' 'r' 't'] | 'u' hex hex hex hex) - | [^'"' '\\'] )* '"' - { let len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in - let s = Bytes.create (len+1) in - Bytes.set s 0 '"'; - Bytes.blit lexbuf.lex_buffer lexbuf.lex_start_pos s 1 len; - Bytes.to_string s - } - | _ { long_error "Invalid string literal" v lexbuf } - | eof { custom_error "Unexpected end of input" v lexbuf } - and read_lt v = parse '<' { () } | _ { long_error "Expected '<' but found" v lexbuf } @@ -619,7 +573,7 @@ and skip_json v = parse | "NaN" { () } | "Infinity" { () } | "-Infinity" { () } - | '"' { finish_skip_stringlit v lexbuf } + | '"' { finish_skip_string v lexbuf } | '-'? positive_int { () } | float { () } @@ -669,7 +623,7 @@ and skip_json v = parse | _ { long_error "Invalid token" v lexbuf } -and finish_skip_stringlit v = parse +and finish_skip_string v = parse ( '\\' (['"' '\\' '/' 'b' 'f' 'n' 'r' 't'] | 'u' hex hex hex hex) | [^'"' '\\'] )* '"' { () } @@ -677,7 +631,7 @@ and finish_skip_stringlit v = parse | eof { custom_error "Unexpected end of input" v lexbuf } and skip_ident v = parse - '"' { finish_skip_stringlit v lexbuf } + '"' { finish_skip_string v lexbuf } | ident { () } | _ { long_error "Expected string or identifier but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } @@ -695,7 +649,7 @@ and buffer_json v = parse | '-'? positive_int | float { add_lexeme v.buf lexbuf } - | '"' { finish_buffer_stringlit v lexbuf } + | '"' { finish_buffer_string v lexbuf } | '{' { try Buffer.add_char v.buf '{'; buffer_space v lexbuf; @@ -748,7 +702,7 @@ and buffer_json v = parse | _ { long_error "Invalid token" v lexbuf } -and finish_buffer_stringlit v = parse +and finish_buffer_string v = parse ( '\\' (['"' '\\' '/' 'b' 'f' 'n' 'r' 't'] | 'u' hex hex hex hex) | [^'"' '\\'] )* '"' { Buffer.add_char v.buf '"'; @@ -758,7 +712,7 @@ and finish_buffer_stringlit v = parse | eof { custom_error "Unexpected end of input" v lexbuf } and buffer_ident v = parse - '"' { finish_buffer_stringlit v lexbuf } + '"' { finish_buffer_string v lexbuf } | ident { add_lexeme v.buf lexbuf } | _ { long_error "Expected string or identifier but found" v lexbuf } | eof { custom_error "Unexpected end of input" v lexbuf } diff --git a/lib/safe.cppo.ml b/lib/safe.cppo.ml index ef4ac18..1fed18a 100644 --- a/lib/safe.cppo.ml +++ b/lib/safe.cppo.ml @@ -1,7 +1,4 @@ -#define INT #define INTLIT -#define FLOAT -#define STRING #include "type.ml" @@ -23,7 +20,4 @@ module Util = struct #include "util.ml" end -#undef INT #undef INTLIT -#undef FLOAT -#undef STRING diff --git a/lib/safe.cppo.mli b/lib/safe.cppo.mli index 32bf52d..766da25 100644 --- a/lib/safe.cppo.mli +++ b/lib/safe.cppo.mli @@ -8,10 +8,7 @@ JSON. *) -#define INT #define INTLIT -#define FLOAT -#define STRING #include "type.ml" @@ -30,7 +27,4 @@ module Util : sig #include "util.mli" end -#undef INT #undef INTLIT -#undef FLOAT -#undef STRING diff --git a/lib/t.cppo.ml b/lib/t.cppo.ml index 8e8a1df..cdb2df0 100644 --- a/lib/t.cppo.ml +++ b/lib/t.cppo.ml @@ -1,9 +1,4 @@ -#define INT #define INTLIT -#define FLOAT -#define FLOATLIT -#define STRING -#define STRINGLIT #include "type.ml" @@ -17,9 +12,4 @@ end #include "write2.ml" -#undef INT #undef INTLIT -#undef FLOAT -#undef FLOATLIT -#undef STRING -#undef STRINGLIT diff --git a/lib/t.cppo.mli b/lib/t.cppo.mli index 865807c..a91150e 100644 --- a/lib/t.cppo.mli +++ b/lib/t.cppo.mli @@ -1,9 +1,4 @@ -#define INT #define INTLIT -#define FLOAT -#define FLOATLIT -#define STRING -#define STRINGLIT #include "type.ml" @@ -13,9 +8,4 @@ #include "write2.mli" -#undef INT #undef INTLIT -#undef FLOAT -#undef FLOATLIT -#undef STRING -#undef STRINGLIT diff --git a/lib/type.ml b/lib/type.ml index b850531..909d9fe 100644 --- a/lib/type.ml +++ b/lib/type.ml @@ -4,24 +4,12 @@ type t = [ | `Null | `Bool of bool -#ifdef INT | `Int of int -#endif #ifdef INTLIT | `Intlit of string #endif -#ifdef FLOAT | `Float of float -#endif -#ifdef FLOATLIT - | `Floatlit of string -#endif -#ifdef STRING | `String of string -#endif -#ifdef STRINGLIT - | `Stringlit of string -#endif | `Assoc of (string * t) list | `List of t list ] diff --git a/lib/util.ml b/lib/util.ml index eed1ff0..bc44726 100644 --- a/lib/util.ml +++ b/lib/util.ml @@ -4,17 +4,11 @@ let typeof = function | `Assoc _ -> "object" | `Bool _ -> "bool" | `Float _ -> "float" -#ifdef INT | `Int _ -> "int" -#endif | `List _ -> "array" | `Null -> "null" | `String _ -> "string" | `Intlit _ -> "intlit" - | `Floatlit _ -> "floatlit" -#ifdef STRINGLIT - | `Stringlit _ -> "stringlit" -#endif let typerr msg js = raise (Type_error (msg ^ typeof js, js)) @@ -64,62 +58,42 @@ let to_bool_option = function | js -> typerr "Expected bool or null, got " js let to_number = function -#ifdef INT | `Int i -> float i -#endif -#ifdef FLOAT | `Float f -> f -#endif | js -> typerr "Expected number, got " js let to_number_option = function -#ifdef INT | `Int i -> Some (float i) -#endif -#ifdef FLOAT | `Float f -> Some f -#endif | `Null -> None | js -> typerr "Expected number or null, got " js let to_float = function -#ifdef FLOAT | `Float f -> f -#endif | js -> typerr "Expected float, got " js let to_float_option = function -#ifdef FLOAT | `Float f -> Some f -#endif | `Null -> None | js -> typerr "Expected float or null, got " js let to_int = function -#ifdef INT | `Int i -> i -#endif | js -> typerr "Expected int, got " js let to_int_option = function -#ifdef INT | `Int i -> Some i -#endif | `Null -> None | js -> typerr "Expected int or null, got " js let to_list = function `List l -> l | js -> typerr "Expected array, got " js let to_string = function -#ifdef STRING | `String s -> s -#endif | js -> typerr "Expected string, got " js let to_string_option = function -#ifdef STRING | `String s -> Some s -#endif | `Null -> None | js -> typerr "Expected string or null, got " js @@ -164,43 +138,25 @@ let filter_member k l = let filter_assoc l = filter_map (function `Assoc l -> Some l | _ -> None) l let filter_bool l = filter_map (function `Bool x -> Some x | _ -> None) l let filter_int l = - filter_map ( - function - #ifdef INT - | `Int x -> Some x - #endif - | _ -> None - ) l + filter_map (function + | `Int x -> Some x + | _ -> None) l let filter_float l = - filter_map ( - function -#ifdef FLOAT - `Float x -> Some x -#endif - | _ -> None - ) l + filter_map (function + | `Float x -> Some x + | _ -> None) l let filter_number l = - filter_map ( - function -#ifdef INT - `Int x -> Some (float x) -#endif -#ifdef FLOAT - | `Float x -> Some x -#endif - | _ -> None - ) l + filter_map (function + | `Int x -> Some (float x) + | `Float x -> Some x + | _ -> None) l let filter_string l = - filter_map ( - function -#ifdef STRING - `String x -> Some x -#endif - | _ -> None - ) l + filter_map (function + | `String x -> Some x + | _ -> None) l let keys o = to_assoc o |> List.map (fun (key, _) -> key) diff --git a/lib/write.ml b/lib/write.ml index c69bc63..08493bc 100644 --- a/lib/write.ml +++ b/lib/write.ml @@ -189,8 +189,6 @@ let write_float_prec significant_figures ob x = let write_std_float_prec = write_float_prec 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 [] -> () @@ -212,24 +210,12 @@ let rec write_json ob (x : t) = match x with `Null -> write_null ob () | `Bool b -> write_bool ob b -#ifdef INT | `Int i -> write_int ob i -#endif #ifdef INTLIT | `Intlit s -> Buffer.add_string ob s #endif -#ifdef FLOAT | `Float f -> write_float ob f -#endif -#ifdef FLOATLIT - | `Floatlit s -> Buffer.add_string ob s -#endif -#ifdef STRING | `String s -> write_string ob s -#endif -#ifdef STRINGLIT - | `Stringlit s -> Buffer.add_string ob s -#endif | `Assoc l -> write_assoc ob l | `List l -> write_list ob l diff --git a/lib/write.mli b/lib/write.mli index ebda5f2..eedd027 100644 --- a/lib/write.mli +++ b/lib/write.mli @@ -117,28 +117,16 @@ val sort : t -> t val write_null : Buffer.t -> unit -> unit val write_bool : Buffer.t -> bool -> unit -#ifdef INT val write_int : Buffer.t -> int -> unit -#endif -#ifdef FLOAT val write_float : Buffer.t -> float -> unit val write_std_float : 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 : Buffer.t -> string -> unit -#endif #ifdef INTLIT val write_intlit : Buffer.t -> string -> unit #endif -#ifdef FLOATLIT -val write_floatlit : Buffer.t -> string -> unit -#endif -#ifdef STRINGLIT -val write_stringlit : Buffer.t -> string -> unit -#endif val write_assoc : Buffer.t -> (string * t) list -> unit val write_list : Buffer.t -> t list -> unit diff --git a/lib/yojson.ml b/lib/yojson.ml index fc1365e..73ba2bd 100644 --- a/lib/yojson.ml +++ b/lib/yojson.ml @@ -2,4 +2,3 @@ include Common include T module Basic = Basic module Safe = Safe -module Raw = Raw diff --git a/lib/yojson.mli b/lib/yojson.mli index 4460dd9..96a1f2c 100644 --- a/lib/yojson.mli +++ b/lib/yojson.mli @@ -2,8 +2,7 @@ The Yojson library provides several types for representing JSON values, with different use cases. - The {{!basic}Basic} JSON type, - - The {{!safe}Safe} JSON type, a superset of JSON with safer support for integers, - - The {{!raw}Raw} JSON type, a superset of JSON, safer but less integrated with OCaml types. + - The {{!safe}Safe} JSON type, a superset of JSON with safer support for integers. Each of these different types have their own module. @@ -23,7 +22,3 @@ module Basic = Basic module Safe = Safe (** {1 JSON tree type with literal int/float/string leaves} *) - -module Raw = Raw - -(** {1:raw Supertype of all JSON tree types} *)