diff --git a/json_serialization/lexer.nim b/json_serialization/lexer.nim index 785ef4d..09af92d 100644 --- a/json_serialization/lexer.nim +++ b/json_serialization/lexer.nim @@ -41,7 +41,7 @@ type errNonPortableInt = "number is outside the range of portable values" JsonLexer* = object - stream: InputStream + stream*: InputStream mode*: JsonMode line*: int @@ -61,6 +61,23 @@ const 1e20, 1e21, 1e22] # TODO: this table should be much larger # The largest JSON number value is 1E308 +proc renderTok*(s: JsonLexer): string = + case s.tok + of tkError, tkEof: "" + of tkString: "" + of tkInt: "" + of tkNegativeInt: "" + of tkFloat: "" + of tkTrue: "true" + of tkFalse: "false" + of tkNull: "null" + of tkCurlyLe: "{" + of tkCurlyRi: "}" + of tkBracketLe: "[" + of tkBracketRi: "]" + of tkColon: ":" + of tkComma: "," + template peek(s: InputStream): char = char inputs.peek(s) diff --git a/json_serialization/reader.nim b/json_serialization/reader.nim index 31dd800..87adbda 100644 --- a/json_serialization/reader.nim +++ b/json_serialization/reader.nim @@ -11,7 +11,7 @@ export type JsonReader* = object lexer*: JsonLexer - forwardCompatible: bool + allowUnknownFields: bool JsonReaderError* = object of JsonError line*, col*: int @@ -120,8 +120,8 @@ proc handleReadException*(r: JsonReader, proc init*(T: type JsonReader, stream: InputStream, mode = defaultJsonMode, - forwardCompatible = false): T = - result.forwardCompatible = forwardCompatible + allowUnknownFields = false): T = + result.allowUnknownFields = allowUnknownFields result.lexer = JsonLexer.init(stream, mode) result.lexer.next() @@ -177,13 +177,24 @@ proc skipSingleJsValue(r: var JsonReader) = of tkString, tkInt, tkNegativeInt, tkFloat, tkTrue, tkFalse, tkNull: r.lexer.next() +proc captureSingleJsValue(r: var JsonReader, value: var string) = + value = $r.lexer.tok + let pos = r.lexer.stream.createRewindPoint() + echo "skipiing ", pos + r.skipSingleJsValue() + let finalPos = r.lexer.stream.pos + echo "done ", finalPos + echo "Rewiding ", finalPos - pos + r.lexer.stream.rewindTo pos + value.setLen(finalPos - pos) + doAssert r.lexer.stream.readInto(value) + proc allocPtr[T](p: var ptr T) = p = create(T) proc allocPtr[T](p: var ref T) = p = new(T) - iterator readArray*(r: var JsonReader, ElemType: typedesc): ElemType = mixin readValue @@ -240,12 +251,14 @@ proc readValue*[T](r: var JsonReader, value: var T) r.requireToken tkString value = r.lexer.strVal r.lexer.next() + elif value is seq[char]: r.requireToken tkString value.setLen(r.lexer.strVal.len) for i in 0..") +