JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
79 stars 15 forks source link

parsing JSON.Object with trailing new line produces invalid output #13

Closed achille-roussel closed 2 years ago

achille-roussel commented 2 years ago

Hello,

I'm reporting what appears to be a bug when parsing inputs with trailing new lines as a JSON.Object.

I am using json-as in version 0.2.6

This is the behavior I am seeing when there are no trailing new lines:

diff --git a/assembly/test.ts b/assembly/test.ts
index 74d817b..caac9be 100644
--- a/assembly/test.ts
+++ b/assembly/test.ts
@@ -189,7 +189,7 @@ check<EmptySchema>('Encode/Decode object', emptyObj)
 //console.log(JSON.stringify(JSON.parse<unknown[]>('["Welcome to dynamic arrays",3.14,["Very",["Deep",["Array"]]],true,"It also supports nulls",null]')))
 //const foo = new Map()

-//console.log(JSON.stringify(parseDynamicObject('{"hello":"world"}')))
+console.log(JSON.stringify(parseDynamicObject('{"hello":"world"}')))
 // @ts-ignore
 @json
 class Test {
> json-as@0.2.6 test
> node --experimental-wasi-unstable-preview1 ./tests/test

{"hello":"world"}
...

This is the behavior when the input has a trailing new line:

diff --git a/assembly/test.ts b/assembly/test.ts
index 74d817b..0cbbbff 100644
--- a/assembly/test.ts
+++ b/assembly/test.ts
@@ -189,7 +189,7 @@ check<EmptySchema>('Encode/Decode object', emptyObj)
 //console.log(JSON.stringify(JSON.parse<unknown[]>('["Welcome to dynamic arrays",3.14,["Very",["Deep",["Array"]]],true,"It also supports nulls",null]')))
 //const foo = new Map()

-//console.log(JSON.stringify(parseDynamicObject('{"hello":"world"}')))
+console.log(JSON.stringify(parseDynamicObject('{"hello":"world"}\n')))
 // @ts-ignore
 @json
 class Test {
> json-as@0.2.6 test
> node --experimental-wasi-unstable-preview1 ./tests/test

{"hello":"world\""}
...

As you can see, the value contains an invalid \" which did not exist in the input.

I have also observed other bugs of this kind when new lines exist in other places, for example with this input:

{
  "hello": "world"
}

Parsing into JSON.Object results in an object with a "hello key and world" value.

JairusSW commented 2 years ago

Okay, yeah. I see this. Dynamic Objects are still in the works atm. I would recommend sticking with the schema objects because the dynamic system is pretty glitchy still. Also, I may have to make a rewrite soon because I'm finding some problems with the transforms. I'll try to get back to you as soon as possible

JairusSW commented 2 years ago

Okay, I'm back on this. Currently finishing up the rewrite and I am writing the algorithms to parse objects now. I'll keep this in mind and add it to tests

JairusSW commented 2 years ago

Fixed with latest release