Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file json.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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. *)(* *)(*****************************************************************************)typejson=[`Oof(string*json)list|`Boolofbool|`Floatoffloat|`Aofjsonlist|`Null|`Stringofstring]typeschema=Json_schema.schematypepair_builder={build:'a'b.Encoding.Kind.t->'aEncoding.t->'bEncoding.t->('a*'b)Encoding.t;}exceptionParse_errorofstringletwrap_errorfstr=tryfstrwithexn->raise(Json_encoding.Cannot_destruct([],exn))letint64_encoding=letopenJson_encodingindef"int64"~title:"64 bit integers"~description:"Decimal representation of 64 bit integers"@@convInt64.to_string(wrap_errorInt64.of_string)stringletn_encoding=letopenJson_encodingindef"positive_bignum"~title:"Positive big number"~description:"Decimal representation of a positive big number"@@conv(funz->ifZ.signz<0theninvalid_arg"negative natural";Z.to_stringz)(funs->letn=Z.of_stringsinifZ.signn<0thenraise(Json_encoding.Cannot_destruct([],Failure"negative natural"));n)stringletz_encoding=letopenJson_encodingindef"bignum"~title:"Big number"~description:"Decimal representation of a big number"@@convZ.to_stringZ.of_stringstringletbytes_jsont=letopenJson_encodinginletschema=letopenJson_schemaincreate{title=None;description=None;default=None;enum=None;kind=String{str_format=None;pattern=Some"^[a-zA-Z0-9]+$";min_length=0;max_length=None;};format=None;id=None;}inconv~schemaHex.of_bytes(wrap_errorHex.to_bytes)(conv(fun(`Hexh)->h)(funh->`Hexh)string)letcheck_utf8s=Uutf.String.fold_utf_8(funvalid_pos->function`Uchar_->valid|`Malformed_->false)truesletraw_string_encoding=letopenJson_encodinginletutf8_case=casestring(funs->ifcheck_utf8sthenSomeselseNone)(funs->s)inletobj_case=case(obj1(req"invalid_utf8_string"(array(ranged_int~minimum:0~maximum:255"byte"))))(funs->Some(Array.init(String.lengths)(funi->Char.codes.[i])))(funa->String.init(Array.lengtha)(funi->Char.chra.(i)))indef"unistring"~title:"Universal string representation"~description:"Either a plain UTF8 string, or a sequence of bytes for strings that \
contain invalid byte sequences."(union[utf8_case;obj_case])letreclift_union:typea.aEncoding.t->aEncoding.t=fune->letopenEncodinginmatche.encodingwith|Conv{proj;inj;encoding=e;schema}->(matchlift_unionewith|{encoding=Union{kind;tag_size;tagged_cases;cases;match_case};_}->letmatch_casex=match_case(projx)inletlift(Case{title;description;encoding;proj=proj';inj=inj';tag})=Case{encoding;title;description;proj=(funx->proj'(projx));inj=(funx->inj(inj'x));tag;}inmake@@Union{kind;tag_size;tagged_cases=Array.maplifttagged_cases;match_case;cases=List.mapliftcases;}|e->make@@Conv{proj;inj;encoding=e;schema})|Objs{kind;left;right}->lift_union_in_pair{build=(funkindleftright->make@@Objs{kind;left;right})}kindleftright|Tups{kind;left;right}->lift_union_in_pair{build=(funkindleftright->make@@Tups{kind;left;right})}kindleftright|_->eandlift_union_in_pair:typeab.pair_builder->Encoding.Kind.t->aEncoding.t->bEncoding.t->(a*b)Encoding.t=funbpe1e2->letopenEncodinginmatch(lift_unione1,lift_unione2)with|(e1,{encoding=Union{tag_size;match_case;cases;tagged_cases;_};_})->letmatch_case(x,y)=matchmatch_caseywith|Matched(tag,e2,v)->Matched(tag,lift_union_in_pairbpe1e2,(x,v))inletlift(Case{title;description;encoding=e2;proj;inj;tag})=Case{encoding=lift_union_in_pairbpe1e2;title;description;proj=(fun(x,y)->matchprojywithNone->None|Somey->Some(x,y));inj=(fun(x,y)->(x,injy));tag;}inmake@@Union{kind=`Dynamic(* ignored *);tag_size;tagged_cases=Array.maplifttagged_cases;match_case;cases=List.mapliftcases;}|({encoding=Union{tag_size;tagged_cases;match_case;cases;_};_},e2)->letmatch_case(x,y)=matchmatch_casexwith|Matched(tag,e1,v)->Matched(tag,lift_union_in_pairbpe1e2,(v,y))inletlift(Case{title;description;encoding=e1;proj;inj;tag})=Case{encoding=lift_union_in_pairbpe1e2;title;description;proj=(fun(x,y)->matchprojxwithNone->None|Somex->Some(x,y));inj=(fun(x,y)->(injx,y));tag;}inmake@@Union{kind=`Dynamic(* ignored *);tag_size;tagged_cases=Array.maplifttagged_cases;match_case;cases=List.mapliftcases;}|(e1,e2)->b.buildpe1e2letrecjson:typea.aEncoding.desc->aJson_encoding.encoding=letopenEncodinginletopenJson_encodinginfunction|Null->null|Empty->empty|Constants->constants|Ignore->unit|Int8->ranged_int~minimum:~-(1lsl7)~maximum:((1lsl7)-1)"int8"|Uint8->ranged_int~minimum:0~maximum:((1lsl8)-1)"uint8"|Int16->ranged_int~minimum:~-(1lsl15)~maximum:((1lsl15)-1)"int16"|Uint16->ranged_int~minimum:0~maximum:((1lsl16)-1)"uint16"|RangedInt{minimum;maximum}->ranged_int~minimum~maximum"rangedInt"|Int31->int|Int32->int32|Int64->int64_encoding|N->n_encoding|Z->z_encoding|Bool->bool|Float->float|RangedFloat{minimum;maximum}->ranged_float~minimum~maximum"rangedFloat"|String(`Fixedexpected)->letchecks=letfound=String.lengthsiniffound<>expectedthenraise(Cannot_destruct([],Unexpected(Format.asprintf"string (len %d)"found,Format.asprintf"string (len %d)"expected)));sinconvcheckcheckraw_string_encoding|String_->raw_string_encoding|Padded(e,_)->get_jsone|Bytes(`Fixedexpected)->letchecks=letfound=Bytes.lengthsiniffound<>expectedthenraise(Cannot_destruct([],Unexpected(Format.asprintf"string (len %d)"found,Format.asprintf"string (len %d)"expected)));sinconvcheckcheckbytes_jsont|Bytes_->bytes_jsont|String_enum(tbl,_)->string_enum(Hashtbl.fold(funa(str,_)acc->(str,a)::acc)tbl[])|Array(_,e)->array(get_jsone)(* FIXME TODO enforce max_length *)|List(_,e)->list(get_jsone)|Objf->obj1(field_jsonf)|Objs{left;right;_}->merge_objs(get_jsonleft)(get_jsonright)|Tupe->tup1(get_jsone)|Tups{left;right;_}->merge_tups(get_jsonleft)(get_jsonright)|Conv{proj;inj;encoding=e;schema}->conv?schemaprojinj(get_jsone)|Describe{id;title;description;encoding=e}->defid?title?description(get_jsone)|Mu{name;fix;_}asty->muname(funjson_encoding->get_json@@fix(make~json_encodingty))|Union{cases;_}->union(List.mapcase_jsoncases)|Splitted{json_encoding;_}->json_encoding|Dynamic_size{encoding=e;_}->get_jsone|Check_size{encoding;_}->get_jsonencoding|Delayedf->get_json(f())andfield_json:typea.aEncoding.field->aJson_encoding.field=letopenJson_encodinginfunction|Encoding.Req{name;encoding=e;title;description}->req?title?descriptionname(get_jsone)|Encoding.Opt{name;encoding=e;title;description;kind=_}->opt?title?descriptionname(get_jsone)|Encoding.Dft{name;encoding=e;default=d;title;description}->dft?title?descriptionname(get_jsone)dandcase_json:typea.aEncoding.case->aJson_encoding.case=letopenJson_encodinginfunction|Encoding.Case{encoding=e;proj;inj;tag=_;title;description}->case~title?description(get_jsone)projinjandget_json:typea.aEncoding.t->aJson_encoding.encoding=fune->matche.json_encodingwith|None->letjson_encoding=json(lift_unione).encodingine.json_encoding<-Somejson_encoding;json_encoding|Somejson_encoding->json_encodingletconvert=get_jsontypepath=path_itemlistandpath_item=[`Fieldofstring(** A field in an object. *)|`Indexofint(** An index in an array. *)|`Star(** Any / every field or index. *)|`Next(** The next element after an array. *)]includeJson_encodingletconstructev=construct(get_jsone)vtypejsonm_lexeme=[`Null|`Boolofbool|`Stringofstring|`Floatoffloat|`Nameofstring|`As|`Ae|`Os|`Oe]letconstruct_seqev=construct_seq(get_jsone)vletdestructev=destruct(get_jsone)vletschema?definitions_pathe=schema?definitions_path(get_jsone)letcannot_destructfmt=Format.kasprintf(funmsg->raise(Cannot_destruct([],Failuremsg)))fmttypet=jsonletto_string?(newline=false)?minifyj=Format.asprintf"%a%s"Json_repr.(pp?compact:minify(moduleEzjsonm))j(ifnewlinethen"\n"else"")letpp=Json_repr.(pp(moduleEzjsonm))letfrom_strings=matchEzjsonm.from_string("["^s^"]")with|exceptionEzjsonm.Parse_error(_,msg)->Errormsg|`A[json]->Okjson|_->Error"Malformed value"letencoding=letbinary:Json_repr.ezjsonmEncoding.t=Encoding.conv(funjson->Json_repr.convert(moduleJson_repr.Ezjsonm)(moduleJson_repr_bson.Repr)json|>Json_repr_bson.bson_to_bytes|>Bytes.to_string)(funs->tryBytes.of_strings|>Json_repr_bson.bytes_to_bson~copy:false|>Json_repr.convert(moduleJson_repr_bson.Repr)(moduleJson_repr.Ezjsonm)withJson_repr_bson.Bson_decoding_error(msg,_,_)->raise(Parse_errormsg))Encoding.stringinletjson=Json_encoding.any_ezjson_valueinEncoding.raw_splitted~binary~jsonletschema_encoding=Encoding.convJson_schema.to_jsonJson_schema.of_jsonencoding