JairusSW / as-json

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

Parsing dates doesn't work #54

Closed mattjohnsonpint closed 1 year ago

mattjohnsonpint commented 1 year ago

According to #35 - we should have support for Date types implemented. But this doesn't work:

import { JSON } from "json-as/assembly";

@json
class Foo {
  timestamp!: Date;
}

const foo = JSON.parse<Foo>("{\"timestamp\":\"1999-12-31T23:59:59.999Z\"}");

When compiling, I get:

ERROR TS2322: Type '~lib/date/Date' is not assignable to type '~lib/wasi_date/wasi_Date'.
     :
 294 │ return Date.fromString(data);
     │        ~~~~~~~~~~~~~~~~~~~~~
     └─ in ~lib/json-as/assembly/src/json.ts(294,12)

FAILURE 1 compile error(s)
mattjohnsonpint commented 1 year ago

Ah, just noticed the detail of the error. It's because I'm using the AssemblyScript WASI Shim. If I remove it, then it compiles.

Any ideas how to work around?

mattjohnsonpint commented 1 year ago

Looks like I can switch back to the normal Date object with:

import { Date } from "date";

Of course that means I can only use the original implementation of Date.now, instead of the WASI implementation, but I think I can just ensure my host exports Date.now properly and ignore that aspect of WASI. (Or just get rid of WASI altogether, perhaps.)

JairusSW commented 1 year ago

Yeah, I have run into this problem before. I'll look into it though. I believe it has something to do with the transform using Date and then the compiler's pass overriding Date to wasi_Date. I'd recommend you ask in the assemblyscript discord server though

JairusSW commented 1 year ago

Can you import the wasi_Date directly? import { Date as wasi_Date } from "@assemblyscript/wasi-shim/assembly/wasi-date" https://github.com/AssemblyScript/wasi-shim/blob/4399cffa93a70da3a48b30c55d90414be7ec8dc0/assembly/wasi_date.ts#L15

mattjohnsonpint commented 1 year ago

Actually, I acted to soon to close the issue. Even without the WASI shim, I still can't deserialize an object containing a date to a Date field. It compiles fine, but at runtime, I get:

abort: Could not deserialize data 2023-11-16T04:06:35.108285303Z to type Date. Make sure to add the correct decorators to classes. in ~lib/json-as/assembly/src/json.ts(297:5)
mattjohnsonpint commented 1 year ago

Can you import the wasi_Date directly?

Doesn't help. It's not the Date type that as-json uses, so it's mismatched. But as I said, I can live without the WASI implementation. I just need it to work with the regular Date object without giving an error. Thanks.

JairusSW commented 1 year ago

Fixed this problem in https://github.com/JairusSW/as-json/commit/2c07ef2dd8eeee1bf4a3d95c2316a6d9709bd2db Changes are live on NPM (v0.6.0)

JairusSW commented 1 year ago

Fixed.

mattjohnsonpint commented 1 year ago

I tested and it works, but I don't think you want to pull in the whole date parser. Please consider #55 instead.

In particular, the date parsing logic has some errors when presented with strings having more than 3 decimal places of fractional seconds. It also doesn't handle ISO strings containing time zone offsets. Both of those issues are better handled in the AssemblyScript repo. I'll send a PR there. Thanks.

mattjohnsonpint commented 1 year ago

FYI - I sent a PR to AssemblyScript to fix the issues I mentioned. https://github.com/AssemblyScript/assemblyscript/pull/2803

JairusSW commented 1 year ago

Merged and published