Coder Social home page Coder Social logo

Comments (9)

pleerock avatar pleerock commented on May 22, 2024

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.

the-ult avatar the-ult commented on May 22, 2024

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.

pleerock avatar pleerock commented on May 22, 2024

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.

the-ult avatar the-ult commented on May 22, 2024

thanks I'll look into that. And see if I get it working with your library.

from class-transformer.

pleerock avatar pleerock commented on May 22, 2024

thanks

from class-transformer.

jcrben avatar jcrben commented on May 22, 2024

@the-ult did you get a pattern worked out? would be nice to see it

from class-transformer.

the-ult avatar the-ult commented on May 22, 2024

@jcrben I started a new job and never got a chance to get it worked out. Sorry.

from class-transformer.

johannesschobel avatar johannesschobel commented on May 22, 2024

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.

github-actions avatar github-actions commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.