GillianPerard/typescript-json-serializer

[BUG]: Getting an error after deserializing and reading property.

quldude opened this issue · 3 comments

Version

5.1.0

Description

Not able to read property that has a decorator with json property name.

Error

Error in reporter TypeError: Cannot read properties of undefined (reading 'find')

Reproduction

Enabled these in tsconfig:

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

I have a class defined as such:

@JsonObject()
export class TestResolutionStates {
  @JsonProperty('__wrappedArray')
  public wrappedArray: WrappedArray[];
}
...
private resolutionStates: TestResolutionStates;

When I call this.resolutionStates.wrappedArray.find(...), I get an error. The json string is defined as:

{
    "__wrappedArray": [..]
}

On which OS the bug appears?

windows

What is your project type?

nodejs

On which build mode the bug appears?

No response

Anything else?

No response

Hi, I tried your code and it works.

Are you sure you have deserialize your object?

import { JsonObject, JsonProperty, JsonSerializer } from "typescript-json-serializer";

class WrappedArray {
  id: number
}

@JsonObject()
export class TestResolutionStates {
  @JsonProperty('__wrappedArray')
  public wrappedArray: WrappedArray[];
}

const json = {
  "__wrappedArray": [{id: 1}, {id: 4}]
}

const jsonSerializer = new JsonSerializer();
const resolutionStates: TestResolutionStates = jsonSerializer.deserializeObject(json, TestResolutionStates);
const result = resolutionStates.wrappedArray.find((a => a.id === 4))

console.log(result) // {id: 4}

I'm getting a different error now. See here. Do you know of a way to resolve it?

It seems Babel does not support decorator correclty