123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326(*****************************************************************************)(* *)(* Open Source License *)(* Copyright 2014 OCamlPro *)(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)type'aview=[`Oof(string*'a)list|`Aof'alist|`Boolofbool|`Floatoffloat|`Stringofstring|`Null]type'arepr_uid='aoptionref(* This is used for limiting conversions. When a value is converted
from a representation to another, which mostly happens when using
the {!type:any} boxing, such as when writing custom encodings, the
original value is usually traversed using the [view] of the
original representation, and recreated using the [repr] of the
destination representation. When converting from a representation
to itself, we want to optimize out this transformation, that is a
deep copy, and just get the same value. For this, we have to prove
to OCaml that it is indeed a value from the same representation.
To do that, we use the following trick. Each representation has a
bucket, the uid below. When converting from the original
representation, we put the value in its bucket. Then, we check the
bucket of the destination, and if it happens to be occupied, we
find in it the original value, under the destination type. Voilà. *)letrepr_uid()=refNoneleteq_repr_uid:'a->'arepr_uid->'brepr_uid->'boption=funatatb->tb:=None;ta:=Somea;!tbmoduletypeRepr=sigtypevaluevalview:value->valueviewvalrepr:valueview->valuevalrepr_uid:valuerepr_uidendmoduleEzjsonm=structtypevalue=[`Oof(string*value)list|`Aofvaluelist|`Boolofbool|`Floatoffloat|`Stringofstring|`Null]letviewv=vletreprv=vletrepr_uid=repr_uid()endtypeezjsonm=Ezjsonm.valuemoduleYojson=structtypevalue=[`Boolofbool|`Assocof(string*value)list|`Floatoffloat|`Intofint|`Intlitofstring|`Listofvaluelist|`Null|`Stringofstring|`Tupleofvaluelist|`Variantofstring*valueoption]letview=function|`Intliti->`Stringi|`Tuplel->`Al|`Variant(label,Somex)->`A[`Stringlabel;x]|`Variant(label,None)->`Stringlabel|`Assocl->`Ol|`Listl->`Al|`Inti->`Float(floati)|`Floatf->`Floatf|`Strings->`Strings|`Null->`Null|`Boolb->`Boolbletrepr=function|`Ol->`Assocl|`Al->`Listl|`Boolb->`Boolb|`Floatf->`Floatf|`Strings->`Strings|`Null->`Nullletrepr_uid=repr_uid()endtypeyojson=Yojson.valueletconvert:typetttf.(moduleReprwithtypevalue=tf)->(moduleReprwithtypevalue=tt)->tf->tt=fun(moduleRepr_f)(moduleRepr_t)v->matcheq_repr_uidvRepr_f.repr_uidRepr_t.repr_uidwith|Somer->r|None->letrecconvv=matchRepr_f.viewvwith|(`Float_|`Bool_|`String_|`Null)asv->Repr_t.reprv|`Avalues->Repr_t.repr(`A(List.mapconvvalues))|`Ovalues->Repr_t.repr(`O(List.map(fun(k,v)->(k,convv))values))inconvvletpp_stringppfs=Format.fprintfppf"\"";fori=0toString.lengths-1domatchs.[i]with|'\"'->Format.fprintfppf"\\\""|'\n'->Format.fprintfppf"\\n"|'\r'->Format.fprintfppf"\\r"|'\b'->Format.fprintfppf"\\b"|'\t'->Format.fprintfppf"\\t"|'\\'->Format.fprintfppf"\\\\"|'\x00'..'\x1F'asc->Format.fprintfppf"\\u%04x"(Char.codec)|c->Format.fprintfppf"%c"cdone;Format.fprintfppf"\""letpp?(compact=false)?(pp_string=pp_string)(typevalue)(moduleRepr:Reprwithtypevalue=value)ppf(v:value)=letrecpp_compactppfv=matchRepr.viewvwith|`Ol->letpp_sepppf()=Format.fprintfppf","inletpp_fieldppf(name,v)=Format.fprintfppf"%a:%a"pp_stringnamepp_compactvinFormat.fprintfppf"{%a}"(Format.pp_print_list~pp_seppp_field)l|`Al->letpp_sepppf()=Format.fprintfppf","inFormat.fprintfppf"[%a]"(Format.pp_print_list~pp_seppp_compact)l|`Booltrue->Format.fprintfppf"true"|`Boolfalse->Format.fprintfppf"false"|`Floatf->let(fract,intr)=modffiniffract=0.0thenFormat.fprintfppf"%.0f"intrelseFormat.fprintfppf"%g"f|`Strings->pp_stringppfs|`Null->Format.fprintfppf"null"inletrecpp_boxppfv=matchRepr.viewvwith|`O[]->Format.fprintfppf"{}"|`Ol->letpp_sepppf()=Format.fprintfppf",@ "inletpp_fieldppf(name,v)=Format.fprintfppf"@[<hov 2>%a:@ %a@]"pp_stringnamepp_boxvinFormat.fprintfppf"@[<hov 2>{ %a }@]"(Format.pp_print_list~pp_seppp_field)l|`A[]->Format.fprintfppf"[]"|`Al->letpp_sepppf()=Format.fprintfppf",@ "inFormat.fprintfppf"@[<hov 2>[ %a ]@]"(Format.pp_print_list~pp_seppp_box)l|_->pp_compactppfvinifcompactthenpp_compactppfvelsepp_boxppfvletfrom_yojsonnon_basic=(* Delete `Variant, `Tuple and `Intlit *)letrecto_basicnon_basic=matchnon_basicwith|`Intliti->`Stringi|`Tuplel->`List(List.mapto_basicl)|`Variant(label,Somex)->`List[`Stringlabel;to_basicx]|`Variant(label,None)->`Stringlabel|`Assocl->`Assoc(List.map(fun(key,value)->(key,to_basicvalue))l)|`Listl->`List(List.mapto_basicl)|`Inti->`Inti|`Floatf->`Floatf|`Strings->`Strings|`Null->`Null|`Boolb->`Boolbin(* Rename `Assoc, `Int and `List *)letrecto_value:'a._->([>ezjsonm]as'a)=function|`Listl->`A(List.mapto_valuel)|`Assocl->`O(List.map(fun(key,value)->(key,to_valuevalue))l)|`Inti->`Float(float_of_inti)|`Floatf->`Floatf|`Null->`Null|`Strings->`Strings|`Boolb->`Boolbinto_basic(non_basic:>yojson)|>to_valueletto_yojsonjson=letrecaux:'a._->([>yojson]as'a)=function|`Avalues->`List(List.mapauxvalues)|`Ovalues->`Assoc(List.map(fun(k,v)->(k,auxv))values)|`Floatf->let(fract,intr)=modffinletmax_intf=float0x3F_FF_FF_FFinletmin_intf=~-.max_intf-.1.iniffract=0.0thenifintr>=min_intf&&intr<=max_intfthen`Int(int_of_floatintr)else`Intlit(Printf.sprintf"%.0f"intr)else`Floatf|`Boolb->`Boolb|`Strings->`Strings|`Null->`Nullinaux(json:>ezjsonm)typeany=Value_with_repr:(moduleReprwithtypevalue='a)*'a->anyletpp_any?compact?pp_string()ppf(Value_with_repr(repr,v))=pp?compact?pp_stringreprppfvletany_to_repr:typett.(moduleReprwithtypevalue=tt)->any->tt=funrepr_t(Value_with_repr(repr_f,v))->convertrepr_frepr_tvletrepr_to_anyreprv=Value_with_repr(repr,v)letfrom_any:'a.any->([>ezjsonm]as'a)=funrepr->letres=any_to_repr(moduleEzjsonm)reprin(res:ezjsonm:>[>ezjsonm])letto_anyv=Value_with_repr((moduleEzjsonm),(v:>ezjsonm))