Contribute to SSENSE/node-php-serialization development by creating an account on GitHub. Writing Awesome Node.JS API Using Decorators in TypeScript! One of my recent addiction was the usage of decorators, I was searching for ways to build cleaner and more concise code, and decorators.
Active4 years, 5 months ago
I am using sails.js (node js framework).
I am trying to JSON.stringify one of the objects, but when I do that it omits one of the fields (rooms array below).
Here is what console.log(object) gives me:
JSON output (rooms are not printed): Kodak printer driver for mac.
What might be the problem?The rooms data seems to be fine.
For the complete function (SailsJS):
EDITED
Schema:
Travis Webb
8,63166 gold badges4343 silver badges9393 bronze badges
user2867106user2867106
61111 gold badge99 silver badges2828 bronze badges
1 Answer
Because Waterline queries return models, not plain javascript objects, they have additional properties and functions on them. One of these is an overridden
toJSON() function which removes attributes that have not been populated. What seems to be happening here is that you are attaching objects to a parent model which doesn't know it has children that have been populated so it strips off the values.
The reasoning behind this is so that if you query for all Users and don't populate Rooms you don't get an incorrect result showing an empty rooms array.
I'm not sure what all you are manipulating here but the reason it works if you
_.cloneDeep is because it removes the custom toJSON field. This is the recommend strategy when you are mutating a parent object from a query like this.
particlebananaparticlebanana
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged jsonnode.jsexpresssails.jswaterline or ask your own question.
Active1 year, 11 months ago
I'm writing a simple serialization / deserialization framework for some application-specific objects.
Consider the following:
At this point, one can ask 'What does
d1 have that d2 lacks?'
One approach that partially works is to manually assign the methods of d1 to d2:
This has a couple of disadvantages. First, I have to manually assign each method of d1 to d2. Hp truevision camera driver windows 7 download. Second, d2 gets its own properties, and doesn't share slots using the prototype mechanism:
So my refined question is: given an object (e.g. Michał Perłakowski
d2 ), is there a way to associate it with the prototype of another object (e.g. d1 ) so it inherits the same behavior?
50.1k1616 gold badges114114 silver badges131131 bronze badges
fearless_foolfearless_fool
22k1818 gold badges100100 silver badges164164 bronze badges
2 AnswersObject.create() and Object.getOwnPropertyDescriptors() Hp printer drivers download windows 10. is what you need.
The difference between this and OP's method is that this method sets
prototype properties on the prototype, whereas OP's method sets properties directly on the object. You can see this when you loop through object own properties using for-in loop with hasOwnProperty() method:
With my method it outputs only
_name , but with OP's method it outputs also getName .
Unfortunately,
Object.getOwnPropertyDescriptors() is part of ECMAScript 2017 and it's supported only in Firefox for now, so you'll need to use Babel.
Alternatively, you can use
Object.setPrototypeOf() . It has better browser support than Object.getOwnPropertyDescriptors() , but it's discouraged by MDN, because it's slow.
Php 7 Vs Node JsGraham
4,1981414 gold badges4343 silver badges6464 bronze badges
Michał PerłakowskiMichał Perłakowski
50.1k1616 gold badges114114 silver badges131131 bronze badges
As I was writing this, I had the idea of creating a custom constructor that uses the deserialized JSON to initialize the object:
Update: how to dynamically find the class and assign the prototype
As @Louis points out, @Gothdo's answer requires that you know what class the deserialized object belongs to. If you're willing to add the class name to the serialized object, you can use that to determine the class dynamically. So, for example, to expand on the OP's example:
Using the trick described in deserialize JSON to JAVASCRIPT object (but tweaked for Node.js) you can use a string to get a handle to a class prototype via Node.js's
global object:
Now you can use that to dynamically reconstruct the object using @Gothdo's technique. Putting it all together:
Community♦
Js Serialize Objectfearless_foolNode Js Phpfearless_fool
22k1818 gold badges100100 silver badges164164 bronze badges
Js Serialize JsonNot the answer you're looking for? Browse other questions tagged javascriptjsonnode.jsclassserialization or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2020
Categories |