123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182(*
Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2017 Anton Lavrik
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.
*)moduleC=Piqi_commonopenCopenPiqobj_commontypejson=Piqi_json_type.json(* configuration/command-line option:
*
* omit missing fields in JSON output; if false, they will be present in the
* output with their values set to "null" for optional fields and "[]" for
* repeated fields
*)letomit_missing_fields=reftrueletomit_missingf=(* use the per-field schema-level setting when specified, default to the
* run-time setting otherwise *)matchf.T.Field.json_omit_missingwith|Somex->x|None->!omit_missing_fieldsletmake_namednamevalue=name,valueletmake_namename=name,`Booltrueletrecgen_obj(x:Piqobj.obj):json=matchxwith(* built-in types *)|`intx->`Intx|`uintx->`Uintx|`floatx->`Floatx|`boolx->`Boolx|`stringx->`Stringx|`binaryx->`String(Piqi_base64.encodex)|`anyx->gen_anyx(* custom types *)|`recordx->gen_recordx|`variantx->gen_variantx|`enumx->gen_enumx|`listx->gen_listx|`aliasx->gen_aliasxandgen_anyx=letopenAnyinifnot!Piqi_config.gen_extended_piqi_anythenmatchPiqobj.json_of_anyxwith|None->`Null()|Somejson_ast->json_astelse(* non-sybolic piqi-any representation *)letmake_json_fieldnamevaluef=matchvaluewith|Nonewhen!omit_missing_fields->[]|None->[name,`Null()]|Somex->[name,fx]inlettypename=make_json_field"type"x.typename(funname->`Stringname)inletprotobuf=make_json_field"protobuf"(Piqobj.pb_of_anyx)(funpb->`String(Piqi_base64.encodepb))inletjson=make_json_field"json"(Piqobj.json_of_anyx)(funjson_ast->json_ast)inletpiq=make_json_field"piq"(Piqobj.piq_of_anyx)(funpiq_ast->`String(!Piqobj.string_of_piqpiq_ast))in`Assoc((* this field indicates that this is an extended piqi-any representation
* (it is necessary for detecting which variant of piqi-any represenation
* is used and to make either representation automatically reversible) *)("piqi_type",`String"piqi-any")::(* actual content *)(typename@protobuf@json@piq))andgen_recordx=letopenRinletfield_types=x.t.T.Record.fieldin`Assoc(U.flatmap(gen_fieldx.field)field_types)andgen_fieldfieldst=letopenT.Fieldinletname=some_oft.json_nameinletopenFinletpredf=f.t==tinmatcht.modewith|`required|`optional->(tryletf=List.findpredfieldsinletres=matchf.objwith|None->assertfalse(* flag must be resolved to true or false by now *)|Someobj->make_namedname(gen_objobj)in[res]withNot_found->ifomit_missingtthen[]else[make_namedname(`Null())])|`repeated->letfields=List.find_allpredfieldsiniffields=[]&&omit_missingtthen[]elseletjson_fields=List.map(funf->gen_obj(some_off.obj))fieldsinletres=make_namedname(`Listjson_fields)in[res]andgen_variantx=letopenVinleto=gen_optionx.optionin`Assoc[o]andgen_optionx=letopenOinletname=some_ofx.t.T.Option.json_nameinmatchx.objwith|None->make_namename|Someobj->make_namedname(gen_objobj)andgen_enumx=letopenEingen_enum_optionx.optionandgen_enum_optionx=letopenOinletname=some_ofx.t.T.Option.json_namein`Stringnameandgen_listx=letopenLin`List(List.mapgen_objx.obj)andgen_aliasx=letopenAinmatchx.objwith|`aliasx->gen_aliasx|x->gen_objxlet_=Piqobj.to_json:=gen_obj