0

Let's say, I have a class like this:

var block = class {
    constructor(val1, val2) {
        this.value1 = val1;
        this.value2 = val2;
    }
}

I want to send an instance of this class from one server to another. I would typically pass it through JSON.stringify() before sending it to the other server, then JSON.parse() once it reaches the other server. However, this would involve converting it to JSON and it would no longer be an instance of the 'block' class.

If I wanted to send it over in a way that keeps it as an instance of the 'block' class what would be best practice.

  • 1
    Possible duplicate of [Using JSON.stringify on custom class](https://stackoverflow.com/questions/13589880/using-json-stringify-on-custom-class) – Daniel Apr 15 '18 at 11:47
  • 1
    You can't send objects, what you will need to do is create the Class at the other side, and you could just use `Object.assign` to re-copy back the props – Keith Apr 15 '18 at 11:47
  • Would this be worth the overhead? I might just use 'fake' function classes to avoid this whole issue. –  Apr 15 '18 at 11:48
  • What overhead? JSON serialization is built into Javascript, the biggest overhead would be sending the data to Server in the first place. – Keith Apr 15 '18 at 11:49
  • The overhead of recreating the object on the other side, I will be sending a lot of them back and forth. –  Apr 15 '18 at 11:51
  • 1
    There is still no overhead here compared to sending the data,.. Micro optimization here, instead of easy to read code is wasting your time. If it makes sense to re-serialize to the same object class at the server side do it. – Keith Apr 15 '18 at 11:52
  • 1
    @DanielPahor Well, As others have mentioned. One small information, HTTP works on string representation only. You have to write a mapper to make object from string as done by Jackson in JAVA. Its should by the way be fairly easy on Javascript server as they support JSON out of the box – binariedMe Apr 15 '18 at 11:59
  • different browsers store objects in different ways in memory, so there is always need for conversion to uniform format. – Slai Apr 15 '18 at 12:09
  • 1
    Have a look at [Casting plain objects to function instances (“classes”) in javascript](https://stackoverflow.com/q/11810028/1048572). Transporting classes, consisting of functions with code and closures and identity references is hard and inefficient. – Bergi Apr 15 '18 at 12:27

1 Answers1

0

What your asking for here, is a kind of RPC (Remote procedure call).

And I think your right in making it so that when you deserialize the object at the server side, you can re-use the class that you used at client side.

Using your example class block, here I've added a method called display,. What's handy here is that display will work on both servers after sending the JSON.

Obviously here in an SO snippet it's a little awkward to show Server to Server comms, but what I have done is save to a String, and then regenerate the same class from the String, exactly what you would do if doing some RPC.

You could of course extend this so that even the class name gets sent, and this is used to re-generate the object, so that it could send lots of types of RPC commands.

var block = class {
    constructor(val1, val2) {
        this.value1 = val1;
        this.value2 = val2;
    }
    display() {
      console.log(this.value1, this.value2);
    }
}

var b1 = new block(1,2);

b1.display();

//lets pretend our stream is what gets sent over network
var stream = JSON.stringify(b1);

var b2 = new block();
Object.assign(b2, JSON.parse(stream));

b2.display();
Keith
  • 22,005
  • 2
  • 27
  • 44