javascript: Serialize/deserialize class instance when posting to api
Basically, I want to be able to POST a serialized class instance (JS front end) to an endpoint and deserialize it on the other end (nodejs backend).
Since I'm using JS on both sides it seems like I should be able to use the same class def to do the serialization/deserialization and end up with an instance of the same class on the backend.
In this way, if the class def changes, the front and back end will be in sync.
Please can someone help with some approaches/libraries that might be good for the job - or if the above sounds totally mad then please tell me!
javascript node.js
|
show 2 more comments
Basically, I want to be able to POST a serialized class instance (JS front end) to an endpoint and deserialize it on the other end (nodejs backend).
Since I'm using JS on both sides it seems like I should be able to use the same class def to do the serialization/deserialization and end up with an instance of the same class on the backend.
In this way, if the class def changes, the front and back end will be in sync.
Please can someone help with some approaches/libraries that might be good for the job - or if the above sounds totally mad then please tell me!
javascript node.js
Add atoJSON()
method in your class and send result to either end
– charlietfl
Nov 23 '18 at 20:55
I want the receiver to know what type of object to instantiate though - I could switch on a type param, but is there a better way?
– Matthew
Nov 23 '18 at 21:05
What do you mean by "what type"?
– charlietfl
Nov 23 '18 at 21:20
If I pass an instance of class Dog, I want to instantiate a Dog on the receiver - but may not know what type to instantiate at runtime
– Matthew
Nov 23 '18 at 21:26
So send an object like{className:'Dog', data:{...}}
– charlietfl
Nov 23 '18 at 21:30
|
show 2 more comments
Basically, I want to be able to POST a serialized class instance (JS front end) to an endpoint and deserialize it on the other end (nodejs backend).
Since I'm using JS on both sides it seems like I should be able to use the same class def to do the serialization/deserialization and end up with an instance of the same class on the backend.
In this way, if the class def changes, the front and back end will be in sync.
Please can someone help with some approaches/libraries that might be good for the job - or if the above sounds totally mad then please tell me!
javascript node.js
Basically, I want to be able to POST a serialized class instance (JS front end) to an endpoint and deserialize it on the other end (nodejs backend).
Since I'm using JS on both sides it seems like I should be able to use the same class def to do the serialization/deserialization and end up with an instance of the same class on the backend.
In this way, if the class def changes, the front and back end will be in sync.
Please can someone help with some approaches/libraries that might be good for the job - or if the above sounds totally mad then please tell me!
javascript node.js
javascript node.js
asked Nov 23 '18 at 20:32
MatthewMatthew
1931315
1931315
Add atoJSON()
method in your class and send result to either end
– charlietfl
Nov 23 '18 at 20:55
I want the receiver to know what type of object to instantiate though - I could switch on a type param, but is there a better way?
– Matthew
Nov 23 '18 at 21:05
What do you mean by "what type"?
– charlietfl
Nov 23 '18 at 21:20
If I pass an instance of class Dog, I want to instantiate a Dog on the receiver - but may not know what type to instantiate at runtime
– Matthew
Nov 23 '18 at 21:26
So send an object like{className:'Dog', data:{...}}
– charlietfl
Nov 23 '18 at 21:30
|
show 2 more comments
Add atoJSON()
method in your class and send result to either end
– charlietfl
Nov 23 '18 at 20:55
I want the receiver to know what type of object to instantiate though - I could switch on a type param, but is there a better way?
– Matthew
Nov 23 '18 at 21:05
What do you mean by "what type"?
– charlietfl
Nov 23 '18 at 21:20
If I pass an instance of class Dog, I want to instantiate a Dog on the receiver - but may not know what type to instantiate at runtime
– Matthew
Nov 23 '18 at 21:26
So send an object like{className:'Dog', data:{...}}
– charlietfl
Nov 23 '18 at 21:30
Add a
toJSON()
method in your class and send result to either end– charlietfl
Nov 23 '18 at 20:55
Add a
toJSON()
method in your class and send result to either end– charlietfl
Nov 23 '18 at 20:55
I want the receiver to know what type of object to instantiate though - I could switch on a type param, but is there a better way?
– Matthew
Nov 23 '18 at 21:05
I want the receiver to know what type of object to instantiate though - I could switch on a type param, but is there a better way?
– Matthew
Nov 23 '18 at 21:05
What do you mean by "what type"?
– charlietfl
Nov 23 '18 at 21:20
What do you mean by "what type"?
– charlietfl
Nov 23 '18 at 21:20
If I pass an instance of class Dog, I want to instantiate a Dog on the receiver - but may not know what type to instantiate at runtime
– Matthew
Nov 23 '18 at 21:26
If I pass an instance of class Dog, I want to instantiate a Dog on the receiver - but may not know what type to instantiate at runtime
– Matthew
Nov 23 '18 at 21:26
So send an object like
{className:'Dog', data:{...}}
– charlietfl
Nov 23 '18 at 21:30
So send an object like
{className:'Dog', data:{...}}
– charlietfl
Nov 23 '18 at 21:30
|
show 2 more comments
1 Answer
1
active
oldest
votes
When an object has a toJSON()
method you can define what needs to be serialized by JSON.stringify()
Then it's just a matter of setting up the constructor and toJSON()
method to be compatible regarding input/output and you can send that json back and forth
class Foo {
constructor(state) {
const defState = {foo: 'bar'}
this.state = state ? Object.assign(defState, state) : defState;
}
get person(){
return this.state.person
}
toJSON() {
return this.state
}
}
// local instance
const localInstance = new Foo({person:'Bob'});
const localJSON = JSON.stringify(localInstance);
console.log('Local class name : ',localInstance.constructor.name)
console.log('LocalJSON', localJSON)
// remote instance from local JSON
const remoteInstance = new Foo(JSON.parse(localJSON))
console.log('Remote instance person : ', remoteInstance.person)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452653%2fjavascript-serialize-deserialize-class-instance-when-posting-to-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When an object has a toJSON()
method you can define what needs to be serialized by JSON.stringify()
Then it's just a matter of setting up the constructor and toJSON()
method to be compatible regarding input/output and you can send that json back and forth
class Foo {
constructor(state) {
const defState = {foo: 'bar'}
this.state = state ? Object.assign(defState, state) : defState;
}
get person(){
return this.state.person
}
toJSON() {
return this.state
}
}
// local instance
const localInstance = new Foo({person:'Bob'});
const localJSON = JSON.stringify(localInstance);
console.log('Local class name : ',localInstance.constructor.name)
console.log('LocalJSON', localJSON)
// remote instance from local JSON
const remoteInstance = new Foo(JSON.parse(localJSON))
console.log('Remote instance person : ', remoteInstance.person)
add a comment |
When an object has a toJSON()
method you can define what needs to be serialized by JSON.stringify()
Then it's just a matter of setting up the constructor and toJSON()
method to be compatible regarding input/output and you can send that json back and forth
class Foo {
constructor(state) {
const defState = {foo: 'bar'}
this.state = state ? Object.assign(defState, state) : defState;
}
get person(){
return this.state.person
}
toJSON() {
return this.state
}
}
// local instance
const localInstance = new Foo({person:'Bob'});
const localJSON = JSON.stringify(localInstance);
console.log('Local class name : ',localInstance.constructor.name)
console.log('LocalJSON', localJSON)
// remote instance from local JSON
const remoteInstance = new Foo(JSON.parse(localJSON))
console.log('Remote instance person : ', remoteInstance.person)
add a comment |
When an object has a toJSON()
method you can define what needs to be serialized by JSON.stringify()
Then it's just a matter of setting up the constructor and toJSON()
method to be compatible regarding input/output and you can send that json back and forth
class Foo {
constructor(state) {
const defState = {foo: 'bar'}
this.state = state ? Object.assign(defState, state) : defState;
}
get person(){
return this.state.person
}
toJSON() {
return this.state
}
}
// local instance
const localInstance = new Foo({person:'Bob'});
const localJSON = JSON.stringify(localInstance);
console.log('Local class name : ',localInstance.constructor.name)
console.log('LocalJSON', localJSON)
// remote instance from local JSON
const remoteInstance = new Foo(JSON.parse(localJSON))
console.log('Remote instance person : ', remoteInstance.person)
When an object has a toJSON()
method you can define what needs to be serialized by JSON.stringify()
Then it's just a matter of setting up the constructor and toJSON()
method to be compatible regarding input/output and you can send that json back and forth
class Foo {
constructor(state) {
const defState = {foo: 'bar'}
this.state = state ? Object.assign(defState, state) : defState;
}
get person(){
return this.state.person
}
toJSON() {
return this.state
}
}
// local instance
const localInstance = new Foo({person:'Bob'});
const localJSON = JSON.stringify(localInstance);
console.log('Local class name : ',localInstance.constructor.name)
console.log('LocalJSON', localJSON)
// remote instance from local JSON
const remoteInstance = new Foo(JSON.parse(localJSON))
console.log('Remote instance person : ', remoteInstance.person)
class Foo {
constructor(state) {
const defState = {foo: 'bar'}
this.state = state ? Object.assign(defState, state) : defState;
}
get person(){
return this.state.person
}
toJSON() {
return this.state
}
}
// local instance
const localInstance = new Foo({person:'Bob'});
const localJSON = JSON.stringify(localInstance);
console.log('Local class name : ',localInstance.constructor.name)
console.log('LocalJSON', localJSON)
// remote instance from local JSON
const remoteInstance = new Foo(JSON.parse(localJSON))
console.log('Remote instance person : ', remoteInstance.person)
class Foo {
constructor(state) {
const defState = {foo: 'bar'}
this.state = state ? Object.assign(defState, state) : defState;
}
get person(){
return this.state.person
}
toJSON() {
return this.state
}
}
// local instance
const localInstance = new Foo({person:'Bob'});
const localJSON = JSON.stringify(localInstance);
console.log('Local class name : ',localInstance.constructor.name)
console.log('LocalJSON', localJSON)
// remote instance from local JSON
const remoteInstance = new Foo(JSON.parse(localJSON))
console.log('Remote instance person : ', remoteInstance.person)
edited Nov 23 '18 at 21:52
answered Nov 23 '18 at 21:26
charlietflcharlietfl
139k1386118
139k1386118
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452653%2fjavascript-serialize-deserialize-class-instance-when-posting-to-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Add a
toJSON()
method in your class and send result to either end– charlietfl
Nov 23 '18 at 20:55
I want the receiver to know what type of object to instantiate though - I could switch on a type param, but is there a better way?
– Matthew
Nov 23 '18 at 21:05
What do you mean by "what type"?
– charlietfl
Nov 23 '18 at 21:20
If I pass an instance of class Dog, I want to instantiate a Dog on the receiver - but may not know what type to instantiate at runtime
– Matthew
Nov 23 '18 at 21:26
So send an object like
{className:'Dog', data:{...}}
– charlietfl
Nov 23 '18 at 21:30