12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091(**************************************************************************)(* *)(* Copyright 2012-2019 OCamlPro *)(* *)(* All rights reserved. This file is distributed under the terms of the *)(* GNU Lesser General Public License version 2.1, with the special *)(* exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)typet=[`Null|`Boolofbool|`Floatoffloat|`Stringofstring|`Aoftlist|`Oof(string*t)list]type'aencoder='a->ttype'adecoder=t->'aoptionletaddcbc=Buffer.add_charbcletaddsbs=Buffer.add_stringbsletadds_escbs=letlen=String.lengthsinletmax_idx=len-1inletflushbstarti=ifstart<lenthenBuffer.add_substringbsstart(i-start);inletrecloopstarti=matchi>max_idxwith|true->flushbstarti|false->letnext=i+1inmatchString.getsiwith|'"'->flushbstarti;addsb"\\\"";loopnextnext|'\\'->flushbstarti;addsb"\\\\";loopnextnext|'\x00'..'\x1F'|'\x7F'(* US-ASCII control chars *)asc->flushbstarti;addsb(Printf.sprintf"\\u%04X"(Char.codec));loopnextnext|_->loopstartnextinloop00letenc_json_stringbs=addcb'"';adds_escbs;addcb'"'letenc_vsepb=addcb','letenc_lexemeb=function|`Null->addsb"null"|`Booltrue->addsb"true"|`Boolfalse->addsb"false"|`Floatf->Printf.bprintfb"%.16g"f|`Strings->enc_json_stringbs|`Namen->enc_json_stringbn;addcb':'|`As->addcb'['|`Ae->addcb']'|`Os->addcb'{'|`Oe->addcb'}'letenc_jsonb(json:t)=letenc=enc_lexemeinletenc_sepseqenc_seqkb=matchseqwith|[]->enc_seqseqkb|seq->enc_vsepb;enc_seqseqkbinletrecvaluevkb=matchvwith|`Avs->arrvskb|`Oms->objmskb|`Null|`Bool_|`Float_|`String_asv->encbv;kbandarrvskb=encb`As;arr_vsvskbandarr_vsvskb=matchvswith|v::vs'->valuev(enc_sepvs'arr_vsk)b|[]->encb`Ae;kbandobjmskb=encb`Os;obj_msmskbandobj_msmskb=matchmswith|(n,v)::ms->encb(`Namen);valuev(enc_sepmsobj_msk)b|[]->encb`Oe;kbinvaluejson(fun_->())bletto_string(json:t)=letb=Buffer.create1024inenc_jsonbjson;Buffer.contentsbletjson_buffer=ref[]letappendkeyjson=json_buffer:=(key,json)::!json_bufferletflushoc=letb=Buffer.create1024inletjson=(`O(List.rev!json_buffer))inletjson=enc_jsonbjson;Buffer.contentsbinoutput_stringocjson;flushoc