代码之家  ›  专栏  ›  技术社区  ›  Roman A. Taycher

记录JSON结构的语法

  •  64
  • Roman A. Taycher  · 技术社区  · 14 年前

    因此,我试图记录我正在编写的API返回的JSON的格式,我想知道JSON结构文档中是否有流行的格式。

    注意,我不想测试或验证任何东西,我只是将它用于文档。另外,一些向非常量添加注释的方法(总是返回值相同的项)也不错。

    这是我目前正在使用的一个不完全经过深思熟虑的方案:

    Plain names refer to identifiers or types.
    Some types have type-comment
    Strings that appear to be constant(always returned for that type of request) strings are "str"
    Constant Numbers would be just the number
    Constant null is null
    Booleans are true/false for constant booleans or Boolean otherwise
    [a,b,c] are lists with 3 items a,b,c
    [...  ...] is a list of repeating elements of some types/constants/patterns
    {a:A,b:B,c:c} and {... ...}  is the same for a dictionary.
    

    例子:

    story          := [header,footer]
    header         := {"data":realHeader,"kind":"Listing"}
    realHeader     := {"after": null, "before": null, "children": [{"data": realRealHeader, "kind": "t3"}], "modhash": ""}
    footer         := {"data":AlmostComments,"kind":"Listing"}
    AlmostComments := {"data": {"after": null, "before": null, "children": comments, "modhash": ""}, "kind": "t1"}
    comments       := [...{"data":comment, "kind":"t1"}...]
    
    realRealHeader :=
    {"author": string,
    "clicked": boolean,
    "created": int,
    "created_utc": int,
    "domain": "code.reddit.com",
    "downs": int,
    "hidden": boolean,
    "id": string-id,
    "is_self": boolean,
    "levenshtein": null,
    "likes": null,
    "media": null,
    "media_embed": { },
    "name": string-id,
    "num_comments": int,
    "over_18": false,
    "permalink": string-urlLinkToStoryStartingFrom/r,
    "saved": false,
    "score": int,
    "selftext": string,
    "selftext_html": string-html,
    "subreddit": string-subredditname,
    "subreddit_id": string-id,
    "thumbnail": "",
    "title": string,
    "ups": int,
    "url": "http://code.reddit.com/"
    }
    
    
    comments := {
    "author": string,
    "body": string-body_html-wout-html,
    "body_html": string-html-formated,
    "created": int,
    "created_utc": int,
    "downs": int,
    "id": string-id,
    "levenshtein": null,
    "likes": null,
    "link_id": string-id,
    "name": string-id",
    "parent_id": string-id,
    "replies": AlmostComments or null,
    "subreddit": string-subredditname,
    "subreddit_id": string-id,
    "ups": int
    }
    
    6 回复  |  直到 12 年前
        1
  •  30
  •   StaxMan    14 年前

    理论上 JSON Schema 可以达到这个目的,但在实践中,我不确定它能做到。我希望值得一提。

    除此之外,我个人的观点是,由于JSON主要用于传输对象,在语言客户端使用文档(Java、C语言、各种脚本语言)中记录等价对象可能是最有意义的——毕竟,这些对象通常被映射/绑定到JSON和JOBE。然后,您可以使用任何可用的文档工具,如Java的JavaDoc(Perl DOC for Perl、C++的C++等)。

    对于指定接口,还有 WADL (Web应用程序描述语言),这可能会有所帮助。

        2
  •  13
  •   Carlos Oliveira    12 年前

    如何从JSON生成HTML文档:

    您需要生成 Json Schema ,您可以粘贴原始JSON并自动生成模式:

    http://www.jsonschema.net/

    有了模式,您可以使用matic自动生成HTML文档。

    https://github.com/mattyod/matic

    生成HTML

    要安装matic,需要安装node.js: http://nodejs.org/

    在Windows上,运行命令

    安装运行此命令的jade: npm install -g jade

    从github打开下载的matic文件夹: cd PATH_TO_FOLDER/matic

    运行安装命令: npm install -g

    下载文档示例项目: https://github.com/mattyod/matic-simple-example

    将您的模式放在“模式”文件夹中

    打开项目文件夹: cd PATH_TO_PROJECT_FOLDER

    运行命令: matic

    您应该看到一条成功消息: Documentation built to ./web/

        3
  •  7
  •   Iterator    13 年前

    我不确定您为什么要记录JSON,我可以猜您试图找到一种一致的方法来告诉IDE或开发人员您的符号上的数据类型。

    jsdoc(http://jsdoc.sourceforge.net/用法)可能是您需要的。

    例如:

    {
       /**
         * Name of author
         * @type String
         */
       "author": null, 
       /**
         * has the author been clicked
         * @type Boolean
         */
       "clicked": null, 
       /**
         * Unix Timestamp of the creation date
         * @type Int
         */
       "created": null
    }
    

    或者,如果您试图演示数据的结构。你可以看看yaml(http://www.yaml.org/),它被设计成一种人类可读的串行化格式,也许更适合记录你的数据结构。

    一个简单的例子:

    Author:
      name: String
      clicked: Boolean
      created: Integer
    
        4
  •  5
  •   Eamonn O'Brien-Strain    13 年前

    对于每个JSON块只有一到两层深度的简单API,通过显示示例来记录似乎是常见的做法。

    但是对于像您这样更复杂的数据模型,我没有看到任何好的解决方案。有一些JSON模式建议,但是这似乎违背了JSON的精神,而且对于您仅仅记录的目的来说似乎太重了。

    我个人认为你的计划很好。通过一些小的扩展来处理可选的和可选的部分,我认为它可能和巴科斯-诺尔形式一样具有表现力,非常容易阅读和理解,并且符合JSON的精神。也许我们可以在其他人之后获得一些动力来使用这种“泰瑟JSON语法形式”(TJGF)!

        5
  •  3
  •   Josh Schumacher    14 年前

    您可以编写一个JSON响应示例,然后使用markdown和 Docco . Docco输出易于遵循的基于HTML的文档。

        6
  •  3
  •   Sebastien Lorber    12 年前

    在您的案例中,它可能没有用处,因为您似乎没有构建API。

    但是如果是这样的话,你使用的是Java或JVM(JAX-RS),你就可以大摇大摆了。

    它允许以JSON表示(如WSDL/WADL)来描述您的API。 它们提供了一个IHM层,可以读取API的JSON表示。 以下是您将得到的: http://petstore.swagger.wordnik.com/

    https://developers.helloreverb.com/swagger/