5

JSON with multiline strings

 1 year ago
source link: https://dev.to/polyseam/json-with-multiline-strings-4645
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JSON with multiline strings

One of the most impactful features to be added to ECMAScript in the last few years is template literals. Creating multiline strings before the template literal syntax was done with \n escape codes.

const exampleObject = {
  exampleText: 'this text appears on line 0\nthis text appears on line 1'
}

console.log(exampleObject.exampleText)
// this text appears on line 0
// this text appears on line 1

This was not only a pain to write, but also a pain to read - especially when a string contains many line breaks. But with modern JS we can do the following instead:

const exampleObject = {
  exampleText: `this text appears on line 0
this text appears on line 1`
}

console.log(exampleObject.exampleText)
// this text appears on line 0
// this text appears on line 1

Note that the output of either method is the same. We can declare a JS Object that contains a string property using template strings with backticks, or we can do it with the \n escape code.

However, this flexibility is not available in JSON. We are able to write these multiline strings in JS, but in JSON the same content would cause a parsing error because JSON doesn't know how to handle strings wrapped with backticks.

But this isn't the first time there was a missing feature in JSON! It is also unable to handle a document if it contains a comment:

{
  "org": "polyseam" // this comment would cause JSON.parse to fail
}

The above document is invalid JSON, despite it being perfectly valid JavaScript. Eventually, someone decided comments are so useful that a new specification and parser were created to allow us to put them in documents.

That spec is called JSONC. Note how the comment is now correctly parsed as such:

{
  "org": "polyseam" // this comment is properly ignored
}

Now we know multiline strings are a missing feature of JSON that is quite valuable, and we know that we can create new specifications and parsers when it is worth while... I propose we create just one last variant of JSON:

{
  "org": "polyseam",
  "exampleText": `this text appears on line 0
this text appears on line 1`
}

By extending JSON in this way we have so much more flexibility in the way we read and write documents with JSON, which has impacts from the browser, to the server, to Kubernetes manifests, and beyond. We also provide a feature that 'just works' the way a JavaScript developer might imagine it should.

Do you think that JSONCT is a good idea? Does it already exist? Do you have a better name for it?

We'd love to hear from you!

github.com/polyseam/jsonct


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK