@@ -298,3 +298,109 @@ let merge = function
298
298
; names = List. rev acc_rev.names
299
299
; sources_content = Option. map ~f: List. rev acc_rev.sources_content
300
300
}
301
+
302
+ (* IO *)
303
+
304
+ let json ?replace_mappings t =
305
+ let rewrite_path path =
306
+ if Filename. is_relative path
307
+ then path
308
+ else
309
+ match Build_path_prefix_map. get_build_path_prefix_map () with
310
+ | Some map -> Build_path_prefix_map. rewrite map path
311
+ | None -> path
312
+ in
313
+ `Assoc
314
+ [ " version" , `Float (float_of_int t.version)
315
+ ; " file" , `String (rewrite_path t.file)
316
+ ; ( " sourceRoot"
317
+ , `String
318
+ (match t.sourceroot with
319
+ | None -> " "
320
+ | Some s -> rewrite_path s) )
321
+ ; " names" , `List (List. map t.names ~f: (fun s -> `String s))
322
+ ; " sources" , `List (List. map t.sources ~f: (fun s -> `String (rewrite_path s)))
323
+ ; ( " mappings"
324
+ , `String (Option. value ~default: (string_of_mapping t.mappings) replace_mappings) )
325
+ ; ( " sourcesContent"
326
+ , `List
327
+ (match t.sources_content with
328
+ | None -> []
329
+ | Some l ->
330
+ List. map l ~f: (function
331
+ | None -> `Null
332
+ | Some s -> `String s)) )
333
+ ]
334
+
335
+ let invalid () = invalid_arg " Source_map.of_json"
336
+
337
+ let string name rest =
338
+ try
339
+ match List. assoc name rest with
340
+ | `String s -> Some s
341
+ | `Null -> None
342
+ | _ -> invalid ()
343
+ with Not_found -> None
344
+
345
+ let list_string name rest =
346
+ try
347
+ match List. assoc name rest with
348
+ | `List l ->
349
+ Some
350
+ (List. map l ~f: (function
351
+ | `String s -> s
352
+ | _ -> invalid () ))
353
+ | _ -> invalid ()
354
+ with Not_found -> None
355
+
356
+ let list_string_opt name rest =
357
+ try
358
+ match List. assoc name rest with
359
+ | `List l ->
360
+ Some
361
+ (List. map l ~f: (function
362
+ | `String s -> Some s
363
+ | `Null -> None
364
+ | _ -> invalid () ))
365
+ | _ -> invalid ()
366
+ with Not_found -> None
367
+
368
+ let of_json ~parse_mappings json =
369
+ let parse ~version rest =
370
+ let def v d =
371
+ match v with
372
+ | None -> d
373
+ | Some v -> v
374
+ in
375
+ let file = string " file" rest in
376
+ let sourceroot = string " sourceRoot" rest in
377
+ let names = list_string " names" rest in
378
+ let sources = list_string " sources" rest in
379
+ let sources_content = list_string_opt " sourcesContent" rest in
380
+ let mappings = string " mappings" rest in
381
+ ( { version
382
+ ; file = def file " "
383
+ ; sourceroot
384
+ ; names = def names []
385
+ ; sources_content
386
+ ; sources = def sources []
387
+ ; mappings = mapping_of_string (def mappings " " )
388
+ }
389
+ , if parse_mappings then None else mappings )
390
+ in
391
+ match json with
392
+ | `Assoc (("version" , `Float version ) :: rest ) when int_of_float version = 3 ->
393
+ parse ~version: 3 rest
394
+ | `Assoc (("version" , `Int 3 ) :: rest ) -> parse ~version: 3 rest
395
+ | _ -> invalid ()
396
+
397
+ let of_string s = of_json ~parse_mappings: true (Yojson.Basic. from_string s) |> fst
398
+
399
+ let to_string m = Yojson.Basic. to_string (json m)
400
+
401
+ let to_file ?mappings m ~file =
402
+ let replace_mappings = mappings in
403
+ Yojson.Basic. to_file file (json ?replace_mappings m)
404
+
405
+ let of_file_no_mappings filename =
406
+ of_json ~parse_mappings: false (Yojson.Basic. from_file filename)
0 commit comments