YAML as a JSON superset and TAB characters

前端 未结 3 816
花落未央
花落未央 2021-02-14 04:20

I am unable to find a reference to this error exactly, but YAML 1.2 says it\'s a JSON superset, and if I use tab characters in a JSON it treats it like an error.

e.g.

3条回答
  •  悲&欢浪女
    2021-02-14 04:32

    Tabs ARE allowed in YAML, but only where indentation does not apply.

    According to YAML 1.2 Section 5.5:

    YAML recognizes two white space characters: space and tab.

    The following examples will use · to denote spaces and to denote tabs. All examples can be validated using the official YAML Reference Parser.

    YAML has a block style and flow style. In block style, indentation determines the structure of a document. The following document uses block style.

    root:
    ··key: value
    

    Validate

    In flow style, special characters indicate the structure of the document. The following equivalent document uses flow style.

    {
    → root: {
    → → key: value
    → }
    }
    

    Validate

    You can even mix indentation in flow style.

    {
    → root: {
    ··→ key: value
    ····}
    }
    

    Validate

    If you're mixing block and flow style, the entire flow style part must respect the block style indentation.

    root:
    ··{
    ····key: value
    ··}
    

    Validate

    But you can still mix your indentation within the flow style part.

    root:
    ··{
    ··→ key: value
    ··}
    

    Validate

    If you have a single value document, you can surround the value with all manner of whitespace.

    → ··value··→ 
    

    Validate

    The point is, every JSON document that is parsed as YAML will put the document into flow style (because of the initial { or [ character) which supports tabs, unless it is a single value JSON document, in which case YAML still allows padding with whitespace.

    If a YAML parser throws because of tabs in a JSON document, then it is not a valid parser.

    That being said, your example is failing because a block style mapping value must always be indented if it's not on the same line as the mapping name.

    root: {
    ··key: value
    }
    

    is not valid, however

    root:
    ··{
    ····key: value
    ··}
    

    is valid, and

    root: { key: value }
    

    is also valid.

提交回复
热议问题