@@ -520,3 +520,82 @@ val json :
520520 'a Deriving_Json.t -> 'b Deriving_Json.t -> ('a, 'b) t Deriving_Json.t =
521521 <fun>
522522|}]
523+
524+
525+ type t = A | B [@@deriving json]
526+ [%%expect {|
527+
528+ type t =
529+ | A
530+ | B [@@deriving json] let _ = fun (_ : t) -> ()
531+ let rec (of_json : Deriving_Json_lexer.lexbuf -> t) =
532+ fun buf ->
533+ match Deriving_Json_lexer.read_case buf with
534+ | `Cst 1 -> B
535+ | `Cst 0 -> A
536+ | _ -> Deriving_Json_lexer.tag_error ~typename:"" buf let _ = of_json
537+ let rec (to_json : Buffer.t -> t -> unit) =
538+ fun buf ->
539+ function
540+ | B -> Deriving_Json.Json_int.write buf 1
541+ | A -> Deriving_Json.Json_int.write buf 0 let _ = to_json
542+ let (json : t Deriving_Json.t) = Deriving_Json.make to_json of_json
543+ let _ = json;;
544+ type t = A | B
545+ val of_json : Deriving_Json_lexer.lexbuf -> t = <fun>
546+ val to_json : Buffer.t -> t -> unit = <fun>
547+ val json : t Deriving_Json.t = <abstr>
548+ |}];;
549+
550+ let x = [%json: t option]
551+ [%%expect {|
552+
553+ let x =
554+ Deriving_Json.make
555+ (fun buf ->
556+ fun a ->
557+ Deriving_Json.write_option (fun buf -> fun a -> to_json buf a) buf a)
558+ (fun buf -> Deriving_Json.read_option (fun buf -> of_json buf) buf);;
559+ val x : t option Deriving_Json.t = <abstr>
560+ |}];;
561+
562+ let y = [%to_json: t list]
563+ [%%expect {|
564+
565+ let y x =
566+ let buf = Buffer.create 50 in
567+ ((fun buf ->
568+ fun a ->
569+ Deriving_Json.write_list (fun buf -> fun a -> to_json buf a) buf a))
570+ buf x;
571+ Buffer.contents buf;;
572+ val y : t list -> string = <fun>
573+ |}];;
574+
575+ let z = [%json_of: t array]
576+ [%%expect {|
577+
578+ let z x =
579+ let buf = Buffer.create 50 in
580+ ((fun buf ->
581+ fun a ->
582+ Deriving_Json.write_array (fun buf -> fun a -> to_json buf a) buf a))
583+ buf x;
584+ Buffer.contents buf;;
585+ val z : t array -> string = <fun>
586+ |}];;
587+
588+ let t = [%of_json: t * t]
589+ [%%expect {|
590+
591+ let t s =
592+ (fun buf ->
593+ Deriving_Json_lexer.read_lbracket buf;
594+ ignore (Deriving_Json_lexer.read_tag_1 0 buf);
595+ Deriving_Json_lexer.read_comma buf;
596+ (let a = of_json buf in
597+ Deriving_Json_lexer.read_comma buf;
598+ (let b = of_json buf in Deriving_Json_lexer.read_rbracket buf; (a, b))))
599+ (Deriving_Json_lexer.init_lexer (Lexing.from_string s));;
600+ val t : string -> t * t = <fun>
601+ |}]
0 commit comments