• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

The Typescript Worksop: TypeScript and JSON

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a Java programmer, but I've been working on a project for a while that has TypeScript and now we need to pull data from a REST service which will be in JSON format.  In Java, we use a library to convert the JSON to Java objects, and we can program to those objects.  

But with JSON being basically JavaScript objects already (without any methods), how would you handle the JSON as TypeScript?  Can there be a direct correlation between a JSON object and a TypeScript class that provides methods?  Feel free to tell me if I'm applying Java techniques to this issue :-)
 
Author
Posts: 10
5
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Timothy, thanks for the question! I have a Java background myself, so I think I can see where you're coming from with this one.

The first thing to mention is that TypeScript provides compile-time type-checking. If you are receiving a RESTful payload that you want to cast to a TypeScript type, keep in mind that is only a cast and the type-checking will not occur at runtime. To fill this gap, you may like to use a validator library to make sure the payload you're deserializing meets your expectations.

What TypeScript can provide is that your code is type-safe, provided the payload you receive passes validation. Then fundamentals here are still the same as you'd find in Java. That JSON API you're talking to really sends string data that needs to be serialized. Since our runtime has JSON.parse() in it, there's no real need for a 3rd party deserialization engine. Most clients like https://github.com/axios/axios or https://github.com/node-fetch will offer a convenience method or automatic deserialization. As a TypeScript programmer, you really want to find a client library that has good support for generics - a concept you may already be familiar with. I like the way axios handles this.



This code will successfully cast the response to the Person type, but again it won't actually type-check. We're somewhat guaranteed a json response here as axios will attempt to deseralize and will fail if the response doesn't include a JSON-parsable string. Most likely we'd want to employ try/catch to handle unexpected errors and as I mentioned before, it might be wise to use additional validators to make sure the response body we received actually matches the schema.

Hope you find that helpful, Timothy!
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you're stuck with something that doesn't provide generics for you, you can do the casting yourself. For instance:

But Matt's warning still holds. You're telling the compiler that you think the result is a Person. It won't check it for you, because TypeScript interfaces no longer exist after compiling to JavaScript. For JavaScript it's just an object.
 
Timothy Gallagher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK - I understand the interface part now.  

What would be a good technique to make the JSON look like a "class Person" with methods such as getName() that combines the first and last name?
 
Rob Spoor
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSON responses never contain (custom) methods, so you'll probably create a class. For instance:
 
Timothy Gallagher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that class appears good.  Is there an automagic way to drop the JSON into that class?
 
Matt Morgan
Author
Posts: 10
5
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd need to use a third-party lib to automatically serialize JSON to a class. There are a few out there such as class-transformer.

Or you could write your own:
 
Timothy Gallagher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info.  I'll check them out.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic