Allowing instances to be sent between device and agent

If I send a table via agent.send() to the device, the serialiser will serialise the object before sending it, reconstituting it on the other side. The serialiser doesn’t like some objects; namely functions, metas, classes, instances. Instead, it replaces them with a null and quietly prints an error message (“ERROR: skipped unserialisable data”) in the console.

I don’t agree that all these items are unserialisable. An instance can easily be serialised by iterating through its class. It would be really handy if this feature were supported by the inbuilt serialiser.

Instances are valuable in the device as they consume relatively little RAM compared to tables. Although, they are use relatively more RAM if the number of instances is small (eg <4). Consequently, I’ll use them as a container, often as part of more complex structures.

class ObjClass {
	d=6
	e=true
}
objInstance <- ObjClass()    
agent.send("objSend",objInstance) // sends null to agent

objTable <- {
    	a=1
    	b=false
    	c={}
    }
agent.send("objSend",objTable)	// sends { "a":1, "b":false, "c":{} } to agent

objTable.c=objInstance
agent.send("objSend",objTable)	// sends { "a":1, "b":false, "c":null } to agent
// ideally, it should send { "a":1, "b":false, "c": { "d":6, "e":true } }

The squirrel code for iterating through an object is:

foreach(k,v in object)
	server.log("KEY="+k+" VALUE="+v)

The squirrel code for iterating through an object or instance of an object is:

foreach(k,v in (typeof(object)=="instance")?object.getclass():object)
	server.log("KEY="+k+" VALUE="+object[k])

I imagine the code for extending agent.send and device.send would be only a couple of lines. :wink:

Do you mean by automatically turning it into a table with the same fields?

You can’t serialise an instance as-is without being sure that the corresponding class is available on the other end, and that has … implications.

Also note that the serialisation is BSON and (currently) is not completely opaque to the various components that it passes through from the device to the agent, so that limits what kind of things that can safely be passed through.

Yes thanks, by serialise I mean turn it into a table with the same fields. I didn’t mean re-instantiation at the other end (although that is something I requested about 3-4 years ago, eg useful for a 64bit datatype).

I wouldn’t think that this would break any transparency rules that haven’t already been broken by behaviour of some strings being converted to blobs during the transfer process, or non string keys (valid Squirrel) being converted to strings.