Skip to content

Commit c4c5617

Browse files
Merge pull request #148 from sim642/document-exc
Expand documentation of exceptions
2 parents 618abfd + 6aaecec commit c4c5617

File tree

7 files changed

+76
-46
lines changed

7 files changed

+76
-46
lines changed

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## unreleased
2-
2+
33
### Added
44

5+
- Expanded documentation of exceptions (@sim642, #148)
6+
57
### Removed
68

79
### Changed

lib/common.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
val version : string
22

33
exception Json_error of string
4+
(** Exception used:
5+
- in JSON readers, if parsing fails;
6+
- in JSON writers and pretty printing, if [float] value is not allowed in standard JSON. *)
47

58
val json_error : string -> 'a
9+
(** @raise Json_error *)
610

711
type lexer_state = {
812
buf : Buffer.t;

lib/read.mli

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
val prettify : ?std:bool -> string -> string
22
(** Combined parser and pretty-printer.
3-
See [to_string] for the role of the optional [std] argument. *)
3+
See [to_string] for the role of the optional [std] argument and raised exceptions. *)
44

55
val compact : ?std:bool -> string -> string
66
(** Combined parser and printer.
7-
See [to_string] for the role of the optional [std] argument. *)
7+
See [to_string] for the role of the optional [std] argument and raised exceptions. *)
88

99
(** {2 JSON readers} *)
1010

@@ -22,6 +22,7 @@ val from_string :
2222
@param fname data file name to be used in error messages. It does
2323
not have to be a real file.
2424
@param lnum number of the first line of input. Default is 1.
25+
@raise Json_error if parsing fails.
2526
*)
2627

2728
val from_channel :
@@ -30,15 +31,15 @@ val from_channel :
3031
?lnum:int ->
3132
in_channel -> t
3233
(** Read a JSON value from a channel.
33-
See [from_string] for the meaning of the optional arguments. *)
34+
See [from_string] for the meaning of the optional arguments and raised exceptions. *)
3435

3536
val from_file :
3637
?buf:Buffer.t ->
3738
?fname:string ->
3839
?lnum:int ->
3940
string -> t
4041
(** Read a JSON value from a file.
41-
See [from_string] for the meaning of the optional arguments. *)
42+
See [from_string] for the meaning of the optional arguments and raised exceptions. *)
4243

4344

4445
type lexer_state = Lexer_state.t = {
@@ -65,7 +66,7 @@ val from_lexbuf :
6566
Lexing.lexbuf -> t
6667
(** Read a JSON value from a lexbuf.
6768
A valid initial [lexer_state] can be created with [init_lexer].
68-
See [from_string] for the meaning of the optional arguments.
69+
See [from_string] for the meaning of the optional arguments and raised exceptions.
6970
7071
@param stream indicates whether more data may follow. The default value
7172
is false and indicates that only JSON whitespace can be found between
@@ -78,7 +79,7 @@ val seq_from_string :
7879
string -> t Seq.t
7980
(** Input a sequence of JSON values from a string.
8081
Whitespace between JSON values is fine but not required.
81-
See [from_string] for the meaning of the optional arguments. *)
82+
See [from_string] for the meaning of the optional arguments and raised exceptions. *)
8283

8384
val seq_from_channel :
8485
?buf:Buffer.t ->
@@ -94,7 +95,7 @@ val seq_from_channel :
9495
@raise Finally When the parsing and the finalizer both raised, [Finally (exn, fin_exn)]
9596
is raised, [exn] being the parsing exception and [fin_exn] the finalizer one.
9697
97-
See [from_string] for the meaning of the other optional arguments. *)
98+
See [from_string] for the meaning of the other optional arguments and other raised exceptions. *)
9899

99100
val seq_from_file :
100101
?buf:Buffer.t ->
@@ -104,7 +105,7 @@ val seq_from_file :
104105
(** Input a sequence of JSON values from a file.
105106
Whitespace between JSON values is fine but not required.
106107
107-
See [from_string] for the meaning of the optional arguments. *)
108+
See [from_string] for the meaning of the optional arguments and raised exceptions. *)
108109

109110
val seq_from_lexbuf :
110111
lexer_state ->
@@ -117,7 +118,7 @@ val seq_from_lexbuf :
117118
is raised, [exn] being the parsing exception and [fin_exn] the finalizer one.
118119
119120
See [seq_from_channel] for the meaning of the optional [fin]
120-
argument. *)
121+
argument and other raised exceptions. *)
121122

122123

123124
type json_line = [ `Json of t | `Exn of exn ]
@@ -135,7 +136,7 @@ val lineseq_from_channel :
135136
136137
See [seq_from_channel] for the meaning of the optional [fin]
137138
argument.
138-
See [from_string] for the meaning of the other optional arguments. *)
139+
See [from_string] for the meaning of the other optional arguments and raised exceptions. *)
139140

140141
val lineseq_from_file :
141142
?buf:Buffer.t ->
@@ -148,7 +149,7 @@ val lineseq_from_file :
148149
149150
See [seq_from_channel] for the meaning of the optional [fin]
150151
argument.
151-
See [from_string] for the meaning of the other optional arguments. *)
152+
See [from_string] for the meaning of the other optional arguments and raised exceptions. *)
152153

153154
val read_t : lexer_state -> Lexing.lexbuf -> t
154155
(** Read a JSON value from the given lexer_state and lexing buffer and return it.

lib/util.mli

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,84 +69,99 @@ exception Undefined of string * t
6969
of bounds. *)
7070

7171
val keys : t -> string list
72-
(** Returns all the key names in the given JSON object *)
72+
(** Returns all the key names in the given JSON object.
73+
@raise Type_error if argument is not a JSON object. *)
7374

7475
val values : t -> t list
75-
(** Return all the value in the given JSON object *)
76+
(** Return all the value in the given JSON object.
77+
@raise Type_error if argument is not a JSON object. *)
7678

7779
val combine : t -> t -> t
78-
(** Combine two JSON Objects together *)
80+
(** Combine two JSON objects together.
81+
@raise Invalid_argument if either argument is not a JSON object. *)
7982

8083
val member : string -> t -> t
8184
(** [member k obj] returns the value associated with the key [k] in the JSON
82-
object [obj], or [`Null] if [k] is not present in [obj]. *)
85+
object [obj], or [`Null] if [k] is not present in [obj].
86+
@raise Type_error if [obj] is not a JSON object. *)
8387

8488
val index : int -> t -> t
8589
(** [index i arr] returns the value at index [i] in the JSON array [arr].
8690
Negative indices count from the end of the list (so -1 is the last
87-
element). *)
91+
element).
92+
@raise Type_error if [arr] is not a JSON array.
93+
@raise Undefined if index is out of bounds. *)
8894

8995
val map : (t -> t) -> t -> t
9096
(** [map f arr] calls the function [f] on each element of the JSON array
91-
[arr], and returns a JSON array containing the results. *)
97+
[arr], and returns a JSON array containing the results.
98+
@raise Type_error if [arr] is not an JSON array. *)
9299

93100
val to_assoc : t -> (string * t) list
94-
(** Extract the items of a JSON object or raise [Type_error]. *)
101+
(** Extract the items of a JSON object.
102+
@raise Type_error if argument is not a JSON object. *)
95103

96104
val to_option : (t -> 'a) -> t -> 'a option
97105
(** Return [None] if the JSON value is null or map the JSON value
98106
to [Some] value using the provided function. *)
99107

100108
val to_bool : t -> bool
101-
(** Extract a boolean value or raise [Type_error]. *)
109+
(** Extract a boolean value.
110+
@raise Type_error if argument is not a JSON boolean. *)
102111

103112
val to_bool_option : t -> bool option
104113
(** Extract [Some] boolean value,
105-
return [None] if the value is null,
106-
or raise [Type_error] otherwise. *)
114+
return [None] if the value is null.
115+
@raise Type_error if argument is neither. *)
107116

108117
val to_number : t -> float
109-
(** Extract a number or raise [Type_error]. *)
118+
(** Extract a number.
119+
@raise Type_error if argument is not a JSON number. *)
110120

111121
val to_number_option : t -> float option
112122
(** Extract [Some] number,
113-
return [None] if the value is null,
114-
or raise [Type_error] otherwise. *)
123+
return [None] if the value is null.
124+
@raise Type_error if argument is neither. *)
115125

116126
val to_float : t -> float
117-
(** Extract a float value or raise [Type_error].
118-
[to_number] is generally preferred as it also works with int literals. *)
127+
(** Extract a float value.
128+
[to_number] is generally preferred as it also works with int literals.
129+
@raise Type_error if argument is not a JSON float. *)
119130

120131
val to_float_option : t -> float option
121132
(** Extract [Some] float value,
122-
return [None] if the value is null,
123-
or raise [Type_error] otherwise.
133+
return [None] if the value is null.
124134
[to_number_option] is generally preferred as it also works
125-
with int literals. *)
135+
with int literals.
136+
@raise Type_error if argument is neither. *)
126137

127138
val to_int : t -> int
128-
(** Extract an int from a JSON int or raise [Type_error]. *)
139+
(** Extract an int from a JSON int.
140+
@raise Type_error if argument is not a JSON int. *)
129141

130142
val to_int_option : t -> int option
131143
(** Extract [Some] int from a JSON int,
132-
return [None] if the value is null,
133-
or raise [Type_error] otherwise. *)
144+
return [None] if the value is null.
145+
@raise Type_error if argument is neither. *)
134146

135147
val to_list : t -> t list
136-
(** Extract a list from JSON array or raise [Type_error]. *)
148+
(** Extract a list from JSON array.
149+
@raise Type_error if argument is not a JSON array. *)
137150

138151
val to_string : t -> string
139-
(** Extract a string from a JSON string or raise [Type_error]. *)
152+
(** Extract a string from a JSON string.
153+
@raise Type_error if argument is not a JSON string. *)
140154

141155
val to_string_option : t -> string option
142156
(** Extract [Some] string from a JSON string,
143-
return [None] if the value is null,
144-
or raise [Type_error] otherwise. *)
157+
return [None] if the value is null.
158+
@raise Type_error if argument is neither. *)
145159

146160
val convert_each : (t -> 'a) -> t -> 'a list
147161
(** The conversion functions above cannot be used with [map], because they do
148162
not return JSON values. This convenience function [convert_each to_f arr]
149-
is equivalent to [List.map to_f (to_list arr)]. *)
163+
is equivalent to [List.map to_f (to_list arr)].
164+
@raise Type_error if [arr] is not a JSON array. *)
150165

151166

152167
(** {3 Exception-free filters} *)

lib/write.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ let float_needs_period s =
121121
122122
The _fast version is faster but often produces unnecessarily long numbers.
123123
*)
124+
(* unused *)
124125
let write_float_fast ob x =
125126
match classify_float x with
126127
FP_nan ->
@@ -175,6 +176,7 @@ let write_normal_float_prec significant_figures ob x =
175176
if float_needs_period s then
176177
Buffer.add_string ob ".0"
177178

179+
(* used by atdgen *)
178180
let write_float_prec significant_figures ob x =
179181
match classify_float x with
180182
FP_nan ->
@@ -190,6 +192,7 @@ let json_string_of_float x =
190192
Buffer.contents ob
191193

192194

195+
(* unused *)
193196
let write_std_float_fast ob x =
194197
match classify_float x with
195198
FP_nan ->
@@ -226,6 +229,7 @@ let write_std_float ob x =
226229
if float_needs_period s then
227230
Buffer.add_string ob ".0"
228231

232+
(* used by atdgen *)
229233
let write_std_float_prec significant_figures ob x =
230234
match classify_float x with
231235
FP_nan ->

lib/write.mli

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ val to_string :
1818
refuse to print NaN and infinities,
1919
require the root node to be either an object or an array.
2020
Default is [false].
21+
@raise Json_error if [float] value is not allowed in standard JSON.
2122
*)
2223

2324
val to_channel :
@@ -29,7 +30,7 @@ val to_channel :
2930
(** Write a compact JSON value to a channel.
3031
Note: the [out_channel] is not flushed by this function.
3132
32-
See [to_string] for the role of the optional arguments. *)
33+
See [to_string] for the role of the optional arguments and raised exceptions. *)
3334

3435
val to_output :
3536
?buf:Buffer.t ->
@@ -39,15 +40,15 @@ val to_output :
3940
< output : string -> int -> int -> int; .. > -> t -> unit
4041
(** Write a compact JSON value to an OO channel.
4142
42-
See [to_string] for the role of the optional arguments. *)
43+
See [to_string] for the role of the optional arguments and raised exceptions. *)
4344

4445
val to_file :
4546
?len:int ->
4647
?std:bool ->
4748
?suf:string ->
4849
string -> t -> unit
4950
(** Write a compact JSON value to a file.
50-
See [to_string] for the role of the optional arguments.
51+
See [to_string] for the role of the optional arguments and raised exceptions.
5152
@param suf is a suffix appended to the output Newline by default
5253
for POSIX compliance. *)
5354

@@ -56,7 +57,7 @@ val to_buffer :
5657
?std:bool ->
5758
Buffer.t -> t -> unit
5859
(** Write a compact JSON value to an existing buffer.
59-
See [to_string] for the role of the optional argument. *)
60+
See [to_string] for the role of the optional argument and raised exceptions. *)
6061

6162
val seq_to_string :
6263
?buf:Buffer.t ->
@@ -67,7 +68,7 @@ val seq_to_string :
6768
(** Write a sequence of [suf]-suffixed compact one-line JSON values to
6869
a string.
6970
@param suf is the suffix ouf each value written. Newline by default.
70-
See [to_string] for the role of the optional arguments. *)
71+
See [to_string] for the role of the optional arguments and raised exceptions. *)
7172

7273
val seq_to_channel :
7374
?buf:Buffer.t ->
@@ -78,7 +79,7 @@ val seq_to_channel :
7879
(** Write a sequence of [suf]-suffixed compact one-line JSON values to
7980
a channel.
8081
@param suf is the suffix of each value written. Newline by default.
81-
See [to_channel] for the role of the optional arguments. *)
82+
See [to_channel] for the role of the optional arguments and raised exceptions. *)
8283

8384
val seq_to_file :
8485
?len:int ->
@@ -88,7 +89,7 @@ val seq_to_file :
8889
(** Write a sequence of [suf]-suffixed compact one-line JSON values to
8990
a file.
9091
@param suf is the suffix of each value written. Newline by default.
91-
See [to_string] for the role of the optional arguments. *)
92+
See [to_string] for the role of the optional arguments and raised exceptions. *)
9293

9394
val seq_to_buffer :
9495
?suf:string ->
@@ -98,7 +99,7 @@ val seq_to_buffer :
9899
(** Write a sequence of [suf]-suffixed compact one-line JSON values to
99100
an existing buffer.
100101
@param suf is the suffix of each value written. Newline by default.
101-
See [to_string] for the role of the optional arguments. *)
102+
See [to_string] for the role of the optional arguments and raised exceptions. *)
102103

103104
val write_t : Buffer.t -> t -> unit
104105
(** Write the given JSON value to the given buffer.

lib/write2.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
val pretty_print : ?std:bool -> Format.formatter -> t -> unit
44
(** Pretty-print into a {!Format.formatter}.
55
See [to_string] for the role of the optional [std] argument.
6+
@raise Json_error if [float] value is not allowed in standard JSON.
67
78
@since 1.3.1 *)
89

910
val pretty_to_string : ?std:bool -> t -> string
1011
(** Pretty-print into a string.
1112
See [to_string] for the role of the optional [std] argument.
13+
See [pretty_print] for raised exceptions.
1214
*)
1315

1416
val pretty_to_channel : ?std:bool -> out_channel -> t -> unit
1517
(** Pretty-print to a channel.
1618
See [to_string] for the role of the optional [std] argument.
19+
See [pretty_print] for raised exceptions.
1720
*)

0 commit comments

Comments
 (0)