Comments (9)
Hi,
looks like its a regular json and you need to create proper classes for this, like:
class StructuredJson {
links: StructuredJsonLinks;
data: StructuredJsonData[];
}
class StructuredJsonLinks {
self: string;
next: string;
last: string;
}
class StructuredJsonData ....
How do you think your data should be transformed by this library from json to objects?
from class-transformer.
That makes sense for the links
, but when you have an article in the above example you would like to have a model class like this:
export class Article{
title:string;
author: Person;
comments: Comments[];
}
export class Person{
firstName: string;
lastName: string;
twitter: string;
// Optional:
link:string // But is this something a person should know?
}
export class Comment{
id:number;
body: string;
author: Person;
}
I'm not very familiar with serializing/deserializing (yet).
But for me it seems, the hard part would be to check the relationships, see if this data is "included", and if so, create object with this data within the main data object.
The 'data' has the id
and attributes
in different fields. So that would mean 2 maps?
import {deserialize} from "serializer.ts/Serializer";
this.http
.get("users.json")
.map(res => res.json())
.map(res => deserialize<Article>(article, res.data.id)) // get the ID
.map(article => deserialize<Article>(article, res.data.attributes)) // get the attributes and add them to the existing article??
.map(article => deserialize<Person>(article, res.data.relationships)) // get the relationships and add them to the existing article??
.subscribe(users => {
// now "users" is type of User[] and each user have getName() and isKid() methods available
console.log(users);
});
hoping there is a faster/better way to do this? :-)
hmm. just thinking..
might be possible to use something like flatmap
or switchmap
or something to combine data and relationships or something.?
Or do something with ES6 Array destructuring to get the proper values..
Really have to take a deep dive into ES6 / Typescript.
from class-transformer.
looks like what you want is not a part of this library, and it should not be. You simply need to create a map function that will convert your json in your specific format into modelled format (only persons, articles and other your models), and then user deserialization
from class-transformer.
thanks I'll look into that. And see if I get it working with your library.
from class-transformer.
thanks
from class-transformer.
@the-ult did you get a pattern worked out? would be nice to see it
from class-transformer.
@jcrben I started a new job and never got a chance to get it worked out. Sorry.
from class-transformer.
Dear @jcrben ,
now i am in the same position as you where 2 years before.. did you manage to solve this? How did you proceed?
Thanks for helping me out!
from class-transformer.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from class-transformer.
Related Issues (20)
- question: how to use exposing method inside a nested object array dto?
- fix: exposeDefaultValues does not work HOT 2
- Renamed property does not exist on type Class HOT 2
- Expose properties with different name using a callback HOT 1
- question: How to expose attr when plain object no define HOT 1
- question: How to ignore Transform decorator when using plainToInstance HOT 2
- question: Getting `ReferenceError: Cannot access 'XXX' before initialization` in class circular dependency with Jest & Typescript HOT 3
- question: Constructors validation seems to break when using plainToInstance HOT 1
- Include @IsOptional values to the body if they are missing with default value HOT 2
- fix: Can't use instanceToPlain with Nitro.js event. HOT 4
- External Values are not stripped. HOT 4
- fix: plainToClass cannot handle URL values HOT 5
- feature: function support of options.discriminator.subTypes HOT 2
- plainToClassFromExist is Deprecated what is the replacement for generics? HOT 2
- Cannot transform string to number HOT 7
- question: Is class-transformer really thought to be used in the backend? HOT 4
- question: Options in @Transform Decorator Do Not Reflect Those Passed to plainToInstance in class-transformer HOT 2
- question: Casting Json to the required datatype HOT 2
- instanceToPlain not working properly using nestjs HOT 3
- Transform decorator in class-transformer does not wait for asynchronous function and returns a Promise instead of the transformed value HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from class-transformer.