JairusSW / as-json

The only JSON library you'll need for AssemblyScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error with null value on nullable type

nickwesselman opened this issue · comments

I'm getting the following error when attempting to parse json with a null value:

abort: Key does not exist in ~lib/map.ts(105:17)

Types

@json
export class Metafield {
  value: string
}

@json
export class Customer {
  metafield: Metafield | null
}

@json
export class BuyerIdentity {
  customer: Customer | null
}

@json
export class Cart {
  buyerIdentity: BuyerIdentity | null
}

@json
export class DiscountNode {
    metafield: Metafield
}

@json
export class FunctionInput {
    cart: Cart
    discountNode: DiscountNode
}

Failing JSON

{
  "discountNode": {
    "metafield": {
      "value": "{\"discountPercentage\":50}"
    }
  },
  "cart": {
    "buyerIdentity": null
  }
}

Successful JSON

The following works fine when parsing.

{
  "discountNode": {
    "metafield": {
      "value": "{\"discountPercentage\":15}"
    }
  },
  "cart": {
    "buyerIdentity": {
      "customer": {
        "metafield": {
          "value": "true"
        }
      }
    }
  }
}

I'll take a look tomorrow

Interestingly, the following is successful as well, with a null value.

{
  "discountNode": {
    "metafield": {
      "value": "{\"discountPercentage\":15}"
    }
  },
  "cart": {
    "buyerIdentity": {
      "customer": null
    }
  }
}

I can confirm Nick's issue. I am getting the same error, abort: Key does not exist in ~lib/map.ts(105:17), for null values.

Hey, @mirceapiturca @nickwesselman, I'm trying to figure this out. Sorry for the wait. I have been quite busy with school and the like.

:) No worries. School should come first!

Seems to be an issue with AssemblyScript itself. For example, this fails but shouldn't since we know that Player.pos is not null

class Vec2 {
  x: f32;
  y: f32;
  serialize(): string {
    return `{"x":${this.x},"y":${this.y}}`
  }
}

class Player {
  pos: Vec2 | null
}

const player: Player = {
  pos: {
    x: 3.4,
    y: 1.2
  }
}

let p!: Player;
console.log(p.pos!.serialize())
//                 ^ Failing right here
abort: unexpected null in assembly/test.ts(82:13)
Error: failed to run main module `./build/test.wasm`

Caused by:
    0: failed to invoke command default
    1: exit with invalid exit status outside of [0..126)
       wasm backtrace:
           0:  0x742 - <unknown>!<wasm function 16>
           1: 0x2c0f - <unknown>!<wasm function 84>
           2: 0x28ae - <unknown>!<wasm function 79>
           ```

I think I figured it out. May need to spend time writing a transform to make this work

Should be fixed now with the latest release. (NPM and GitHub works)
Didn't need to write a transform either. I figured out I was attempting to call __JSON_Deserialize<T>(map) on a null pointer which caused the problem.
Again, thank you for raising this issue to my attention and I hope the new release helps :)

Also closing b/c fix is released