0

I just implemented a serializer for a class as follow:

import {File} from './file'
import Serialization from './serializable'

class User implements Serialization<User> {
  username: string
  deserialize(input) {
    this.username = input.username

    return this
  }

}

Some details are omitted

However, I thought to do serialization and convert this object into Json, I need to implement a serializer which is reverse to what I wrote in this deserializer

But the puzzling thing is, when I do:

app.use('/abc', (req, res) => {
  getLoggedInUser(sessionToken)
    .then(
      user => {
        const tmp: User = s
        res.send(tmp)
      }
    ).catch(
      err => {
        console.log("err =" + JSON.stringify(err))
      }
  )

express actually returned the JSON version of this User object.

I'm quite surprised by this behavior. Does that mean I don't need a serializer since it's done by typescript automatically? Or am I missing something?

1 Answer 1

1

If it's an object or an array it will stringify it as JSON. From the Express docs:

When the parameter is an Array or Object, Express responds with the JSON representation:

res.send({ user: 'tobi' });
res.send([1,2,3]);

https://expressjs.com/en/api.html#res.send

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. even if it's an instance of a class it will have this behavior?
An instance of a class is just an object. It will ignore the functions and stringify the own properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.