-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
Improve JSON compatibility #189
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As replacer & options have different shapes, YAML.stringify and the Document constructor can still accept the options as the second argument. The behaviour of the replacer should match the JSON.stringify equivalent exactly for the values both support. Map is treated equivalently to an Object; others act like arrays.
BREAKING CHANGE: The `toJSON()` utility exported by 'yaml/util' is renamed as `toJS()`.
This effectively implements this ECMA-262 proposal: https://github.com/tc39/proposal-well-formed-stringify
epic! found my way here by looking for a |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The main intent here is to have the signatures and behaviour of
YAML.parse()
andYAML.stringify()
more closely model that ofJSON.parse()
andJSON.stringify()
. Mostly this is a matter of adding new features, but some breaking changes are also required.YAML.parse(str, reviver?, options?)
Passing a function as a second arg will treat it as a reviver. Implementation follows the JSON spec, extending it naturally for Set and Map, which may be returned from
!!set
and!!omap
collections, or by enabling themapAsMap
option. CallingYAML.parse(str, options)
is still supported.YAML.stringify(value, replacer?, options?)
Passing a function as a second arg will treat it as a replacer. Implementation follows the JSON spec, extending it naturally for Set and Map. Calling
YAML.stringify(value, options)
is still supported. For furtherJSON.stringify
compatibility, using a number or string as the thirdoptions
value will set theindent
option accordingly.Document#toJS({ mapAsMap, onAnchor, reviver })
Split from
toJSON()
, this is now the preferred way of getting "native" JavaScript output from a Document.mapAsMap
overrides document & default settings,onAnchor
is a callback for each aliased anchor in the document, andreviver
is as inYAML.parse()
.Document#toJSON()
Now always returns JSON-compatible output, which is a breaking change from the previous. Thanks to the split, the
keepBlobsInJSON
option is dropped.import { toJS } from 'yaml/util'
This exposed utility function is renamed from
toJSON
totoJS
, while keeping its signature and behaviour the same.Well-formed YAML.stringify
Implements the ECMA-262 Stage 4 proposal. Always stringify scalars that include unpaired surrogate code points as double-quoted, and represent them with
\u
escape sequences.Conceptually, the reviver and replacer that are added take place between the YAML Document and the native JS representations of the document. To put that another way, nothing changes here for
YAML.parseDocument()
orDocument#toString()
.The extension of the JSON spec of reviver and replacer to also cover Map and Set means that the
key
that's used in the callbacks is not always a string. For Set, we follow the pattern used by its iteration methods, and use the same value for bothkey
andvalue
. It's also good to note that as YAML supports collections as keys, the callbacks will also get called with their values, as appropriate.With this PR, the codebase will also start to include some previously-avoided methods like
Array.from
andObject.values
, which are not supported in all the environments thatyaml@1
supported without any further transpilation. It's not quite clear yet just what the new minimum will be.