GillianPerard / typescript-json-serializer

A typescript library to deserialize json into typescript classes and serialize classes into json.
MIT License
210 stars 29 forks source link

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

Closed quldude closed 1 year ago

quldude commented 1 year ago

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

GillianPerard commented 1 year ago

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}
quldude commented 1 year ago

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

GillianPerard commented 1 year ago

It seems Babel does not support decorator correclty