Pino Toscano
2018-Aug-23 17:15 UTC
[Libguestfs] [PATCH 1/2] mltools: JSON: add json_parser_tree_parse_file
Easy way to parse JSON from a file, without reading it all to string first. --- common/mltools/JSON_parser-c.c | 25 +++++++++++++++++++++++++ common/mltools/JSON_parser.ml | 1 + common/mltools/JSON_parser.mli | 3 +++ common/mltools/JSON_parser_tests.ml | 23 +++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/common/mltools/JSON_parser-c.c b/common/mltools/JSON_parser-c.c index e10a2b69d..be1f011d1 100644 --- a/common/mltools/JSON_parser-c.c +++ b/common/mltools/JSON_parser-c.c @@ -37,6 +37,7 @@ #define JSON_DICT_TAG 5 value virt_builder_json_parser_tree_parse (value stringv); +value virt_builder_json_parser_tree_parse_file (value stringv); static value convert_json_t (json_t *val, int level) @@ -142,3 +143,27 @@ virt_builder_json_parser_tree_parse (value stringv) CAMLreturn (rv); } + +value +virt_builder_json_parser_tree_parse_file (value filev) +{ + CAMLparam1 (filev); + CAMLlocal1 (rv); + json_t *tree; + json_error_t err; + + tree = json_load_file (String_val (filev), JSON_DECODE_ANY, &err); + if (tree == NULL) { + char buf[1024 + JSON_ERROR_TEXT_LENGTH]; + if (strlen (err.text) > 0) + snprintf (buf, sizeof buf, "%s: JSON parse error: %s", String_val (filev), err.text); + else + snprintf (buf, sizeof buf, "%s: unknown JSON parse error", String_val (filev)); + caml_invalid_argument (buf); + } + + rv = convert_json_t (tree, 1); + json_decref (tree); + + CAMLreturn (rv); +} diff --git a/common/mltools/JSON_parser.ml b/common/mltools/JSON_parser.ml index 642e24d65..7c01ddf04 100644 --- a/common/mltools/JSON_parser.ml +++ b/common/mltools/JSON_parser.ml @@ -21,6 +21,7 @@ open Tools_utils open Common_gettext.Gettext external json_parser_tree_parse : string -> JSON.json_t = "virt_builder_json_parser_tree_parse" +external json_parser_tree_parse_file : string -> JSON.json_t = "virt_builder_json_parser_tree_parse_file" let object_find_optional key = function | JSON.Dict fields -> diff --git a/common/mltools/JSON_parser.mli b/common/mltools/JSON_parser.mli index 5ad0ef017..a62d387cd 100644 --- a/common/mltools/JSON_parser.mli +++ b/common/mltools/JSON_parser.mli @@ -19,6 +19,9 @@ val json_parser_tree_parse : string -> JSON.json_t (** Parse the JSON string. *) +val json_parser_tree_parse_file : string -> JSON.json_t +(** Parse the JSON in the specified file. *) + val object_get_string : string -> JSON.json_t -> string (** [object_get_string key yv] gets the value of the [key] field as a string in the [yv] structure *) diff --git a/common/mltools/JSON_parser_tests.ml b/common/mltools/JSON_parser_tests.ml index 024817711..286724616 100644 --- a/common/mltools/JSON_parser_tests.ml +++ b/common/mltools/JSON_parser_tests.ml @@ -122,6 +122,28 @@ let test_tree_parse_inspect ctx assert_is_number 10_L (List.nth a 2); assert_is_number 2_L (List.assoc "second" l) +let test_tree_parse_file_basic ctx + begin + let tmpfile, chan = bracket_tmpfile ctx in + output_string chan "{}\n"; + flush chan; + close_out chan; + let value = json_parser_tree_parse_file tmpfile in + assert_is_object value + end; + begin + let tmpfile, chan = bracket_tmpfile ctx in + output_string chan "{\"foo\":5}\n"; + flush chan; + close_out chan; + let value = json_parser_tree_parse_file tmpfile in + let l = get_dict value in + assert_equal_int 1 (List.length l); + assert_equal_string "foo" (fst (List.hd l)); + assert_is_number 5_L (snd (List.hd l)); + end; + () + (* Suites declaration. *) let suite "mltools JSON_parser" >::: @@ -129,6 +151,7 @@ let suite "tree_parse.invalid" >:: test_tree_parse_invalid; "tree_parse.basic" >:: test_tree_parse_basic; "tree_parse.inspect" >:: test_tree_parse_inspect; + "tree_parse_file.basic" >:: test_tree_parse_file_basic; ] let () -- 2.17.1
Pino Toscano
2018-Aug-23 17:15 UTC
[Libguestfs] [PATCH 2/2] builder: use the new json_parser_tree_parse_file
No need to read the whole file manually. --- builder/simplestreams_parser.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/simplestreams_parser.ml b/builder/simplestreams_parser.ml index ccbfdff67..407eb11fb 100644 --- a/builder/simplestreams_parser.ml +++ b/builder/simplestreams_parser.ml @@ -44,7 +44,7 @@ let get_index ~downloader ~sigchecker { Sources.uri; proxy } | Some f -> f ) else tmpfile in - json_parser_tree_parse (read_whole_file file) in + json_parser_tree_parse_file file in let downloads let uri_index -- 2.17.1
Richard W.M. Jones
2018-Aug-23 17:21 UTC
Re: [Libguestfs] [PATCH 2/2] builder: use the new json_parser_tree_parse_file
On Thu, Aug 23, 2018 at 07:15:46PM +0200, Pino Toscano wrote:> No need to read the whole file manually. > --- > builder/simplestreams_parser.ml | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/builder/simplestreams_parser.ml b/builder/simplestreams_parser.ml > index ccbfdff67..407eb11fb 100644 > --- a/builder/simplestreams_parser.ml > +++ b/builder/simplestreams_parser.ml > @@ -44,7 +44,7 @@ let get_index ~downloader ~sigchecker { Sources.uri; proxy } > | Some f -> f > ) else > tmpfile in > - json_parser_tree_parse (read_whole_file file) in > + json_parser_tree_parse_file file in > > let downloads > let uri_indexACK series. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Seemingly Similar Threads
- [PATCH 1/2] mltools: JSON: add json_parser_tree_parse_file
- [PATCH v3 1/4] mltools: Rename Yajl module as JSON_parser and move to common/mltools.
- [PATCH 4/4] mltools: JSON: unify JSON_parser type with JSON.json_t.
- [PATCH 2/2] builder: support Simple Streams v1.0 as index metadata
- [PATCH 4/4] builder: support Simple Streams v1.0 as index metadata