Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Deserializing 2 Levels Deep #9

Open
bassrock opened this issue Jun 7, 2017 · 10 comments
Open

Only Deserializing 2 Levels Deep #9

bassrock opened this issue Jun 7, 2017 · 10 comments

Comments

@bassrock
Copy link

bassrock commented Jun 7, 2017

Currently I have object that are nested 3-4 levels deep. However the library is currently only serializing 2 levels deep.

Is there an option somewhere to make the library deserialize deeper?

@bassrock
Copy link
Author

bassrock commented Jun 7, 2017

I should also note all my objects have custom types. And that it is creating objects 3 levels deep but not creating the objects with my custom types 3 levels deep.

@shakilsiraj
Copy link
Owner

Hi there,

There is no limit to how deep you can go. Here are some examples you can look at:
https://github.com/shakilsiraj/json-object-mapper/blob/master/src/test/DeserializeDataset.spec.ts
https://github.com/shakilsiraj/json-object-mapper/blob/master/src/test/DeserializeLargeDataset3.spec.ts

Can you provide a testcase that I can try?

@bassrock
Copy link
Author

bassrock commented Jun 8, 2017

So its deserializing but its not deserializing to the custom types. And upon further inspection it looks like it is not deserializing any custom nested objects.

My code looks like this:

getProjectList() {
        return this.http.get(this.usersURL + 'projects/' + '1' + '/limit/' + '6')
          .map((response: Response) => {
               const projectUserList: ProjectUser[] = ObjectMapper.deserializeArray(ProjectUser, response.json());
        });
}
export class ProjectUser {

  @JsonProperty({name: 'id'})
  id: string = undefined;

  @JsonProperty({ type: Date, deserializer: DateSerializerDeserializer, serializer: DateSerializerDeserializer, name: 'created_at'})
  createdAt: Date = undefined;

  @JsonProperty({ type: Date, deserializer: DateSerializerDeserializer, serializer: DateSerializerDeserializer, name: 'updated_at'})
  updatedAt: Date = undefined;

  @JsonProperty({type: User, name: 'user'})
  user: User = undefined;

  @JsonProperty({type: Address, name: 'address'})
  address: Address = undefined;
}
export class Address {
  @JsonProperty({name: 'id'})
  id: string = undefined;

  @JsonProperty({ type: Date, deserializer: DateSerializerDeserializer, serializer: DateSerializerDeserializer, name: 'created_at'})
  createdAt: Date = undefined;

  @JsonProperty({ type: Date, deserializer: DateSerializerDeserializer, serializer: DateSerializerDeserializer, name: 'updated_at'})
  updatedAt: Date = undefined;

  @JsonProperty({name: 'street_address_line1'})
  streetAddressLine1: string = undefined;

  @JsonProperty({name: 'street_address_line2'})
  streetAddressLine2: string = undefined;
}

When I breakpoint and look at the ProjectUser object and then at the Address object all the primitive and custom deserializer date values are deserialized properly but not the Address object underneath.

It looks like the deserialize function may not be taking note of the custom type annotation.

@bassrock
Copy link
Author

bassrock commented Jun 8, 2017

I also tried to pull the library and run the karma tests to make a test case but I couldn't get that running either. Can you provide some documentation on how to run the test cases in the readme?

@bassrock
Copy link
Author

bassrock commented Jun 8, 2017

@shakilsiraj So stepping through a debugger it seems the reflector library is not returning the type parameter to the deserializer.

@bassrock
Copy link
Author

bassrock commented Jun 8, 2017

And even more weird. Nested objects of the same type of the parent object seem to work inside.

@bassrock
Copy link
Author

bassrock commented Jun 8, 2017

Ok definitely something with the Reflect library as I tried this:

const stuff = Reflect.getMetadata("JsonProperty", projectUserList[0], 'createdAt');

That returns:
screen shot 2017-06-08 at 4 07 21 pm

While:

const stuff = Reflect.getMetadata("JsonProperty", projectUserList[0], 'user');

Returns:
screen shot 2017-06-08 at 4 08 42 pm

So there is something up with the types annotation.

@bassrock
Copy link
Author

bassrock commented Jun 8, 2017

I think it might be the same as: jf3096/json-typescript-mapper#19

But not sure how to do it with different class files.

@bassrock
Copy link
Author

bassrock commented Jun 8, 2017

Hmm so changing my imports around change things, but I use the same objects in various places so not sure what to do here.

@tomas-ortega
Copy link

tomas-ortega commented Sep 10, 2017

Ex. I have 1 nested class, address instance inside person instance:
class Address {
@JsonProperty({name: "id"})
public _id: number;

public constructor() {
    this._id = undefined;
}

}

class Person {
@JsonProperty({name: "id"})
public _id: number;

@JsonProperty({name: "address"})
public _address: Address;

public constructor() {
    this._id = undefined;
    this._address = undefined;
}

}

Serialization Result: { id: 3, address: { id: 32 } } (Working)
Deserialization Result: Person { _id: 3, _address: { id: 32 } } (Not Working, _address.id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants