123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177(*****************************************************************************)(* Open Source License *)(* Copyright (c) 2021-present Étienne Marais <etienne@maiste.fr> *)(* *)(* 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. *)(* *)(*****************************************************************************)let(>>=)=Result.bindlet(>|=)vf=Result.mapfvtypet=(Ezjsonm.value,[`Msgofstring])resultletcreate?(kind=`Obj)()=matchkindwith|`Strstr->Ok(`Stringstr)|`Floatn->Ok(`Floatn)|`Obj->Ok(`O[])|`Arr->Ok(`A[])leterrormsg=Error(`Msgmsg)letezjsonm_value_to_string=function|`Null->"null"|`Boolb->Format.sprintf"bool (%b)"b|`Floatf->Format.sprintf"float (%f)"f|`Strings->Format.sprintf"string (%s)"s|`A_->"json array"|`O_->"json object"letconversion_errorgotexpected=leterror_msg=Format.sprintf"Conversion error: trying to convert %s into %s."(ezjsonm_value_to_stringgot)expectedinerrorerror_msgletof_stringcontent=tryOk(Ezjsonm.from_stringcontent)withEzjsonm.Parse_error(_,str_err)->errorstr_errletgetojsonname=json>>=function|`Ofields->(matchList.assoc_optnamefieldswith|None->letmsg=Format.sprintf"JSON object doesn't contain the %s field."nameinerrormsg|Somejson->Okjson)|_->letmsg="Trying to access a non-object field in JSON."inerrormsgletgetajsonnth=json>>=function|`Aitems->(matchList.nth_optitemsnthwith|None->letmsg=Format.sprintf"JSON array doesn't contain the %d field."nthinerrormsg|Someitem->Okitem)|_->letmsg="Trying to access a non-array field in JSON."inerrormsgletgeto_from_ajson(k,v):t=letis_obj_with_field=function|`Ofields->(matchList.assoc_optkfieldswith|Some(`Stringv')->v'=v|_->false)|_->falseinjson>>=function|`Aobjs->(tryOk(List.findis_obj_with_fieldobjs)withNot_found->letmsg="Can't find the field into the array."inerrormsg)|_->letmsg="Trying to access a non-array field in JSON."inerrormsgletto_int_rjson=json>>=function|`Floatfasjson->ifFloat.is_integerfthenOk(int_of_floatf)elseconversion_errorjson"integer"|`Stringsasjson->(matchint_of_string_optswith|None->conversion_errorjson"integer"|Somei->Oki)|json->conversion_errorjson"integer"letto_float_rjson=json>>=function|`Floatf->Okf|`Stringsasjson->(matchfloat_of_string_optswith|None->conversion_errorjson"float"|Somef->Okf)|json->conversion_errorjson"float"letto_string_rjson=json>>=function`Strings->Oks|json->conversion_errorjson"string"letto_unit_rjson=json>>=function_->Ok()letpp_rjson=json>|=funjson->Ezjsonm.value_to_string~minify:falsejson|>Format.printf"%s"letaddot(k,v)=v>>=funv->t>>=function|`Oassoc->Ok(`O((k,v)::assoc))|_->letmsg=Format.sprintf"Trying to add key-value to a non-object json for key %s.)"kinerrormsgletaddatv=v>>=funv->t>>=function|`Aarr->Ok(`A(v::arr))|_->letmsg=Format.sprintf"Trying to add a value to a non-array json."inerrormsgletexportjson=json>|=funjson->Ezjsonm.value_to_stringjsonmoduleInfix=structlet(~+)v=create~kind:(`Strv)()let(~$)v=create~kind:(`Floatv)()let(-->)jsonname=getojsonnamelet(|->)jsonnth=getajsonnthlet(|->?)jsonkv=geto_from_ajsonkvlet(-+>)jsonkv=addojsonkvlet(|+>)jsonkv=addajsonkvendmodulePrivate=structletfilter_errorjson=json>>=function|`Ofieldsasjson->(List.assoc_opt"errors"fields|>function|Some(`A[`Strings])->letmsg=Format.sprintf"The API returns the following error \"%s\"."sinerrormsg|_->Okjson)|json->Okjsonend