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

YAML decodes malformed JSON Files with Newline Characters as Valid YAML #1065

Closed
viveksahu26 opened this issue Jan 9, 2025 · 4 comments
Closed

Comments

@viveksahu26
Copy link

Description

I encountered an issue where a file in JSON format, which contained a content issue (newline character splitting a string), could not be decoded by a JSON parser but was successfully decoded by the YAML parser. This behavior caused the file to be incorrectly identified as YAML, even though it was intended to be JSON.

Steps to Reproduce:

  1. Create a file with the following content (malformed JSON with a newline character in a string):

    {
        "creators": ["Person: Yukihiro
        Kawada"],
        "spdxVersion": "SPDX-2.2"
    }
  2. Attempt to decode this file:

    • Using a JSON parser: This will fail with a syntax error (due to the newline character breaking the JSON string).
    • Using the YAML parser: This will succeed, interpreting the content as valid YAML.
  3. The YAML parser decodes the file into the following structure:

    creators:
      - "Person: Yukihiro Kawada"
    spdxVersion: "SPDX-2.2"

Expected Behavior:

The YAML parser should either:

  1. Recognize that the file is malformed and fail to decode it.
  2. Provide a mechanism to differentiate between valid YAML and JSON masquerading as YAML.

Actual Behavior:

The YAML parser successfully decodes the malformed JSON, causing the file to be misclassified as YAML. In actually, the feeded file is JSON format.

Extra resource:

file:
tt.json

@perlpunk
Copy link

perlpunk commented Jan 9, 2025

go-yaml is correct here. YAML is a superset of JSON: https://yaml.org/spec/1.2.2/#12-yaml-history
Its so called "flow style" basically works like JSON, but allows more things.

@viveksahu26
Copy link
Author

Thanks for your response!!
Hey @perlpunk So, it's a fine if a JSON format file is considered as a YAML format, right ?

@perlpunk
Copy link

perlpunk commented Jan 9, 2025

So, it's a fine if a JSON format file is considered as a YAML format, right ?

yes

@viveksahu26
Copy link
Author

closing it, as discussion completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants