1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
|
;;
#directory "+compiler-libs"
;;
#directory "../../../lib/deriving_json/"
;;
#directory "../../../lib/deriving_json/.js_of_ocaml_deriving.objs/byte/"
let () = Clflags.dump_source := true
[%%expect {|
|}]
type int_list = int list [@@deriving json]
[%%expect
{|
type int_list = int list[@@deriving json]
include
struct
let _ = fun (_ : int_list) -> ()
let rec int_list_of_json : Deriving_Json_lexer.lexbuf -> int_list =
fun buf ->
Deriving_Json.read_list (fun buf -> Deriving_Json.Json_int.read buf)
buf
let _ = int_list_of_json
let rec int_list_to_json : Buffer.t -> int_list -> unit =
fun buf a ->
Deriving_Json.write_list
(fun buf a -> Deriving_Json.Json_int.write buf a) buf a
let _ = int_list_to_json
let int_list_json : int_list Deriving_Json.t =
Deriving_Json.make int_list_to_json int_list_of_json
let _ = int_list_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type int_list = int list
val int_list_of_json : Deriving_Json_lexer.lexbuf -> int_list = <fun>
val int_list_to_json : Buffer.t -> int_list -> unit = <fun>
val int_list_json : int_list Deriving_Json.t = <abstr>
|}]
type int_ref = int ref [@@deriving json]
[%%expect
{|
type int_ref = int ref[@@deriving json]
include
struct
let _ = fun (_ : int_ref) -> ()
let rec int_ref_of_json : Deriving_Json_lexer.lexbuf -> int_ref =
fun buf ->
Deriving_Json.read_ref (fun buf -> Deriving_Json.Json_int.read buf)
buf
let _ = int_ref_of_json
let rec int_ref_to_json : Buffer.t -> int_ref -> unit =
fun buf a ->
Deriving_Json.write_ref
(fun buf a -> Deriving_Json.Json_int.write buf a) buf a
let _ = int_ref_to_json
let int_ref_json : int_ref Deriving_Json.t =
Deriving_Json.make int_ref_to_json int_ref_of_json
let _ = int_ref_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type int_ref = int ref
val int_ref_of_json : Deriving_Json_lexer.lexbuf -> int_ref = <fun>
val int_ref_to_json : Buffer.t -> int_ref -> unit = <fun>
val int_ref_json : int_ref Deriving_Json.t = <abstr>
|}]
type int_option = int option [@@deriving json]
[%%expect
{|
type int_option = int option[@@deriving json]
include
struct
let _ = fun (_ : int_option) -> ()
let rec int_option_of_json : Deriving_Json_lexer.lexbuf -> int_option =
fun buf ->
Deriving_Json.read_option
(fun buf -> Deriving_Json.Json_int.read buf) buf
let _ = int_option_of_json
let rec int_option_to_json : Buffer.t -> int_option -> unit =
fun buf a ->
Deriving_Json.write_option
(fun buf a -> Deriving_Json.Json_int.write buf a) buf a
let _ = int_option_to_json
let int_option_json : int_option Deriving_Json.t =
Deriving_Json.make int_option_to_json int_option_of_json
let _ = int_option_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type int_option = int option
val int_option_of_json : Deriving_Json_lexer.lexbuf -> int_option = <fun>
val int_option_to_json : Buffer.t -> int_option -> unit = <fun>
val int_option_json : int_option Deriving_Json.t = <abstr>
|}]
type int_array = int array [@@deriving json]
[%%expect
{|
type int_array = int array[@@deriving json]
include
struct
let _ = fun (_ : int_array) -> ()
let rec int_array_of_json : Deriving_Json_lexer.lexbuf -> int_array =
fun buf ->
Deriving_Json.read_array (fun buf -> Deriving_Json.Json_int.read buf)
buf
let _ = int_array_of_json
let rec int_array_to_json : Buffer.t -> int_array -> unit =
fun buf a ->
Deriving_Json.write_array
(fun buf a -> Deriving_Json.Json_int.write buf a) buf a
let _ = int_array_to_json
let int_array_json : int_array Deriving_Json.t =
Deriving_Json.make int_array_to_json int_array_of_json
let _ = int_array_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type int_array = int array
val int_array_of_json : Deriving_Json_lexer.lexbuf -> int_array = <fun>
val int_array_to_json : Buffer.t -> int_array -> unit = <fun>
val int_array_json : int_array Deriving_Json.t = <abstr>
|}]
type tuple1 = int * string [@@deriving json]
[%%expect
{|
type tuple1 = (int * string)[@@deriving json]
include
struct
let _ = fun (_ : tuple1) -> ()
let rec tuple1_of_json : Deriving_Json_lexer.lexbuf -> tuple1 =
fun buf ->
Deriving_Json_lexer.read_lbracket buf;
ignore (Deriving_Json_lexer.read_tag_1 0 buf);
Deriving_Json_lexer.read_comma buf;
(let a = Deriving_Json.Json_int.read buf in
Deriving_Json_lexer.read_comma buf;
(let b = Deriving_Json.Json_string.read buf in
Deriving_Json_lexer.read_rbracket buf; (a, b)))
let _ = tuple1_of_json
let rec tuple1_to_json : Buffer.t -> tuple1 -> unit =
fun buf a ->
let (a, b) = a in
Buffer.add_string buf "[0";
((Buffer.add_string buf ","; Deriving_Json.Json_int.write buf a);
Buffer.add_string buf ",";
Deriving_Json.Json_string.write buf b);
Buffer.add_string buf "]"
let _ = tuple1_to_json
let tuple1_json : tuple1 Deriving_Json.t =
Deriving_Json.make tuple1_to_json tuple1_of_json
let _ = tuple1_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type tuple1 = int * string
val tuple1_of_json : Deriving_Json_lexer.lexbuf -> tuple1 = <fun>
val tuple1_to_json : Buffer.t -> tuple1 -> unit = <fun>
val tuple1_json : tuple1 Deriving_Json.t = <abstr>
|}]
type variant1 =
| A
| B
| C
| D of variant1
[@@deriving json]
[%%expect
{|
type variant1 =
| A
| B
| C
| D of variant1 [@@deriving json]
include
struct
let _ = fun (_ : variant1) -> ()
let rec variant1_of_json : Deriving_Json_lexer.lexbuf -> variant1 =
fun buf ->
match Deriving_Json_lexer.read_case buf with
| `NCst 0 ->
(Deriving_Json_lexer.read_comma buf;
(let a = variant1_of_json buf in
Deriving_Json_lexer.read_rbracket buf; D a))
| `Cst 2 -> C
| `Cst 1 -> B
| `Cst 0 -> A
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf
let _ = variant1_of_json
let rec variant1_to_json : Buffer.t -> variant1 -> unit =
fun buf ->
function
| D a ->
(Buffer.add_string buf "[0";
(Buffer.add_string buf ","; variant1_to_json buf a);
Buffer.add_string buf "]")
| C -> Deriving_Json.Json_int.write buf 2
| B -> Deriving_Json.Json_int.write buf 1
| A -> Deriving_Json.Json_int.write buf 0
let _ = variant1_to_json
let variant1_json : variant1 Deriving_Json.t =
Deriving_Json.make variant1_to_json variant1_of_json
let _ = variant1_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type variant1 = A | B | C | D of variant1
val variant1_of_json : Deriving_Json_lexer.lexbuf -> variant1 = <fun>
val variant1_to_json : Buffer.t -> variant1 -> unit = <fun>
val variant1_json : variant1 Deriving_Json.t = <abstr>
|}]
type variant2 =
| D of string
| E of variant1
[@@deriving json]
[%%expect
{|
type variant2 =
| D of string
| E of variant1 [@@deriving json]
include
struct
let _ = fun (_ : variant2) -> ()
let rec variant2_of_json : Deriving_Json_lexer.lexbuf -> variant2 =
fun buf ->
match Deriving_Json_lexer.read_case buf with
| `NCst 1 ->
(Deriving_Json_lexer.read_comma buf;
(let a = variant1_of_json buf in
Deriving_Json_lexer.read_rbracket buf; E a))
| `NCst 0 ->
(Deriving_Json_lexer.read_comma buf;
(let a = Deriving_Json.Json_string.read buf in
Deriving_Json_lexer.read_rbracket buf; D a))
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf
let _ = variant2_of_json
let rec variant2_to_json : Buffer.t -> variant2 -> unit =
fun buf ->
function
| E a ->
(Buffer.add_string buf "[1";
(Buffer.add_string buf ","; variant1_to_json buf a);
Buffer.add_string buf "]")
| D a ->
(Buffer.add_string buf "[0";
(Buffer.add_string buf ",";
Deriving_Json.Json_string.write buf a);
Buffer.add_string buf "]")
let _ = variant2_to_json
let variant2_json : variant2 Deriving_Json.t =
Deriving_Json.make variant2_to_json variant2_of_json
let _ = variant2_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type variant2 = D of string | E of variant1
val variant2_of_json : Deriving_Json_lexer.lexbuf -> variant2 = <fun>
val variant2_to_json : Buffer.t -> variant2 -> unit = <fun>
val variant2_json : variant2 Deriving_Json.t = <abstr>
|}]
type record1 =
{ f : variant1
; g : variant2
; h : record1 option }
[@@deriving json]
[%%expect
{|
type record1 = {
f: variant1 ;
g: variant2 ;
h: record1 option }[@@deriving json]
include
struct
let _ = fun (_ : record1) -> ()
let rec record1_of_json : Deriving_Json_lexer.lexbuf -> record1 =
fun buf ->
Deriving_Json_lexer.read_lbracket buf;
ignore (Deriving_Json_lexer.read_tag_2 0 254 buf);
Deriving_Json_lexer.read_comma buf;
(let a = variant1_of_json buf in
Deriving_Json_lexer.read_comma buf;
(let b = variant2_of_json buf in
Deriving_Json_lexer.read_comma buf;
(let c =
Deriving_Json.read_option (fun buf -> record1_of_json buf) buf in
Deriving_Json_lexer.read_rbracket buf; { f = a; g = b; h = c })))
let _ = record1_of_json
let rec record1_to_json : Buffer.t -> record1 -> unit =
fun buf { f; g; h } ->
Buffer.add_string buf "[0";
(((Buffer.add_string buf ","; variant1_to_json buf f);
Buffer.add_string buf ",";
variant2_to_json buf g);
Buffer.add_string buf ",";
Deriving_Json.write_option (fun buf a -> record1_to_json buf a) buf
h);
Buffer.add_string buf "]"
let _ = record1_to_json
let record1_json : record1 Deriving_Json.t =
Deriving_Json.make record1_to_json record1_of_json
let _ = record1_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type record1 = { f : variant1; g : variant2; h : record1 option; }
val record1_of_json : Deriving_Json_lexer.lexbuf -> record1 = <fun>
val record1_to_json : Buffer.t -> record1 -> unit = <fun>
val record1_json : record1 Deriving_Json.t = <abstr>
|}]
type poly1 =
[ `A
| `B of string ]
[@@deriving json]
[%%expect
{|
type poly1 = [ `A | `B of string ][@@deriving json]
include
struct
let _ = fun (_ : poly1) -> ()
let rec poly1_recognize : [ `NCst of int | `Cst of int ] -> bool =
function | `Cst 65 -> true | `NCst 66 -> true | _ -> false
let _ = poly1_recognize
let rec poly1_of_json_with_tag :
Deriving_Json_lexer.lexbuf -> [ `NCst of int | `Cst of int ] -> poly1
=
fun buf ->
function
| `Cst 65 -> `A
| `NCst 66 ->
(Deriving_Json_lexer.read_comma buf;
(let v = Deriving_Json.Json_string.read buf in
Deriving_Json_lexer.read_rbracket buf; `B v))
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf
and poly1_of_json : Deriving_Json_lexer.lexbuf -> poly1 =
fun buf ->
poly1_of_json_with_tag buf (Deriving_Json_lexer.read_vcase buf)
let _ = poly1_of_json_with_tag
and _ = poly1_of_json
let rec poly1_to_json : Buffer.t -> [> poly1] -> unit =
fun buf a ->
match a with
| `A -> Deriving_Json.Json_int.write buf 65
| `B a ->
let (a, b) = (66, a) in
(Buffer.add_string buf "[0";
((Buffer.add_string buf ","; Deriving_Json.Json_int.write buf a);
Buffer.add_string buf ",";
Deriving_Json.Json_string.write buf b);
Buffer.add_string buf "]")
let _ = poly1_to_json
let poly1_json : poly1 Deriving_Json.t =
Deriving_Json.make poly1_to_json poly1_of_json
let _ = poly1_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type poly1 = [ `A | `B of string ]
val poly1_recognize : [ `Cst of int | `NCst of int ] -> bool = <fun>
val poly1_of_json_with_tag :
Deriving_Json_lexer.lexbuf -> [ `Cst of int | `NCst of int ] -> poly1 =
<fun>
val poly1_of_json : Deriving_Json_lexer.lexbuf -> poly1 = <fun>
val poly1_to_json : Buffer.t -> poly1 -> unit = <fun>
val poly1_json : poly1 Deriving_Json.t = <abstr>
|}]
type poly2 =
[ poly1
| `C of int ]
[@@deriving json]
[%%expect
{|
type poly2 = [ | poly1 | `C of int ][@@deriving json]
include
struct
let _ = fun (_ : poly2) -> ()
let rec poly2_recognize : [ `NCst of int | `Cst of int ] -> bool =
function
| x when poly1_recognize x -> true
| `NCst 67 -> true
| _ -> false
let _ = poly2_recognize
let rec poly2_of_json_with_tag :
Deriving_Json_lexer.lexbuf -> [ `NCst of int | `Cst of int ] -> poly2
=
fun buf ->
function
| x when poly1_recognize x ->
(poly1_of_json_with_tag buf x :> [ | poly1 | `C of int ])
| `NCst 67 ->
(Deriving_Json_lexer.read_comma buf;
(let v = Deriving_Json.Json_int.read buf in
Deriving_Json_lexer.read_rbracket buf; `C v))
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf
and poly2_of_json : Deriving_Json_lexer.lexbuf -> poly2 =
fun buf ->
poly2_of_json_with_tag buf (Deriving_Json_lexer.read_vcase buf)
let _ = poly2_of_json_with_tag
and _ = poly2_of_json
let rec poly2_to_json : Buffer.t -> [> poly2] -> unit =
fun buf a ->
match a with
| #poly1 as a -> poly1_to_json buf a
| `C a ->
let (a, b) = (67, a) in
(Buffer.add_string buf "[0";
((Buffer.add_string buf ","; Deriving_Json.Json_int.write buf a);
Buffer.add_string buf ",";
Deriving_Json.Json_int.write buf b);
Buffer.add_string buf "]")
let _ = poly2_to_json
let poly2_json : poly2 Deriving_Json.t =
Deriving_Json.make poly2_to_json poly2_of_json
let _ = poly2_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type poly2 = [ `A | `B of string | `C of int ]
val poly2_recognize : [ `Cst of int | `NCst of int ] -> bool = <fun>
val poly2_of_json_with_tag :
Deriving_Json_lexer.lexbuf -> [ `Cst of int | `NCst of int ] -> poly2 =
<fun>
val poly2_of_json : Deriving_Json_lexer.lexbuf -> poly2 = <fun>
val poly2_to_json : Buffer.t -> poly2 -> unit = <fun>
val poly2_json : poly2 Deriving_Json.t = <abstr>
|}]
type inline_record =
| I of
{ name : string
; age : int }
| J of {empty : unit}
[@@deriving json]
[%%expect
{|
type inline_record =
| I of {
name: string ;
age: int }
| J of {
empty: unit } [@@deriving json]
include
struct
let _ = fun (_ : inline_record) -> ()
let rec inline_record_of_json :
Deriving_Json_lexer.lexbuf -> inline_record =
fun buf ->
match Deriving_Json_lexer.read_case buf with
| `NCst 1 ->
(Deriving_Json_lexer.read_comma buf;
(let a = Deriving_Json.Json_unit.read buf in
Deriving_Json_lexer.read_rbracket buf; J { empty = a }))
| `NCst 0 ->
(Deriving_Json_lexer.read_comma buf;
(let a = Deriving_Json.Json_string.read buf in
Deriving_Json_lexer.read_comma buf;
(let b = Deriving_Json.Json_int.read buf in
Deriving_Json_lexer.read_rbracket buf; I { name = a; age = b })))
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf
let _ = inline_record_of_json
let rec inline_record_to_json : Buffer.t -> inline_record -> unit =
fun buf ->
function
| J { empty } ->
(Buffer.add_string buf "[1";
(Buffer.add_string buf ",";
Deriving_Json.Json_unit.write buf empty);
Buffer.add_string buf "]")
| I { name; age } ->
(Buffer.add_string buf "[0";
((Buffer.add_string buf ",";
Deriving_Json.Json_string.write buf name);
Buffer.add_string buf ",";
Deriving_Json.Json_int.write buf age);
Buffer.add_string buf "]")
let _ = inline_record_to_json
let inline_record_json : inline_record Deriving_Json.t =
Deriving_Json.make inline_record_to_json inline_record_of_json
let _ = inline_record_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type inline_record =
I of { name : string; age : int; }
| J of { empty : unit; }
val inline_record_of_json : Deriving_Json_lexer.lexbuf -> inline_record =
<fun>
val inline_record_to_json : Buffer.t -> inline_record -> unit = <fun>
val inline_record_json : inline_record Deriving_Json.t = <abstr>
|}]
type 'a t = 'a array [@@deriving json]
[%%expect {|
type 'a t = 'a array[@@deriving json]
include
struct
let _ = fun (_ : 'a t) -> ()
let rec of_json :
(Deriving_Json_lexer.lexbuf -> 'a) ->
Deriving_Json_lexer.lexbuf -> 'a t
=
fun poly_a buf -> Deriving_Json.read_array (fun buf -> poly_a buf) buf
let _ = of_json
let rec to_json : (Buffer.t -> 'a -> unit) -> Buffer.t -> 'a t -> unit =
fun poly_a buf a ->
Deriving_Json.write_array (fun buf a -> poly_a buf a) buf a
let _ = to_json
let json : 'a Deriving_Json.t -> 'a t Deriving_Json.t =
fun poly_a ->
Deriving_Json.make (to_json (Deriving_Json.write poly_a))
(of_json (Deriving_Json.read poly_a))
let _ = json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type 'a t = 'a array
val of_json :
(Deriving_Json_lexer.lexbuf -> 'a) -> Deriving_Json_lexer.lexbuf -> 'a t =
<fun>
val to_json : (Buffer.t -> 'a -> unit) -> Buffer.t -> 'a t -> unit = <fun>
val json : 'a Deriving_Json.t -> 'a t Deriving_Json.t = <fun>
|}]
type ('a,'b) t = ('a array * 'b) [@@deriving json]
[%%expect {|
type ('a, 'b) t = ('a array * 'b)[@@deriving json]
include
struct
let _ = fun (_ : ('a, 'b) t) -> ()
let rec of_json :
(Deriving_Json_lexer.lexbuf -> 'a) ->
(Deriving_Json_lexer.lexbuf -> 'b) ->
Deriving_Json_lexer.lexbuf -> ('a, 'b) t
=
fun poly_a poly_b buf ->
Deriving_Json_lexer.read_lbracket buf;
ignore (Deriving_Json_lexer.read_tag_1 0 buf);
Deriving_Json_lexer.read_comma buf;
(let a = Deriving_Json.read_array (fun buf -> poly_a buf) buf in
Deriving_Json_lexer.read_comma buf;
(let b = poly_b buf in Deriving_Json_lexer.read_rbracket buf; (a, b)))
let _ = of_json
let rec to_json :
(Buffer.t -> 'a -> unit) ->
(Buffer.t -> 'b -> unit) -> Buffer.t -> ('a, 'b) t -> unit
=
fun poly_a poly_b buf a ->
let (a, b) = a in
Buffer.add_string buf "[0";
((Buffer.add_string buf ",";
Deriving_Json.write_array (fun buf a -> poly_a buf a) buf a);
Buffer.add_string buf ",";
poly_b buf b);
Buffer.add_string buf "]"
let _ = to_json
let json :
'a Deriving_Json.t -> 'b Deriving_Json.t -> ('a, 'b) t Deriving_Json.t
=
fun poly_a poly_b ->
Deriving_Json.make
((to_json (Deriving_Json.write poly_a))
(Deriving_Json.write poly_b))
((of_json (Deriving_Json.read poly_a)) (Deriving_Json.read poly_b))
let _ = json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type ('a, 'b) t = 'a array * 'b
val of_json :
(Deriving_Json_lexer.lexbuf -> 'a) ->
(Deriving_Json_lexer.lexbuf -> 'b) ->
Deriving_Json_lexer.lexbuf -> ('a, 'b) t = <fun>
val to_json :
(Buffer.t -> 'a -> unit) ->
(Buffer.t -> 'b -> unit) -> Buffer.t -> ('a, 'b) t -> unit = <fun>
val json :
'a Deriving_Json.t -> 'b Deriving_Json.t -> ('a, 'b) t Deriving_Json.t =
<fun>
|}]
type t = A | B [@@deriving json]
[%%expect {|
type t =
| A
| B [@@deriving json]
include
struct
let _ = fun (_ : t) -> ()
let rec of_json : Deriving_Json_lexer.lexbuf -> t =
fun buf ->
match Deriving_Json_lexer.read_case buf with
| `Cst 1 -> B
| `Cst 0 -> A
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf
let _ = of_json
let rec to_json : Buffer.t -> t -> unit =
fun buf ->
function
| B -> Deriving_Json.Json_int.write buf 1
| A -> Deriving_Json.Json_int.write buf 0
let _ = to_json
let json : t Deriving_Json.t = Deriving_Json.make to_json of_json
let _ = json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type t = A | B
val of_json : Deriving_Json_lexer.lexbuf -> t = <fun>
val to_json : Buffer.t -> t -> unit = <fun>
val json : t Deriving_Json.t = <abstr>
|}];;
let x = [%json: t option]
[%%expect {|
let x =
Deriving_Json.make
(fun buf a ->
Deriving_Json.write_option (fun buf a -> to_json buf a) buf a)
(fun buf -> Deriving_Json.read_option (fun buf -> of_json buf) buf);;
val x : t option Deriving_Json.t = <abstr>
|}];;
let y = [%to_json: t list]
[%%expect {|
let y x =
let buf = Buffer.create 50 in
((fun buf a -> Deriving_Json.write_list (fun buf a -> to_json buf a) buf a))
buf x;
Buffer.contents buf;;
val y : t list -> string = <fun>
|}];;
let z = [%json_of: t array]
[%%expect {|
let z x =
let buf = Buffer.create 50 in
((fun buf a -> Deriving_Json.write_array (fun buf a -> to_json buf a) buf a))
buf x;
Buffer.contents buf;;
val z : t array -> string = <fun>
|}];;
let t = [%of_json: t * t]
[%%expect {|
let t s =
(fun buf ->
Deriving_Json_lexer.read_lbracket buf;
ignore (Deriving_Json_lexer.read_tag_1 0 buf);
Deriving_Json_lexer.read_comma buf;
(let a = of_json buf in
Deriving_Json_lexer.read_comma buf;
(let b = of_json buf in Deriving_Json_lexer.read_rbracket buf; (a, b))))
(Deriving_Json_lexer.init_lexer (Lexing.from_string s));;
val t : string -> t * t = <fun>
|}]
;;
type id' = int [@@deriving json]
[%%expect {|
type id' = int[@@deriving json]
include
struct
let _ = fun (_ : id') -> ()
let rec id'_of_json : Deriving_Json_lexer.lexbuf -> id' =
fun buf -> Deriving_Json.Json_int.read buf
let _ = id'_of_json
let rec id'_to_json : Buffer.t -> id' -> unit =
fun buf a -> Deriving_Json.Json_int.write buf a
let _ = id'_to_json
let id'_json : id' Deriving_Json.t =
Deriving_Json.make id'_to_json id'_of_json
let _ = id'_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type id' = int
val id'_of_json : Deriving_Json_lexer.lexbuf -> id' = <fun>
val id'_to_json : Buffer.t -> id' -> unit = <fun>
val id'_json : id' Deriving_Json.t = <abstr>
|}]
type poly3 =
[ poly1
| `C of [`p1 of poly1 | `p2 of poly2 | `p3 of poly3] ]
[@@deriving json]
[%%expect
{|
type poly3 =
[ | poly1 | `C of [ `p1 of poly1 | `p2 of poly2 | `p3 of poly3 ] ]
[@@deriving json]
include
struct
let _ = fun (_ : poly3) -> ()
let rec poly3_recognize : [ `NCst of int | `Cst of int ] -> bool =
function
| x when poly1_recognize x -> true
| `NCst 67 -> true
| _ -> false
let _ = poly3_recognize
let rec poly3_of_json_with_tag :
Deriving_Json_lexer.lexbuf -> [ `NCst of int | `Cst of int ] -> poly3
=
fun buf ->
function
| x when poly1_recognize x ->
(poly1_of_json_with_tag buf x :> [ | poly1
| `C of
[ `p1 of poly1
| `p2 of poly2
| `p3 of poly3 ] ])
| `NCst 67 ->
(Deriving_Json_lexer.read_comma buf;
(let v =
(fun buf ->
function
| `NCst 25025 ->
(Deriving_Json_lexer.read_comma buf;
(let v = poly1_of_json buf in
Deriving_Json_lexer.read_rbracket buf; `p1 v))
| `NCst 25026 ->
(Deriving_Json_lexer.read_comma buf;
(let v = poly2_of_json buf in
Deriving_Json_lexer.read_rbracket buf; `p2 v))
| `NCst 25027 ->
(Deriving_Json_lexer.read_comma buf;
(let v = poly3_of_json buf in
Deriving_Json_lexer.read_rbracket buf; `p3 v))
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf) buf
(Deriving_Json_lexer.read_vcase buf) in
Deriving_Json_lexer.read_rbracket buf; `C v))
| _ -> Deriving_Json_lexer.tag_error ~typename:"" buf
and poly3_of_json : Deriving_Json_lexer.lexbuf -> poly3 =
fun buf ->
poly3_of_json_with_tag buf (Deriving_Json_lexer.read_vcase buf)
let _ = poly3_of_json_with_tag
and _ = poly3_of_json
let rec poly3_to_json : Buffer.t -> [> poly3] -> unit =
fun buf a ->
match a with
| #poly1 as a -> poly1_to_json buf a
| `C a ->
let (a, b) = (67, a) in
(Buffer.add_string buf "[0";
((Buffer.add_string buf ","; Deriving_Json.Json_int.write buf a);
Buffer.add_string buf ",";
(match b with
| `p1 a ->
let (a, b) = (25025, a) in
(Buffer.add_string buf "[0";
((Buffer.add_string buf ",";
Deriving_Json.Json_int.write buf a);
Buffer.add_string buf ",";
poly1_to_json buf b);
Buffer.add_string buf "]")
| `p2 a ->
let (a, b) = (25026, a) in
(Buffer.add_string buf "[0";
((Buffer.add_string buf ",";
Deriving_Json.Json_int.write buf a);
Buffer.add_string buf ",";
poly2_to_json buf b);
Buffer.add_string buf "]")
| `p3 a ->
let (a, b) = (25027, a) in
(Buffer.add_string buf "[0";
((Buffer.add_string buf ",";
Deriving_Json.Json_int.write buf a);
Buffer.add_string buf ",";
poly3_to_json buf b);
Buffer.add_string buf "]")));
Buffer.add_string buf "]")
let _ = poly3_to_json
let poly3_json : poly3 Deriving_Json.t =
Deriving_Json.make poly3_to_json poly3_of_json
let _ = poly3_json
end[@@ocaml.doc "@inline"][@@merlin.hide ];;
type poly3 =
[ `A
| `B of string
| `C of [ `p1 of poly1 | `p2 of poly2 | `p3 of poly3 ] ]
val poly3_recognize : [ `Cst of int | `NCst of int ] -> bool = <fun>
val poly3_of_json_with_tag :
Deriving_Json_lexer.lexbuf -> [ `Cst of int | `NCst of int ] -> poly3 =
<fun>
val poly3_of_json : Deriving_Json_lexer.lexbuf -> poly3 = <fun>
val poly3_to_json : Buffer.t -> poly3 -> unit = <fun>
val poly3_json : poly3 Deriving_Json.t = <abstr>
|}]
|