0

I am used to using JSON to simply store string values and everything just works. Now I run into situation where I can't use JSON and need to have a set of strings stored in an array buffer. Wondering what the practices are to delimit the strings to handle the edge cases such as:

  • If the delimiter is double quotes "string...", what if there are quotes inside the string "str"ing...".
  • If the delimiter is a random sequence of characters such as --MYDELIMITER--, there is some chance that it will result in a false match (i.e. some string actually contains that sequence).

The array I am thinking would look like this as JSON:

["123", "foo\"", "bar", "ba\"\"z"]

So I'm confused how to do that without JSON:

"123""foo\"""bar""ba\"\"z"

In reality what I would like to do is separate arbitrary values such as booleans, JSON dates, strings, and numbers in a byte array.

Lance
  • 75,200
  • 93
  • 289
  • 503

1 Answers1

1

Can you JSON.stringify() an array of strings and write it to the buffer? This may be a good solution if you don't need consume the data as a stream on the receiving end, and instead Json.parse it at the end.

Arguably any UNICODE character can exist in the data, in the form of hexadecimal notation. You can create (practically) unique delimiter but this will make your data a lot bigger.

One solution which will work in any case is to create a Uint16Array, which stores the length of the first string at index 0, and then [length] items containing the string.charCodeAt(position) at each position, then the length of the second string and all the charcodes on the subsequent [length] items, etc. Basically a Pascal string implementation.

Daniel Alexandrov
  • 1,299
  • 8
  • 13
  • Oh wow that Pascal string idea is nice. The link says it only stores 1 unit of length information, so does this mean if the string is larger than 65,536 it won't work? – Lance Apr 22 '18 at 21:00
  • The JSON idea could work, but hoping to avoid using JSON and instead doing it from scratch. – Lance Apr 22 '18 at 21:01
  • Yes but you can also use Uint32Array and store 2 chars in every position, so you won't waste space. This will give you 4 times bigger limit. – Daniel Alexandrov Apr 22 '18 at 21:08