From 9098426c3b0a2744c4cc44cc2b5b27f1f836189f Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 22 Dec 2024 12:16:10 -0800 Subject: [PATCH] server: add rpc support for shallow serialized arrays --- server/python/rpc.py | 14 ++++++++++++++ server/src/rpc.ts | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/server/python/rpc.py b/server/python/rpc.py index 63cc04b4c0..5b46d857eb 100644 --- a/server/python/rpc.py +++ b/server/python/rpc.py @@ -292,6 +292,14 @@ def serialize(self, value, serializationContext: Dict): ret[key] = self.serialize(val, serializationContext) return ret + if getattr(value, RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN, None) == True: + array = [] + for key, val in value.items(): + array[key] = self.serialize(val, serializationContext) + return { + RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN: array + } + if RpcPeer.isTransportSafe(value): return value @@ -427,6 +435,12 @@ def deserialize(self, value, deserializationContext: Dict): RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN, None ) if copySerializeChildren: + if type(copySerializeChildren) == list: + array = [] + for val in copySerializeChildren: + array.append(self.deserialize(val, deserializationContext)) + return array + ret = {} for key, val in value.items(): ret[key] = self.deserialize(val, deserializationContext) diff --git a/server/src/rpc.ts b/server/src/rpc.ts index 1ff82a953a..7890159c67 100644 --- a/server/src/rpc.ts +++ b/server/src/rpc.ts @@ -396,7 +396,7 @@ export class RpcPeer { this.killed = new Promise((resolve, reject) => { this.killedDeferred = { resolve, reject, method: undefined }; }).catch(e => e.message || 'Unknown Error'); - this.killedSafe = this.killed.then(() => {}).catch(() => { }); + this.killedSafe = this.killed.then(() => { }).catch(() => { }); } static isTransportSafe(value: any) { @@ -497,6 +497,14 @@ export class RpcPeer { const copySerializeChildren = value[RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN]; if (copySerializeChildren) { + if (Array.isArray(copySerializeChildren)) { + const array = []; + for (const val of copySerializeChildren) { + array.push(this.deserialize(val, deserializationContext)); + } + return array; + } + const ret: any = {}; for (const [key, val] of Object.entries(value)) { ret[key] = this.deserialize(val, deserializationContext); @@ -561,6 +569,17 @@ export class RpcPeer { serialize(value: any, serializationContext: any): any { if (value?.[RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN] === true) { + if (Array.isArray(value)) { + const array = []; + for (const val of value) { + array.push(this.serialize(val, serializationContext)); + } + + return { + [RpcPeer.PROPERTY_JSON_COPY_SERIALIZE_CHILDREN]: array, + }; + } + const ret: any = {}; for (const [key, val] of Object.entries(value)) { ret[key] = this.serialize(val, serializationContext);