123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293(******************************************************************************)(* Metadb *)(* Copyright (C) 2022 Nathan Guermond *)(* *)(* This program is free software: you can redistribute it and/or modify it *)(* under the terms of the GNU General Public License as published by the Free *)(* Software Foundation, either version 3 of the License, or (at your option) *)(* any later version. *)(* *)(* This program is distributed in the hope that it will be useful, but *)(* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *)(* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *)(* for more details. *)(* *)(* You should have received a copy of the GNU General Public License along *)(* with this program. If not, see <https://www.gnu.org/licenses/>. *)(* *)(******************************************************************************)includeYojson.BasicexceptionParsingFailureofstringletwrite_string(j:t):string=to_stringjletpretty_to_string(j:t):string=pretty_to_stringjletfrom_string(s:string):t=from_stringsletget(key:string)(json:t):toption=matchjsonwith|`Assockv_lst->(match(List.assoc_optkeykv_lst)with|Somev->Somev|None->None)|_->raise(ParsingFailure"Not an association list")letto_list(json:t):tlist=matchjsonwith|`Listj_lst->j_lst|_->raise(ParsingFailure"Not a list")letto_int(json:t):int=matchjsonwith|`Intk->k|_->letpp=pretty_to_stringjsoninraise(ParsingFailure("Not an int:"^pp))letto_bool(json:t):bool=matchjsonwith|`Boolb->b|_->raise(ParsingFailure"Not a bool")letto_string(json:t):string=matchjsonwith|`Strings->s|_->raise(ParsingFailure"Not a string")letraise_opterr(x:'aoption):'a=matchxwith|None|Some`Null->raise(ParsingFailureerr)|Somex->xletdefaultd(x:'aoption):'a=matchxwith|None|Some`Null->d|Somex->xletget_err(key:string)(json:t):t=raise_opt("Unbound key: `"^key^"`")(getkeyjson)letadd_entry(k:string)(v:t)(json:t):t=matchjsonwith|`Assockv_lst->(`Assoc((k,v)::kv_lst))|_->raise(ParsingFailure"Not an association list")letremove_entry(k:string)(json:t):t=matchjsonwith|`Assockv_lst->(`Assoc(List.remove_assockkv_lst))|_->raise(ParsingFailure"Not an association list")letto_file(path:Path.root)(json:t):unit=to_file(Path.string_of_rootpath)jsonletfrom_file(path:Path.root):t=from_file(Path.string_of_rootpath)