0

I am receiving a stream of JSON string from a hardware. It works most of the time. But when the data getting more and more congested, I will encounter situation like below:

  1. JSON string received is not yet complete

    { test: 'test1',

  2. Multiple JSON string received

    { test: 'test1', valid: true }{ test: 'test2', valid: true }{ test:

In the example above.

  • Case 1: I need to wait until the string is complete.
  • Case 2: I wish to be able to extract the completed 2 Json strings and parse separately

Keep in mind that the above are for illustrative purposes only. In real life string might include { or } and, the string might use single or double quote, and the Json string is much longer, might contains multiple objects inside it.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
s k
  • 4,342
  • 3
  • 42
  • 61
  • 1
    You can deserialize a sequence of JSON values in a stream by setting `JsonTextReader.SupportMultipleContent = true` as shown in [this answer](https://stackoverflow.com/a/29729419/3744182) by [Yuval Itzchakov](https://stackoverflow.com/users/1870803/yuval-itzchakov) to [Line delimited json serializing and de-serializing](https://stackoverflow.com/q/29729063/3744182). As far as the "not yet complete" JSON case, Json.NET should already be throwing an exception, which you can catch. Is that not currently happening? – dbc Jun 08 '22 at 03:31
  • so you need to validate string: https://stackoverflow.com/questions/14977848/how-to-make-sure-that-string-is-valid-json-using-json-net – Power Mouse Jun 08 '22 at 18:37
  • What have you tried so far? Where did you get stuck? – Peter Csala Jun 09 '22 at 08:00
  • Take a look at [How to validate JSON Data before you import it into a database.](https://www.red-gate.com/simple-talk/blogs/how-to-validate-json-data-before-you-import-it-into-a-database/) – Eskandar Abedini Jun 09 '22 at 08:10
  • 1
    @PeterCsala I am evaluating a few solutions. The incomplete stream (Json string) will eventually be completed. Even if I have retrieved part of the completed Json, I wish the incomplete Json can be retrieved in subsequent call. Will give an update once I found a good solution. – s k Jun 09 '22 at 08:24
  • If you can control the format of the stream, you might consider using [message framing](https://stackoverflow.com/q/7257139). – dbc Jun 09 '22 at 17:16

0 Answers0