Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OCaml:如何使用Yojson派生JSON记录,其中一个字段名是OCaml关键字?_Json_Parsing_Ocaml_Keyword - Fatal编程技术网

OCaml:如何使用Yojson派生JSON记录,其中一个字段名是OCaml关键字?

OCaml:如何使用Yojson派生JSON记录,其中一个字段名是OCaml关键字?,json,parsing,ocaml,keyword,Json,Parsing,Ocaml,Keyword,我正在尝试制作一个可以被visjs网络库接受的json 为此,我需要创建一个节点和边的数组。 虽然每个节点都包含一些字段,这些字段的名称可以安全地用作OCaml(id、标签等)中的记录字段,但边缘需要名为“to”的字段。不幸的是,这是OCaml中的一个关键字,所以我不能将其作为记录的名称 我正在使用ppx\u yojson\u conv将OCaml记录转换为yojson对象 下面是一些代码: type node = {id:int;label:string;shape:string;col

我正在尝试制作一个可以被visjs网络库接受的json

为此,我需要创建一个节点和边的数组。 虽然每个节点都包含一些字段,这些字段的名称可以安全地用作OCaml(id、标签等)中的记录字段,但边缘需要名为“to”的字段。不幸的是,这是OCaml中的一个关键字,所以我不能将其作为记录的名称

我正在使用
ppx\u yojson\u conv
将OCaml记录转换为
yojson
对象

下面是一些代码:

type node = {id:int;label:string;shape:string;color:string} (* this type is perfectly ok since it is exactly what visjs library accepts and OCaml accepts each of its fields' name *)
[@@deriving yojson_of]
type edge = {from:int;to:int;arrow:string} (* this type is what visjs accepts but OCaml does not allow to create field with the name "to" *)
[@@deriving yojson_of]


我是否可以创建一个OCaml类型,该类型可以通过
yojson
库轻松解析,而无需手动转换每个字段?

您可以在字段级别添加
[@key“your_arbitral_name”]

类型边缘={
from:int;
到:int[@key“to”];
箭头:字符串
}[@@r\u of]
正如这里提到的:

祝你好运