Native-messaging API JSON white space

In the native-messaging API, where it reads in the documentation that

Each message is serialized using JSON, UTF-8 encoded and is preceded with a 32-bit value containing the message length in native byte order.

is it guaranteed that the JSON string passed from the browser to the native application will never have white space?

The native application is coded in C which doesn’t have an equivalent of JSON parse and, likely, the SQLite3 JSON1 extension will handle the JSON operations needed for storing/updating data but I recently learned that they work only within the SQLite3 APIs and are not available to the general C code. I’d like to place a property at the beginning of the JSON string indicating the requested operation in C and would like to know if I can count on it always being in the same position.

When I test this by writing the message recieved from the browser to a text file on the local disk, there are no white spaces. Will that always be the case and/or can I do something within the extension to ensure that the JSON string will either always have or not have white space around the delimiters?

Otherwise, I suppose I could just read each character starting from the beginning until I read the property and then its value, ignoring all white spaces.

Thank you.

I would assume that it doesn’t tell the JSON serializer to “pretty” format the JSON, however that is likely an implementation detail. What you could do is your own string based format, so send a string message that you then have C code to decode. The JSON serialization of a string is always "{string}", so all your “json decoder” would have to do is find the first and last " and everything between is whatever message you sent from the extension.

Thank you. I won’t rely on the white space being absent or present. I’ve for the most part done as you suggested.

There are some JSON extensions for C but I doubt all the extra code is worth it since I’m controlling both sides of the communication and can put the “instructions” in the front of the message informing the C code what the rest of the message is about.

1 Like