我如何 return 来自教堂记录的 JSON 字符串?
How do I return a JSON string from a Record in Chapel?
我有一些 DTO 记录定义为
record NodeDTO{
var id: int,
name: string,
community: int;
}
record LinkDTO {
var source: int,
target: int,
strength: real;
}
record GraphDTO {
var nodes:[1..0] NodeDTO,
links:[1..0] LinkDTO;
proc init() {}
}
proc Graph.DTO() {
var dto = new GraphDTO();
for key in this.verts.keys {
dto.nodes.push_back(new NodeDTO(id=this.verts.get(key), name=key, community=1));
for nbs in this.neighbors(key).keys {
dto.links.push_back(new LinkDTO(source=this.verts.get(key), target=this.verts.get(nbs), strength=1.0));
}
}
return dto;
}
但是当我 writeln(dto)
我得到
(nodes = (id = 7, name = yondu, community = 1) (id = 1, name = star lord, community = 1) (id = 5, name = rocket, community =
1) (id = 4, name = drax, community = 1) (id = 8, name = nebula, community = 1) (id = 3, name = groot, community = 1) (id = 2, name = gamora,
community = 1) (id = 6, name = mantis, community = 1), links = (source = 7, target = 8, strength = 1.0) (source = 1, target = 4, strength = 1.
0) (source = 1, target = 3, strength = 1.0) (source = 1, target = 2, strength = 1.0) (source = 5, target = 6, strength = 1.0) (source = 4, tar
get = 5, strength = 1.0) (source = 3, target = 4, strength = 1.0) (source = 2, target = 4, strength = 1.0) (source = 6, target = 7, strength =
1.0) (source = 6, target = 8, strength = 1.0))
这不会正确解释为 JSON。我使用 Chrest 作为 Web 服务器将其发送到浏览器。我如何让它在字符串周围加上引号并做所有好的 JSONy 事情?
目前,Chapel 的格式化 I/O 包括 JSON 支持。 (请注意,尽管将来可能会被删除以改进界面)。请参阅 general conversions section of the formatted I/O documentation. Note that you can use string.format, readf, and writef 以访问格式化的 I/O。
在您的特定情况下,string.format
可能是您想要的 - 例如"%jt\n".format(dto)
.
我有一些 DTO 记录定义为
record NodeDTO{
var id: int,
name: string,
community: int;
}
record LinkDTO {
var source: int,
target: int,
strength: real;
}
record GraphDTO {
var nodes:[1..0] NodeDTO,
links:[1..0] LinkDTO;
proc init() {}
}
proc Graph.DTO() {
var dto = new GraphDTO();
for key in this.verts.keys {
dto.nodes.push_back(new NodeDTO(id=this.verts.get(key), name=key, community=1));
for nbs in this.neighbors(key).keys {
dto.links.push_back(new LinkDTO(source=this.verts.get(key), target=this.verts.get(nbs), strength=1.0));
}
}
return dto;
}
但是当我 writeln(dto)
我得到
(nodes = (id = 7, name = yondu, community = 1) (id = 1, name = star lord, community = 1) (id = 5, name = rocket, community =
1) (id = 4, name = drax, community = 1) (id = 8, name = nebula, community = 1) (id = 3, name = groot, community = 1) (id = 2, name = gamora,
community = 1) (id = 6, name = mantis, community = 1), links = (source = 7, target = 8, strength = 1.0) (source = 1, target = 4, strength = 1.
0) (source = 1, target = 3, strength = 1.0) (source = 1, target = 2, strength = 1.0) (source = 5, target = 6, strength = 1.0) (source = 4, tar
get = 5, strength = 1.0) (source = 3, target = 4, strength = 1.0) (source = 2, target = 4, strength = 1.0) (source = 6, target = 7, strength =
1.0) (source = 6, target = 8, strength = 1.0))
这不会正确解释为 JSON。我使用 Chrest 作为 Web 服务器将其发送到浏览器。我如何让它在字符串周围加上引号并做所有好的 JSONy 事情?
目前,Chapel 的格式化 I/O 包括 JSON 支持。 (请注意,尽管将来可能会被删除以改进界面)。请参阅 general conversions section of the formatted I/O documentation. Note that you can use string.format, readf, and writef 以访问格式化的 I/O。
在您的特定情况下,string.format
可能是您想要的 - 例如"%jt\n".format(dto)
.