123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151(*
* This file is part of the Watson Conversation Service OCaml API project.
*
* Copyright 2016-2017 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)openWcs_t(** {6 Utility function} *)letread_json_filereaderf=begintryletlexstate=Yojson.init_lexer~fname:f()inletch=open_infinletlexbuf=Lexing.from_channelchinletjson=readerlexstatelexbufinclose_inch;jsonwith|Yojson.Json_errorerr->Log.error"Json"None("Unable to parse file "^f^": "^err)|exn->Log.error"Json"None("Unable to read file "^f^": "^(Printexc.to_stringexn))end(** {6 Builders} *)letnull:json=`Nullletint(n:int):json=`Intnletbool(b:bool):json=`Boolbletstring(s:string):json=`Stringsletassoc(o:(string*json)list):json=`Assocoletlist(l:jsonlist):json=`Listl(** {6 Manipulation functions} *)letset(ctx:json)(lbl:string)(v:json):json=beginmatchctxwith|`Null->`Assoc[lbl,v]|`Assocl->`Assoc((lbl,v)::(List.remove_assoclbll))|_->Log.error"Json"(Somectx)"Unable to add a property to a non-object value"endlettake(ctx:json)(lbl:string):json*jsonoption=beginmatchctxwith|`Assocl->begintryletv=List.assoclbllin`Assoc(List.remove_assoclbll),SomevwithNot_found->ctx,Noneend|_->ctx,Noneendletget(ctx:json)(lbl:string):jsonoption=begintrybeginmatchYojson.Basic.Util.memberlblctxwith|`Null->None|x->Somexendwith_->Noneendletassign(os:jsonlist):json=List.fold_left(funacco->beginmatchowith|`Assocl->List.fold_right(fun(lbl,v)acc->setacclblv)lacc|_->Log.error"Json"(Someacc)""end)nullosletpush(ctx:json)(lbl:string)(v:json):json=beginmatchtakectxlblwith|ctx,(None|Some`Null)->setctxlbl(`List[v])|ctx,Some(`Listl)->setctxlbl(`List(l@[v]))|ctc,Some_->Log.error"Json"(Somectx)"Unable to push an element in a non-list property"endletpop(ctx:json)(lbl:string):json*jsonoption=beginmatchtakectxlblwith|ctx,Some(`List(v::l))->setctxlbl(`Listl),Somev|_->ctx,Noneend(** {6 Setters and getters} *)(** {8 Bool} *)letset_bool(ctx:json)(lbl:string)(b:bool):json=setctxlbl(`Boolb)letget_bool(ctx:json)(lbl:string):booloption=beginmatchYojson.Basic.Util.memberlblctxwith|`Boolb->Someb|_->Noneend(** {8 String} *)letset_string(ctx:json)(lbl:string)(s:string):json=setctxlbl(`Strings)letget_string(ctx:json)(lbl:string):stringoption=beginmatchgetctxlblwith|Some(`Strings)->Somes|_->Noneendlettake_string(ctx:json)(lbl:string):json*stringoption=beginmatchtakectxlblwith|ctx,Some(`Strings)->ctx,Somes|ctx,_->ctx,Noneend