1

I'm trying to develop a CRUD operations on IPFS, so I need to serialize a javascript object with its methods to save it in IPFS.

JSON.stringify(obj) it just serializes the object proprieties.

msanford
  • 11,803
  • 11
  • 66
  • 93
maroodb
  • 1,026
  • 2
  • 16
  • 28
  • 1
    Because JSON does not have functions as Properties of Objects – Luca Kiebel May 02 '18 at 13:35
  • I'm afraid it never was a good idea. You can keep code for class with methods in separate JS file and serve data serialized separately – Andrey May 02 '18 at 13:35
  • 1
    *"How to serialize a javascript object **into json file** that include all Class methods?"* You can't, by definition. JSON doesn't have any way of representing functions. You can create a string that's JSON-like (perhaps adhering to JavaScript's syntax for an object initializer), but it won't be JSON. You also have to consider: How many of its methods? Just its "own" methods? How about methods from its prototype? Its prototype's protoype? `Object.prototype`? The solution will likely involve recursion and `for-in`. – T.J. Crowder May 02 '18 at 13:38
  • In addition to the already good info on what `JSON.stringify()` serializes and what not, the [**JSON.stringify documentation**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) has a few example as well showing how functions, symbols, booleans, etc.. are serialized. – Nope May 02 '18 at 13:40
  • if I can't, I have to create a new object every time I retrieve data from IPFS and initialize the new object with the data from IPFS. I mean when make : let u = userDAO.findOne(id) ; i have to do: user = new User(u.name, u.email ..) and then return (user) – maroodb May 02 '18 at 13:57

0 Answers0