json - 如何从 Chapel 中的记录返回 JSON 字符串?

标签 json 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 将其发送到浏览器作为网络服务器。我如何让它在字符串周围加上引号并完成所有漂亮的 JSONy 事情?

最佳答案

目前,Chapel 的格式化 I/O 包括 JSON 支持。 (但请注意,将来可能会删除它以改进界面)。请参阅general conversions section of the formatted I/O documentation 。请注意,您可以使用 string.format , readf ,和writef访问格式化的 I/O。

在您的特定情况下,string.format可能就是您想要的 - 例如“%jt\n”.format(dto)

关于json - 如何从 Chapel 中的记录返回 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50609764/

相关文章:

recursion - Chapel 是否实现尾部调用优化?

java - 如何在Java中验证Instagram实时API x-hub-signature?

arrays - 在数据存储中的 JSON 类型字段中插入空数组

ios - 在 Swift 中创建类和结构来表示 JSON 对象

chapel - 使用 --fast 标志编译时,我遇到了一个我不确定的错误

chapel - 是否可以生成映射 Distribution 的 Locales 网格?

java - Jackson JsonParser 获取数组 token

javascript - jQuery 自动完成建议所有选项,无论输入条目如何

nvidia - 使用 Chapel 使用两个 Nvidia Jetson nano 开发工具包时遇到问题