question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialize Array

See original GitHub issue

Hi, I’m struggling to deserialize an array with this deserialize<Array<User>>(json, Array<User>). I get the error Argument of type 'User[]' is not assignable to parameter of type 'new (...params: any[]) => User[]'. How do I go about deserializing an array? Any help is appreciated. Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
GillianPerardcommented, Feb 9, 2021

Hi thank you for using my lib.

You cannot directly deserialize an array because it’s not a json object.

You can do for example:

{
  "myArray": [
    { "id": 0, "name": "Item 1" },
    { "id": 1, "name": "Item 2" }
  ]
}
@Serializable()
class MyItem {
  @JsonProperty()
  id: number;

  @JsonProperty()
  name: string;
}

@Serializable()
class MyObject {
  @JsonProperty({ type: MyItem })
  myArray: Array<MyItem>;
}

deserialize<MyObject>(myJson, MyObject);

I hope it will help you!

0reactions
hecmeccommented, Nov 13, 2021

Hello, (just in case someone else has the same question) as for now I just preparse the string of a list

const list = JSON.parse(str);

and then I map each element

    const itemViewList: ItemViewLite[] = list.map((elm) => {
      return deserialize<ItemViewLite>(elm, ItemViewLite);
    });

That works ok for me. Thanks again. Hector

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize a JSON array in C# - Stack Overflow
Deserialize a JSON array in C# ; bool status = true ; byte[] readBuffer = null ; try { // Combine the new...
Read more >
Deserialize a Collection - Json.NET
This sample deserializes JSON into a collection.
Read more >
Deserializing a JSON array string into a C# object
Deserializing a JSON array string into a C# object. Hi, Everybody! I need some help… I wish to deserialize a JSON string into...
Read more >
How to Deserialize JSON Nested Arrays into C# ... - YouTube
In this video, I will show you how to Deserialize JSON Nested Arrays into C# classes.Learn how to construct C# classes that can...
Read more >
C# - Deserialize a JSON array to a list - MAKOLYTE
Shows how to deserialize a JSON array to a list, deserialize and yield one object at a time, and deserializing non-array collections of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found