Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected quotations when dumping #320

Closed
lbauret opened this issue Jan 18, 2017 · 4 comments
Closed

Unexpected quotations when dumping #320

lbauret opened this issue Jan 18, 2017 · 4 comments

Comments

@lbauret
Copy link

lbauret commented Jan 18, 2017

When dumping the following JSON, it seems there are some specific situations where strings are not held between quotation marks. For example when a certain string starts with 0, or "08" and "09" strings. Also, strings are not properly indented when containing \t or \r.

Input:

{
  "start-with-0" : {
    "fails": "015678",
    "ok": "115678"
  },
  "number-list" : [
    "01",
    "02",
    "03",
    "04",
    "05",
    "06",
    "07",
    "08",
    "09",
    "10",
    "11"
  ],
  "white-spaces": {
    "string1": "**Hello World**\n\n**Sample Text.** \t End of text.",
    "string2": "**Hello World**\n\n**Sample Text.** \r End of text.",
    "string3": "**Hello World**\n\n**Sample Text.**  End of text.",
  }
}

Output:

start-with-0:
  fails: 015678
  ok: '115678'
number-list:
  - '01'
  - '02'
  - '03'
  - '04'
  - '05'
  - '06'
  - '07'
  - 08
  - 09
  - '10'
  - '11'
white-spaces:
  string1: "**Hello World**\n\n**Sample Text.** \t End of text."
  string2: "**Hello World**\n\n**Sample Text.** \r End of text."
  string3: |-
    **Hello World**

    **Sample Text.**  End of text.
@puzrin
Copy link
Member

puzrin commented Jan 19, 2017

Did you tried online demo with your sample?

Dumper keeps minimal possible quoting, and tries to beautify strings when possible for readability. As far as i see, loading dumed yaml (from your message) is equal to original. Did i missed something?

@aepsilon
Copy link
Contributor

aepsilon commented Jan 19, 2017

As the author of the current scalar dumper, I hope this helps:

  • Numbers beginning with 0 are octals under YAML 1.1. By default the dumper tries to remain backward-compatible, so it enquotes strings that could be misinterpreted. In your example the strings that aren't in quotes are exactly the ones that aren't valid octals: 015678, 08, and 09 contain the digits 8 and 9. Cf. http://yaml.org/type/int.html. It looks like Ruby's YAML module does something similar: https://stackoverflow.com/questions/33097353.

  • Escaping tabs was a simplification to avoid potential problems like issue 80. It may have been overly cautious though. I could try supporting literal tabs if there's demand. (For future reference, some changes would be needed in block indent indicators and line folding.)

  • The primary goal of the dumper is to preserve content. To preserve a carriage return \r in YAML, it has to be escaped, which necessitates double-quoted style. Otherwise it will be normalized into a \n the next time it's parsed.

    Line breaks inside scalar content must be normalized by the YAML processor. Each such line break must be parsed into a single line feed character.
    http://yaml.org/spec/1.2/spec.html#id2774608

    You could use something like stringvalue.replace(/\r\n?/g, '\n') to normalize each string first if you would rather have a nicely-formatted output and don't mind losing the distinction between \r, \r\n, and \n.

@lbauret
Copy link
Author

lbauret commented Jan 19, 2017

Thank you very much for the quick response.
About the octal numbers, you said:

By default the dumper tries to remain backward-compatible, so it enquotes strings that could be misinterpreted.

Is there a way to avoid this default behavior then? If there is not, would you consider this to be a bug?

@puzrin
Copy link
Member

puzrin commented Jan 19, 2017

I don't see enougth added value to change current behaviour.

@puzrin puzrin closed this as completed Jan 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants