0

I have a serial port which I'm monitoring and occasionally JSON objects are published to it. The objects are not prepended with any kind of frame, so I simply need to catch when they do show.

There is some prior art at https://stackoverflow.com/a/3845829, but 1) it presumes that the string starts and ends with a correct JSON object, which isn't the case for monitoring an arbitrary serial port datastream; and 2) there's no direct C++ examples.

Furthermore, when trying with some arbitrary strings with embedded JSON Objects, such as {{"a": "a", "b": "b", "c": "c", "d": [3, 2, 3]}, it fails to correctly find the object.

What is the correct approach for this? Regex? Something else?

Kenn Sebesta
  • 7,485
  • 1
  • 19
  • 21
  • Is the json always an object? Not an array? Does your data contain `{` outside the JSON data? Could you just search for `{` and attempt parsing from that position? – Alan Birtles Apr 18 '23 at 12:56
  • @AlanBirtles that's what I'm doing right now, but, man, does that feel brittle. Unfortunately, I have limited ability to predict what will flow across the serial terminal, nor what the developers will decide they want to emit. I'm monitoring the stream for JSON objects which are to be fed to my UI, but it's really just ASCII text streaming with a VT100-ish interface. It's not how I would have chosen to do telemetry and user interaction, but it's not my project so I defer to the designers in what they want to do. – Kenn Sebesta Apr 18 '23 at 13:36
  • That being said, I think I can provide guidelines to ensure that `{` doesn't show up except in JSON objects. That should make a regex be saner. – Kenn Sebesta Apr 18 '23 at 13:38

0 Answers0