Closed dckc closed 7 years ago
This is an API error; go
only receives the first segment of the input, which is going to be incomplete JSON data. Buffering the entire file in memory yields a valid parse.
oops. thanks for debugging
win!
# import "lib/monte/mast" =~ [=> makeMASTContext :DeepFrozen]
import "lib/json" =~ [=> JSON :DeepFrozen]
import "lib/streams" =~ [=> flow :DeepFrozen]
import "lib/codec/utf8" =~ [=> UTF8 :DeepFrozen]
exports (main)
def main(_argv, => stdio) as DeepFrozen:
var buf := b``
object inputSink:
to run(more):
buf += more
trace(`got ${more.size()} bytes of input`)
to complete():
trace("complete")
def text := UTF8.decode(buf, throw)
def suite := JSON.decode(text, throw)
trace(`suite size: ${suite.size()}`)
to abort(problem):
throw(problem)
flow(stdio.stdin(), inputSink)
perhaps for a rosetta stone section or maybe even a library function, consider makeTextReader
:
# import "lib/monte/mast" =~ [=> makeMASTContext :DeepFrozen]
import "lib/json" =~ [=> JSON :DeepFrozen]
import "lib/streams" =~ [=> flow :DeepFrozen]
import "lib/codec/utf8" =~ [=> UTF8 :DeepFrozen]
exports (main)
def makeTextReader(source) as DeepFrozen:
var buf := b``
def [iouText, gotText] := Ref.promise()
object textReader:
to read():
return iouText
to run(more):
buf += more
to complete():
gotText.resolve(UTF8.decode(buf, throw))
to abort(problem):
throw(problem)
flow(source, textReader)
return textReader
def main(_argv, => stdio) as DeepFrozen:
when(def text := makeTextReader(stdio.stdin()).read()) ->
def suite := JSON.decode(text, throw)
trace(`suite size: ${suite.size()}`)
simpson suggests using makeSink.asList()
to avoid quadratic-time append... How's this?
# import "lib/monte/mast" =~ [=> makeMASTContext :DeepFrozen]
import "lib/json" =~ [=> JSON :DeepFrozen]
import "lib/streams" =~ [=> flow :DeepFrozen, => makeSink :DeepFrozen]
import "lib/codec/utf8" =~ [=> UTF8 :DeepFrozen]
exports (main)
def makeTextReader(source) as DeepFrozen:
def [chunkVow, collector] := makeSink.asList()
flow(source, collector)
return object textReader:
to read():
return when (def chunks := chunkVow) ->
UTF8.decode(b``.join(chunks), throw)
def main(_argv, => stdio) as DeepFrozen:
when (def text := makeTextReader(stdio.stdin()).read()) ->
def suite := JSON.decode(text, throw)
trace(`suite size: ${suite.size()}`)
One might ask for a smaller example, but
JSON.decode()
doesn't tell me where it thinks the error is, so it's hard to narrow it down.https://gist.github.com/dckc/3204a8bb40aa43a3b5843c744cb865ef
I get this stacktrace:
And this is the code I was trying to use to read the JSON data: