JSON 的 SWI-Prolog 语法

SWI-Prolog syntax for JSON

json([ name='Demo term',
       created=json([day= @null, month='December', year=2007]),
       confirmed= @true,
       members=[1, 2, 3]
     ])

我正在盯着 swi prolog 手册看,但不理解其中的一些语法 (https://linkedpolitics.project.cwi.nl/swish/pldoc/man?section=jsonsupport),而且我无法找到合适的搜索词。 这是什么意思? name='Demo term', 还有这个? [day= @null 还有这个? members=[1, 2, 3] 这些是特殊语法还是奇怪的原子?

这个语法真的没有什么特别之处:

json/1 是一个复合词,只有列表作为参数。

该列表有条目

  • name='Demo term'
  • created=json([day= @null, month='December', year=2007])
  • confirmed= @true
  • members=[1, 2, 3]

显然是在尝试模拟地图关键字 -> 值:

  • 关键字 name 与原子 'Demo term' 相关联(在单引号中,因为它包含 space 并以大写字母开头
  • 关键字 created 与复合词 json([day= @null, month='December', year=2007])
  • 相关联
  • 关键字confirmed与复合词@true相关联,最好写成@(true).
  • 关键字member与列表[1,2,3]相关联

注意 https://eu.swi-prolog.org/pldoc/man?section=jsonsupport

中的描述

The JSON constants true and false are mapped -like JPL- to @(true) and @(false). The JSON constant null is mapped to the Prolog term @(null)

虽然得写

X='@'(true).

所以这个例子可能是假的。

您可以在 SWI-Prolog 的命令行中执行此操作以打印术语 规范模式(为可读性添加了换行符;我认为 write_canonical 也应该正确缩进,但遗憾的是它没有):

?- write_canonical(json([ name='Demo term',
|           created=json([day= '@'(null), month='December', year=2007]),
|           confirmed= '@'(true),
|           members=[1, 2, 3]
|         ])).
json([=(name,'Demo term'),
      =(created,json(
                  [=(day,@(null)),
                   =(month,'December'),
                   =(year,2007)]
                )
       ),
      =(confirmed,@(true)),
      =(members,[1,2,3])])
true.