mltools: JSON: combine JSON.Int and JSON.Int64 into a single variant.

It was convenient to have these as separate variants when we were only
using this type to generate JSON.  However if we also use it to parse
JSON documents then integers in the document should only map to a
single variant.
This commit is contained in:
Richard W.M. Jones
2018-08-20 15:31:09 +01:00
parent bd5c0b7c12
commit 279472f304
11 changed files with 38 additions and 39 deletions

View File

@@ -117,7 +117,7 @@ and list_entries_json ~sources index =
| None -> item
| Some str -> ("full-name", JSON.String str) :: item in
let item = ("arch", JSON.String (Index.string_of_arch arch)) :: item in
let item = ("size", JSON.Int64 size) :: item in
let item = ("size", JSON.Int size) :: item in
let item =
match compressed_size with
| None -> item
@@ -148,7 +148,7 @@ and list_entries_json ~sources index =
JSON.Dict (List.rev item)
) index in
let doc = [
"version", JSON.Int 1;
"version", JSON.Int 1L;
"sources", JSON.List json_sources;
"templates", JSON.List json_templates;
] in

View File

@@ -21,8 +21,7 @@
type field = string * json_t
and json_t =
| String of string
| Int of int
| Int64 of int64
| Int of int64
| Float of float
| Bool of bool
| List of json_t list
@@ -108,14 +107,13 @@ and output_list fields ~fmt ~indent =
and output_field ~indent ~fmt = function
| String s -> json_quote_string s
| Int i -> string_of_int i
| Bool b -> if b then "true" else "false"
| Int64 i -> Int64.to_string i
| Int i -> Int64.to_string i
(* The JSON standard permits either "1" or "1.0" but not "1.".
* OCaml string_of_float will generate "1.", but the %g formatter
* will only generate the valid JSON values.
*)
| Float f -> Printf.sprintf "%g" f
| Bool b -> if b then "true" else "false"
| List l -> output_list ~indent:(indent + 1) ~fmt l
| Dict d -> output_dict ~indent:(indent + 1) ~fmt d

View File

@@ -21,8 +21,7 @@
type field = string * json_t (** ["field": "value"] *)
and json_t = (** JSON value. *)
| String of string (** string value, eg. ["string"] *)
| Int of int (** int value, eg. [99] *)
| Int64 of int64 (** int64 value, eg. [99] *)
| Int of int64 (** int value, eg. [99] *)
| Float of float (** floating point value, eg. [9.9] *)
| Bool of bool (** boolean value, [true] or [false] *)
| List of json_t list (** array value, eg. [[1,2,3]] *)

View File

@@ -18,6 +18,8 @@
(* This file tests the JSON module. *)
open Std_utils
open OUnit2
(* Utils. *)
@@ -53,21 +55,21 @@ let test_bool ctx =
(JSON.string_of_doc ~fmt:JSON.Indented doc)
let test_int ctx =
let doc = [ "test_zero", JSON.Int 0;
"test_pos", JSON.Int 5;
"test_neg", JSON.Int (-5);
"test_pos64", JSON.Int64 (Int64.of_int 10);
"test_neg64", JSON.Int64 (Int64.of_int (-10)); ] in
let doc = [ "test_zero", JSON.Int 0L;
"test_pos", JSON.Int 5L;
"test_neg", JSON.Int (-5L);
"test_pos64", JSON.Int 1_000_000_000_000L;
"test_neg64", JSON.Int (-1_000_000_000_000L); ] in
assert_equal_string
"{ \"test_zero\": 0, \"test_pos\": 5, \"test_neg\": -5, \"test_pos64\": 10, \"test_neg64\": -10 }"
"{ \"test_zero\": 0, \"test_pos\": 5, \"test_neg\": -5, \"test_pos64\": 1000000000000, \"test_neg64\": -1000000000000 }"
(JSON.string_of_doc doc);
assert_equal_string
"{
\"test_zero\": 0,
\"test_pos\": 5,
\"test_neg\": -5,
\"test_pos64\": 10,
\"test_neg64\": -10
\"test_pos64\": 1000000000000,
\"test_neg64\": -1000000000000
}"
(JSON.string_of_doc ~fmt:JSON.Indented doc)
@@ -91,7 +93,7 @@ let test_float ctx =
(JSON.string_of_doc ~fmt:JSON.Indented doc)
let test_list ctx =
let doc = [ "item", JSON.List [ JSON.String "foo"; JSON.Int 10; JSON.Bool true ] ] in
let doc = [ "item", JSON.List [ JSON.String "foo"; JSON.Int 10L; JSON.Bool true ] ] in
assert_equal_string
"{ \"item\": [ \"foo\", 10, true ] }"
(JSON.string_of_doc doc);
@@ -107,8 +109,8 @@ let test_list ctx =
let test_nested_dict ctx =
let doc = [
"item", JSON.Dict [ "int", JSON.Int 5; "string", JSON.String "foo"; ];
"last", JSON.Int 10;
"item", JSON.Dict [ "int", JSON.Int 5L; "string", JSON.String "foo"; ];
"last", JSON.Int 10L;
] in
assert_equal_string
"{ \"item\": { \"int\": 5, \"string\": \"foo\" }, \"last\": 10 }"
@@ -125,10 +127,10 @@ let test_nested_dict ctx =
let test_nested_nested_dict ctx =
let doc = [
"item", JSON.Dict [ "int", JSON.Int 5;
"item2", JSON.Dict [ "int", JSON.Int 0; ];
"item", JSON.Dict [ "int", JSON.Int 5L;
"item2", JSON.Dict [ "int", JSON.Int 0L; ];
];
"last", JSON.Int 10;
"last", JSON.Int 10L;
] in
assert_equal_string
"{ \"item\": { \"int\": 5, \"item2\": { \"int\": 0 } }, \"last\": 10 }"
@@ -159,8 +161,8 @@ let test_qemu ctx =
let doc = [
"file.driver", JSON.String "https";
"file.url", JSON.String "https://libguestfs.org";
"file.timeout", JSON.Int 60;
"file.readahead", JSON.Int (64 * 1024 * 1024);
"file.timeout", JSON.Int 60L;
"file.readahead", JSON.Int (64L *^ 1024L *^ 1024L);
] in
assert_equal_string
"{ \"file.driver\": \"https\", \"file.url\": \"https://libguestfs.org\", \"file.timeout\": 60, \"file.readahead\": 67108864 }"
@@ -176,7 +178,7 @@ let test_qemu ctx =
let test_builder ctx =
let doc = [
"version", JSON.Int 1;
"version", JSON.Int 1L;
"sources", JSON.List [
JSON.Dict [
"uri", JSON.String "http://libguestfs.org/index";
@@ -187,7 +189,7 @@ let test_builder ctx =
"os-version", JSON.String "phony-debian";
"full-name", JSON.String "Phony Debian";
"arch", JSON.String "x86_64";
"size", JSON.Int64 536870912_L;
"size", JSON.Int 536870912_L;
"notes", JSON.Dict [
"C", JSON.String "Phony Debian look-alike used for testing.";
];
@@ -197,7 +199,7 @@ let test_builder ctx =
"os-version", JSON.String "phony-fedora";
"full-name", JSON.String "Phony Fedora";
"arch", JSON.String "x86_64";
"size", JSON.Int64 1073741824_L;
"size", JSON.Int 1073741824_L;
"notes", JSON.Dict [
"C", JSON.String "Phony Fedora look-alike used for testing.";
];

View File

@@ -83,7 +83,7 @@ object
(* qemu will actually assert-fail if you send the port
* number as a string ...
*)
| i -> ("file.port", JSON.Int i) :: json_params in
| i -> ("file.port", JSON.Int (Int64.of_int i)) :: json_params in
let json_params =
match parsed_uri.uri_user with

View File

@@ -161,8 +161,8 @@ class input_ova ova = object
let doc = [
"file", JSON.Dict [
"driver", JSON.String "raw";
"offset", JSON.Int64 offset;
"size", JSON.Int64 size;
"offset", JSON.Int offset;
"size", JSON.Int size;
"file", JSON.Dict [
"driver", JSON.String "file";
"filename", JSON.String tar_path]

View File

@@ -245,7 +245,7 @@ and qemu_uri_of_filename vmx_source filename =
match port_of_uri uri with
| None -> json_params
| Some port ->
("file.port", JSON.Int port) :: json_params in
("file.port", JSON.Int (Int64.of_int port)) :: json_params in
"json:" ^ JSON.string_of_doc json_params, format

View File

@@ -276,7 +276,7 @@ object
let disk_size = ov.ov_virtual_size in
let json_params =
("disk_size", JSON.Int64 disk_size) :: json_params in
("disk_size", JSON.Int disk_size) :: json_params in
(* Ask the plugin to write the disk ID to a special file. *)
let diskid_file = diskid_file_of_id id in

View File

@@ -63,8 +63,8 @@ let create_curl_qemu_uri driver host port path =
let json_params = [
"file.driver", JSON.String driver; (* "http" or "https" *)
"file.url", JSON.String url;
"file.timeout", JSON.Int 2000;
"file.readahead", JSON.Int (1024 * 1024);
"file.timeout", JSON.Int 2000_L;
"file.readahead", JSON.Int (1024_L *^ 1024_L);
(* "file.sslverify", JSON.String "off"; XXX *)
] in

View File

@@ -114,8 +114,8 @@ let qemu_img_supports_offset_and_size () =
let json = [
"file", JSON.Dict [
"driver", JSON.String "raw";
"offset", JSON.Int 512;
"size", JSON.Int 512;
"offset", JSON.Int 512_L;
"size", JSON.Int 512_L;
"file", JSON.Dict [
"filename", JSON.String tmp
]

View File

@@ -78,14 +78,14 @@ let rec map_source ?readahead ?password_file dcPath uri server path =
"file.driver", JSON.String "https";
"file.url", JSON.String https_url;
(* https://bugzilla.redhat.com/show_bug.cgi?id=1146007#c10 *)
"file.timeout", JSON.Int 2000;
"file.timeout", JSON.Int 2000_L;
] in
let json_params =
match readahead with
| None -> json_params
| Some readahead ->
("file.readahead", JSON.Int readahead) :: json_params in
("file.readahead", JSON.Int (Int64.of_int readahead)) :: json_params in
let json_params =
if sslverify then json_params