Skip to content

Commit

Permalink
server: add rpc support for shallow serialized arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Dec 22, 2024
1 parent 0d9d425 commit 9098426
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 14 additions & 0 deletions server/python/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion server/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export class RpcPeer {
this.killed = new Promise<string>((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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9098426

Please sign in to comment.