[ENGINE] Best serialization and deserialization approach (entity/component <-> JSON)

First things first: i’m making offline editor for PlayCanvas projects and because of that i’m using ‘naked’ engine (without Editor), and this is something that i cannot simply find in API docs (or i’m blind :smile:).

What is the best approach to serialize and deserialize entities and components from and to JSON? My goal is to make a wrapper that will take care about all changes sent from editor and apply those changes on given entity/component and for that i will use JSON data objects to transfer them between C# application and PlayCanvas scene viewer, so i need to simply call some serialize() and deserialize() methods that will apply this JSON data to given objects - how should i do that? :slight_smile:

Cheers! ^^

Don’t bother to store the actual object, just figure out what needs to be stored/sent.

When you save a file on your computer does it dump the ram for that project into a file? Nope, it saves the actual data.

Instead of relying on your own seialization, rely on playcanvas’ eg: instead of transfering the mesh data yourself, send a URL to the data, then use playcanvas’ asset library to load it.

hi @sdfgeoff, i think you misinterpreted my question - let me ask again in different words:
let’s say that you have component A and this component have given properties that you can edit in editor:

  • [number] speed: 30
  • [vector3] direction: <0, 1, 0>
    and your component have method serialize() that when you call it, it will return a json to you:
    { speed: 30, direction: [0,1, 1] }
    and that JSON goes from scene viewer through wrapper to editor and update component properties data in properties editor.
    now you change it’s ‘speed’ property value to 100, so editor send notification message to scene viewer with JSON data:
    { speed: 100 }
    and that data goes to wrapper that call deserialize() method of given component with given JSON data and component translates this data to actual properties values and applies them to given properties.

Is this possible somehow in PlayCanvas right now, or should i make that serialization/deserialization functionality by myself?:slight_smile:

Hi there,

I take that you just want de/serialise Javascript objects (components are objects), then just add this external js

-> https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Then use JSON.stringify(this) or JSON.parse(json)? It may be a little bit more to retain PlayCanvas .prototype functions…