123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234openPrintfopenStdLabelsopenPpxlibopenAst_builder.DefaultopenPpx_deriving_toolsopenPpx_deriving_tools.ConvopenPpx_deriving_json_commonmoduleOf_json=structletbuild_tuple~locderivesi(ts:core_typelist)e=pexp_tuple~loc(List.mapits~f:(funit->derivet[%exprJs.Array.unsafe_get[%ee][%eeint~loc(si+i)]]))letbuild_js_type~loc(fs:label_declarationlist)=letfld=letn=ld.pld_nameinletn=Option.value~default:n(ld_attr_json_keyld)inletpof_desc=Otag(n,[%type:Js.Json.tJs.undefined])in{pof_loc=loc;pof_attributes=[];pof_desc}inletrow=ptyp_object~loc(List.mapfs~f)Closedin[%type:[%trow]Js.t]letbuild_record~locderive(fs:label_declarationlist)xmake=lethandle_fieldfsld=(map_loclidentld.pld_name,letn=ld.pld_nameinletn=Option.value~default:n(ld_attr_json_keyld)in[%exprmatchJs.Undefined.toOption[%efs]##[%epexp_ident~loc:n.loc(map_loclidentn)]with|Stdlib.Option.Somev->[%ederiveld.pld_type[%exprv]]|Stdlib.Option.None->[%ematchld_attr_defaultldwith|Somedefault->default|None->[%exprPpx_deriving_json_runtime.of_json_error[%eestring~loc(sprintf"missing field %S"n.txt)]]]])in[%exprletfs=(Obj.magic[%ex]:[%tbuild_js_type~locfs])in[%emake(pexp_record~loc(List.mapfs~f:(handle_field[%exprfs]))None)]]leteis_json_object~locx=[%exprStdlib.(&&)(Stdlib.(=)(Js.typeof[%ex])"object")(Stdlib.(&&)(Stdlib.not(Js.Array.isArray[%ex]))(Stdlib.not(Stdlib.(==)(Obj.magic[%ex]:'aJs.null)Js.null)))]letensure_json_object~locx=[%exprifStdlib.not[%eeis_json_object~locx]thenPpx_deriving_json_runtime.of_json_error[%eestring~loc(sprintf"expected a JSON object")]]letensure_json_array_len~locnlen=[%exprifStdlib.(<>)[%elen][%eeint~locn]thenPpx_deriving_json_runtime.of_json_error[%eestring~loc(sprintf"expected a JSON array of length %i"n)]]letderive_of_tuplederivetx=letloc=t.tpl_locinletn=List.lengtht.tpl_typesin[%exprifStdlib.(&&)(Js.Array.isArray[%ex])(Stdlib.(=)(Js.Array.length(Obj.magic[%ex]:Js.Json.tarray))[%eeint~locn])thenletes=(Obj.magic[%ex]:Js.Json.tarray)in[%ebuild_tuple~locderive0t.tpl_types[%expres]]elsePpx_deriving_json_runtime.of_json_error[%eestring~loc(sprintf"expected a JSON array of length %i"n)]]letderive_of_recordderivetx=letloc=t.rcd_locin[%expr[%eensure_json_object~locx];[%ebuild_record~locderivet.rcd_fieldsxFun.id]]letderive_of_variant_derivetbodyx=letloc=t.vrt_locinletis_enum=List.for_allt.vrt_cases~f:(function|Vcs_enum_->true|_->false)inmatchis_enumwith|true->[%exprlettag=Ppx_deriving_json_runtime.Primitives.string_of_json[%ex]in[%ebody]]|false->[%exprifJs.Array.isArray[%ex]thenletarray=(Obj.magic[%ex]:Js.Json.tarray)inletlen=Js.Array.lengtharrayinifStdlib.(>)len0thenlettag=Js.Array.unsafe_getarray0inifStdlib.(=)(Js.typeoftag)"string"thenlettag=(Obj.magictag:string)in[%ebody]elsePpx_deriving_json_runtime.of_json_error"expected a non empty JSON array with element being a \
string"elsePpx_deriving_json_runtime.of_json_error"expected a non empty JSON array"elsePpx_deriving_json_runtime.of_json_error"expected a non empty JSON array"]letderive_of_variant_casederivemakecnext=matchcwith|Vcs_enum(n,ctx)->letloc=n.locinletn=Option.value~default:n(vcs_attr_json_asctx)in[%exprifStdlib.(=)tag[%eestring~loc:n.locn.txt]then[%emakeNone]else[%enext]]|Vcs_record(n,r)->letloc=n.locinletn=Option.value~default:n(vcs_attr_json_asr.rcd_ctx)in[%exprifStdlib.(=)tag[%eestring~loc:n.locn.txt]then([%eensure_json_array_len~loc2[%exprlen]];letfs=Js.Array.unsafe_getarray1in[%eensure_json_object~loc[%exprfs]];[%ebuild_record~locderiver.rcd_fields[%exprfs](fune->make(Somee))])else[%enext]]|Vcs_tuple(n,t)->letloc=n.locinletn=Option.value~default:n(vcs_attr_json_ast.tpl_ctx)inletarity=List.lengtht.tpl_typesin[%exprifStdlib.(=)tag[%eestring~loc:n.locn.txt]then([%eensure_json_array_len~loc(arity+1)[%exprlen]];[%eifStdlib.(=)arity0thenmakeNoneelsemake(Some(build_tuple~locderive1t.tpl_types[%exprarray]))])else[%enext]]letderiving:Ppx_deriving_tools.deriving=deriving_of()~name:"of_json"~error:(fun~loc->[%exprPpx_deriving_json_runtime.of_json_error"invalid JSON"])~of_t:(fun~loc->[%type:Js.Json.t])~derive_of_tuple~derive_of_record~derive_of_variant~derive_of_variant_caseendmoduleTo_json=structletas_json~locx=[%expr(Obj.magic[%ex]:Js.Json.t)]letderive_of_tuplederivetes=letloc=t.tpl_locinas_json~loc(pexp_array~loc(List.map2t.tpl_typeses~f:derive))letderive_of_recordderivetes=letloc=t.rcd_locinletfs=List.map2t.rcd_fieldses~f:(funldx->letn=ld.pld_nameinletn=Option.value~default:n(ld_attr_json_keyld)inletthis=deriveld.pld_typexinmap_loclidentn,this)inletrecord=pexp_record~locfsNoneinas_json~loc[%expr[%mel.obj[%erecord]]]letderive_of_variant_casederiveces=matchcwith|Vcs_enum(n,ctx)->letloc=n.locinletn=Option.value~default:n(vcs_attr_json_asctx)inlettag=[%exprstring_to_json[%eestring~loc:n.locn.txt]]inas_json~loctag|Vcs_record(n,r)->letloc=n.locinletn=Option.value~default:n(vcs_attr_json_asr.rcd_ctx)inlettag=[%exprstring_to_json[%eestring~loc:n.locn.txt]]inletes=[derive_of_recordderiveres]inas_json~loc(pexp_array~loc(tag::es))|Vcs_tuple(n,t)->letloc=n.locinletn=Option.value~default:n(vcs_attr_json_ast.tpl_ctx)inlettag=[%exprstring_to_json[%eestring~loc:n.locn.txt]]inletes=List.map2t.tpl_typeses~f:deriveinas_json~loc(pexp_array~loc(tag::es))letderiving:Ppx_deriving_tools.deriving=deriving_to()~name:"to_json"~t_to:(fun~loc->[%type:Js.Json.t])~derive_of_tuple~derive_of_record~derive_of_variant_caseendlet()=let_=Ppx_deriving_tools.registerOf_json.derivinginlet_=Ppx_deriving_tools.registerTo_json.derivinginlet_=Ppx_deriving_tools.register_combined"json"[To_json.deriving;Of_json.deriving]in()