What is the practical different between the usage of JSON and YAML in Swagger?

妖精的绣舞 提交于 2020-01-15 01:21:52

问题


It appears that JSON includes the path information and http request verb, whereas YAML seems to just definition a tree structure alone.

What is the difference between them? Or am I mixing up different concepts/hierarchies here? newbie to swagger, just started learning.

If YAML is a superset of JSON what specifically is the superset adding here - is it URL paths and HTTP verbs ? Is adding an example also something that YAML adds to JSON for Swagger ?


回答1:


According to the OpenAPI Specification,

An OpenAPI document that conforms to the OpenAPI Specification is itself a JSON object, which may be represented either in JSON or YAML format.

So feature-wise, there is no difference between using JSON or YAML. What YAML as a superset of JSON adds here is merely a different syntax. Using an example from the specification, these two documents are identical:

{
  "servers": [
    {
      "url": "https://development.gigantic-server.com/v1",
      "description": "Development server"
    },
    {
      "url": "https://staging.gigantic-server.com/v1",
      "description": "Staging server"
    },
    {
      "url": "https://api.gigantic-server.com/v1",
      "description": "Production server"
    }
  ]
}

and

servers:
- url: https://development.gigantic-server.com/v1
  description: Development server
- url: https://staging.gigantic-server.com/v1
  description: Staging server
- url: https://api.gigantic-server.com/v1
  description: Production server

The first document is valid JSON and valid YAML (since YAML is a superset of JSON). The second document is valid YAML and structurally identical to the first document.

So to answer the question about what the YAML superset adds here: It adds different syntax for specifying the same structures. Nothing more.

YAML does include some features that are not mappable to JSON, but they are irrelevant here because Swagger/OpenAPI doesn't use them.




回答2:


YAML is neater, easier to read and write than JSON if you look at it from another angle.



来源:https://stackoverflow.com/questions/46990638/what-is-the-practical-different-between-the-usage-of-json-and-yaml-in-swagger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!