JSON3.jl icon indicating copy to clipboard operation
JSON3.jl copied to clipboard

Reading JSON objects from streaming buffer

Open tk3369 opened this issue 7 months ago • 1 comments

Hi, I'm just looking for suggestions. I want to read JSON objects from an IO in a streaming fashion e.g. taking 1024 bytes at a time. That means I have 3 possible situations when parsing JSON objects from the data buffer:

  1. The buffer contains exactly one JSON object
  2. The buffer contains more than one JSON object
  3. The buffer contains a partial JSON object

When I use JSON3.read, it returns the first JSON object from the string. For case 2, I want to read the first object and shift the pointer so that I can call JSON3.read again to get the next object. l will repeat until I hit case 3 and by then I will trigger a read from the IO.

julia> s = """{"a":1,"b":2}{"a":3,"b":4}{"""
"{\"a\":1,\"b\":2}{\"a\":3,\"b\":4}{"

julia> JSON3.read(s)
JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 2 entries:
  :a => 1
  :b => 2

julia> length("""{"a":1,"b":2}""")
13

So, I think I just need a way to get the offset after reading an object (in the above example, the value is 14). Any suggestion?

tk3369 avatar Jun 08 '25 17:06 tk3369