Can I store a Map of custom objects in DOpus.Vars?

Trying to save and restore a map of custom objects ... and not succeeding.

I have a custom object defined as something like this in an include script :

function CustomObject(name, value1, ...) {
this.name = name;
this.someProperty = value1;
this.someMethod = function() { DOpus.Output(this.someProperty); };
// ...
}

In another script, I am trying to save a map of those custom objects (DOpus.Vars.Set and DOpus.Vars("myMapVarName").persist = true;).
The map gets saved, I can get it back (sort of) : I get the proper length and can use Map methods on it.
But, when I try to enumerate the map and get each element, I don't seem to be getting back my custom object :

  • I can't access properties
  • The DOpus.TypeOf of the retrieved object is unknown
  • If I check (via instanceof) the type of the retrieved object, it does not match CustomObject
  • JSON.stringify returns undefined (whereas working on object in the map before saving).

Note : my uservars.oxc looke like this (and the map is CommonLoggerConfig) :

<?xml version="1.0" encoding="UTF-8"?>
<uservars>
	<CommonLoggerConfig map="yes" size="2" type="9">
		<elements />
	</CommonLoggerConfig>
	<EverythingDopusCLI path="yes" string="C:\ZZ_PORTABLE_SOFTWARE_x64\EverythingDopus\ed.exe" type="9" />
</uservars>

Since persisting Maps was added (as reported here), I add the hope it would be also extended to custom object types ... it looks like I got carried away :slight_smile:

Any clue as to what would be the best way to persist custom objects ?
Maybe having an inside mechanism to serialize/deserialize ?

Many thanks for any insight.

Not possible:

If you want your variable to be persistent you should only use bool , int , string , date , currency or a Vector of those types.

You could think about storing it in json format (as string) and converting it back later. I think its not advised (as it is considered insecure) but would do the job.

e.g.
For saving

vars.Set("CUSTOM", JSON.stringify(yourObject));

For retrieving the object back evaluate the stored json (insecure, but it mostly is just your own code, its up to you) and the continue as usual.

eval("var storedObject = vars.Get(CUSTOM);")
storedObject.doYourThing(...);

No guarantee that it (still) works :wink:

1 Like

Thanks for the suggestion.
That's what I had in mind when talking about serialization/desarialization.

Currently trying to implement this (a bit more complicated than a simple stringify since the object has a map of map in its properties, and this is not included in the JSON I get from stringify, probably because DOpus.Map does not implement toJSON() which could be a bit tricky to implement in a generic way.
I'm gonna try and build an object that can be serialized as a string (very closely to stringify) and that can build itself back from such a string.