Skip to content

Commit 136e118

Browse files
committed
tweak the API
1 parent 9c3ac25 commit 136e118

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

jscomp/main/builtin_cmi_datasets.ml

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

jscomp/main/builtin_cmj_datasets.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(* 2b41bdcad1d4f3c8f5eb529fc8752854 *)
1+
(* 6b7a15eb37c6d562fecdc27a2b979270 *)
22
let module_names : string array = Obj.magic (
33
"Gc" (* 220 *),
44
"Js" (* 23 *),
@@ -42,7 +42,7 @@ let module_names : string array = Obj.magic (
4242
"Js_cast" (* 23 *),
4343
"Js_date" (* 23 *),
4444
"Js_dict" (* 137 *),
45-
"Js_json" (* 225 *),
45+
"Js_json" (* 228 *),
4646
"Js_list" (* 612 *),
4747
"Js_math" (* 295 *),
4848
"Js_null" (* 181 *),
@@ -177,7 +177,7 @@ let module_data : string array = Obj.magic (
177177
(* Js_cast *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
178178
(* Js_date *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
179179
(* Js_dict *)"\132\149\166\190\000\000\000u\000\000\000%\000\000\000v\000\000\000p\160\240\176#get\144\160\160B@@@\176#map\144\160\160B@@@\176&values\144\160\160A@@@\176'entries\144\160\160A@@@\176(fromList\144\160\160A@@@\176)fromArray\144\160\160A@@@\176/unsafeDeleteKey\144\160\160B@@@A",
180-
(* Js_json *)"\132\149\166\190\000\000\000\205\000\000\0004\000\000\000\179\000\000\000\163\160\b\000\000(\000\176$test\144\160\160B@@@\176(classify\144\160\160A@@@\176*decodeNull\144\160\160A@@@\176+decodeArray\144\160\160A@@@\176,decodeNumber\144\160\160A@@@\176,decodeObject\144\160\160A@@@\176,decodeString\144\160\160A@@@\176,serializeExn\144\160\160A@@@\176-decodeBoolean\144\160\160A@@@\176.deserializeExn\144\160\160A@@@A",
180+
(* Js_json *)"\132\149\166\190\000\000\000\208\000\000\0004\000\000\000\180\000\000\000\164\160\b\000\000(\000\176$test\144\160\160B@@@\176(classify\144\160\160A@@@\176*decodeNull\144\160\160A@@@\176+decodeArray\144\160\160A@@@\176,decodeNumber\144\160\160A@@@\176,decodeObject\144\160\160A@@@\176,decodeString\144\160\160A@@@\176,serializeExn\144\160\160A@@@\176-decodeBoolean\144\160\160A@@@\1761deserializeUnsafe\144\160\160A@@@A",
181181
(* Js_list *)"\132\149\166\190\000\000\002P\000\000\000\189\000\000\002]\000\000\002F\160\b\000\000T\000\176\"hd\144\160\160A@@@\176\"tl\144\160\160A@@@\176#map\144\160\160B@@@\176#nth\144\160\160B@@@\176#rev\144\160\160A@@@\176$cons\144\160\160B@@\144\180B\160\176\001\003\242!x@\160\176\001\003\243\"xs@@\151\176\176@\166\"::A@\160\144\004\012\160\144\004\011@\176\1921others/js_list.mld\001\005\190\001\005\207\192\004\002d\001\005\190\001\005\214@\176$init\144\160\160B@@\144\180B\160\176\001\004\200!n@\160\176\001\004\201!f@@\147\192\151\176\161G\146&toList\160\145\176@)Js_vectorA@\176\192&_none_A@\000\255\004\002A\160\147\192\151\176\161L\146$init\160\145\176@)Js_vectorA@\004\015\160\144\004!\160\144\004 @\176\192\004/\001\000\152\001\014>\001\014Q\192\0040\001\000\152\001\014>\001\014f@A@\176\192\0042\001\000\152\001\014>\001\014@\004\003@A\176$iter\144\160\160B@@@\176%equal\144\160\160C@@@\176%iteri\144\160\160B@@@\176&filter\144\160\160B@@@\176&length\144\160\160A@@@\176&mapRev\144\160\160B@@@\176'countBy\144\160\160B@@@\176'flatten\144\160\160A@@@\176'isEmpty\144\160\160A@@\144\180A\160\176\001\003\245!x@@\151\176\153@\160\144\004\007\160\146\168@\176\"[]AA@\176\192\004pf\001\005\216\001\005\233\192\004qf\001\005\216\001\005\239@\176(foldLeft\144\160\160C@@@\176(toVector\144\160\160A@@@\176)filterMap\144\160\160B@@@\176)foldRight\144\160\160C@@@\176)revAppend\144\160\160B@@@A",
182182
(* Js_math *)"\132\149\166\190\000\000\001\019\000\000\000H\000\000\000\246\000\000\000\229\160\240\176$ceil\144\160\160A@@@\176%floor\144\160\160A@@@\176(ceil_int\144\004\n@\176)floor_int\144\004\b@\176*random_int\144\160\160B@@@\176+unsafe_ceil\144\160\160A@@\144\180A\160\176\001\004y$prim@@\151\176\181$ceil\160\160AA@\196$ceil@@\160$Math@\160\144\004\014@\176\1921others/js_math.ml\000S\001\rr\001\r\132\192\004\002\000S\001\rr\001\r\147@\176,unsafe_floor\144\160\160A@@\144\180A\160\176\001\004x\004\026@@\151\176\181%floor\160\004\025@\196%floor@@\160$Math@\160\144\004\012@\176\192\004\024\000s\001\018u\001\018\136\192\004\025\000s\001\018u\001\018\152@A",
183183
(* Js_null *)"\132\149\166\190\000\000\000\161\000\000\000/\000\000\000\153\000\000\000\144\160\224\176$bind\144\160\160B@@@\176$iter\144\160\160B@@@\176$test\144\160\160A@@\144\180A\160\176\001\004A!x@@\151\176\148*caml_equal\160\144\004\b\160\146@@\176\1921others/js_null.mla\001\006\020\001\0067\192\004\002a\001\006\020\001\006B@\176&getExn\144\160\160A@@@\176(from_opt\144\160\160A@@@\176*fromOption\144\004\005@A",

jscomp/others/js_json.ml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,22 @@ let patch : _ -> _ = [%raw{|function (json) {
174174
|}]
175175

176176

177-
let serializeExn (type t) (x : t) : string option = [%raw{| function(obj){
178-
return JSON.stringify(obj,function(_,value){
177+
let serializeExn (type t) (x : t) : string = [%raw{| function(obj){
178+
var output= JSON.stringify(obj,function(_,value){
179179
if(value===undefined){
180180
return {RE_PRIVATE_NONE : true}
181181
}
182182
return value
183-
})
184-
}
183+
});
184+
185+
if(output === undefined){
186+
// JSON.stringify will raise TypeError when it detects cylic objects
187+
throw new TypeError("output is undefined")
188+
}
189+
return output
190+
}
185191
|}] x
186192

187-
let deserializeExn (s: string) : 'a =
193+
let deserializeUnsafe (s: string) : 'a =
188194
patch (parseExn s)
189195

jscomp/others/js_json.mli

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ Js.log \@\@ Js.Json.stringifyAny [| "foo"; "bar" |]
239239
(** Best-effort serialization, it tries to seralize as
240240
many objects as possible and deserialize it back*)
241241

242-
val deserializeExn : string -> 'a
242+
(**
243+
It is unsafe in two aspects
244+
- It may throw during parsing
245+
- when you cast it to a specific type, it may have a type mismatch
246+
*)
247+
val deserializeUnsafe : string -> 'a
248+
243249

244-
val serializeExn : 'a -> string option
250+
(**
251+
It will raise in such situations:
252+
- The object can not be serlialized to a JSON
253+
- There are cycles
254+
- Some JS engines can not stringify deeply nested json objects
255+
*)
256+
val serializeExn : 'a -> string

jscomp/test/js_json_test.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -737,23 +737,11 @@ eq("File \"js_json_test.ml\", line 388, characters 5-12", Js_json.decodeNull({})
737737
eq("File \"js_json_test.ml\", line 390, characters 5-12", Js_json.decodeNull(1.23), undefined);
738738

739739
function id(obj) {
740-
var x = Js_json.serializeExn(obj);
741-
if (x !== undefined) {
742-
return Js_json.deserializeExn(x);
743-
}
744-
throw {
745-
RE_EXN_ID: "Assert_failure",
746-
_1: [
747-
"js_json_test.ml",
748-
396,
749-
14
750-
],
751-
Error: new Error()
752-
};
740+
return Js_json.deserializeUnsafe(Js_json.serializeExn(obj));
753741
}
754742

755743
function idtest(obj) {
756-
return eq("File \"js_json_test.ml\", line 400, characters 5-12", obj, id(obj));
744+
return eq("File \"js_json_test.ml\", line 399, characters 5-12", obj, Js_json.deserializeUnsafe(Js_json.serializeExn(obj)));
757745
}
758746

759747
idtest(undefined);

jscomp/test/js_json_test.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,8 @@ let () =
392392

393393

394394
let id (type t ) (obj : t) : t =
395-
match J.serializeExn obj with
396-
| None -> assert false
397-
| Some x -> J.deserializeExn x
395+
obj |. J.serializeExn |. J.deserializeUnsafe
396+
398397

399398
let idtest obj =
400399
eq __LOC__ obj (id obj)

0 commit comments

Comments
 (0)