Skip to content

Add end() to emit a number at the end of the input - #51

Open
joszamama wants to merge 1 commit into
creationix:masterfrom
joszamama:fix/number-at-end
Open

joszamama wants to merge 1 commit into
creationix:masterfrom
joszamama:fix/number-at-end

Conversation

@joszamama

Copy link
Copy Markdown

Fixes #50.

A number is complete only when a byte that cannot be part of it arrives, so a top-level number at the very end of the input was never emitted, and the parser had no way to know that the input had ended.

This adds Parser.prototype.end(), to call when there is no more input:

  • If a number is pending, end() writes one space, so the number is emitted by the same code in write() as before (token, offset and number reviver).
  • If the input stops inside a string, literal, array or object, end() calls onError with "Unexpected end of input".
  • Empty input, or input that ends between top-level values, gives no error.

Wrappers such as JSONStream can call it from their stream end handler.

Not changed here: end() after a lone - hits the ReferenceError in numberReviver that #48 fixes.

Testing

  • New test/end.js: '1' then end() emits 1; '[1' then end() reports "Unexpected end of input".
  • npm test: 129/129 before, 132/132 after. Without the change to jsonparse.js, test/end.js fails (p.end is not a function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A top-level number at the end of the input is never emitted (no way to signal end of input)

1 participant