Skip to content

Commit

Permalink
Merge pull request #268 from betwixt-labs/json-over-bebop-perf
Browse files Browse the repository at this point in the history
perf: minor speed up to json-over-bebop
  • Loading branch information
andrewmd5 authored Jun 20, 2023
2 parents ce077ae + fd1342e commit 86d041d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
5 changes: 2 additions & 3 deletions Core/Generators/TypeScript/TypeScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,12 @@ public string CompileJsonMethods(Definition definition)
builder.AppendLine(FormatDocumentation("Serializes the specified object into a JSON-Over-Bebop string", string.Empty, 0));
builder.CodeBlock($"public static encodeToJson(record: I{definition.ClassName()}): string", indentStep, () =>
{
builder.AppendLine("const clone = Object.assign(Object.create(Object.getPrototypeOf(record)), record);");
if (definition is UnionDefinition)
{
// delete the redundant discriminator field
builder.AppendLine("delete clone.value.discriminator;");
builder.AppendLine("delete (record.data.value as any).discriminator;");
}
builder.AppendLine("return JSON.stringify(clone, BebopJson.replacer);");
builder.AppendLine("return JSON.stringify(record, BebopJson.replacer);");
});
builder.AppendLine();

Expand Down
46 changes: 21 additions & 25 deletions Runtime/TypeScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ const isPrimitive = (value: any): boolean => {
* @returns The modified value for the property, or the original value if not a BigInt or Map.
*/
const replacer = (_key: string, value: any): any => {

if (typeof value === "bigint") {
return { [typeMarker]: bigIntTag, value: value.toString() };
}
Expand Down Expand Up @@ -774,24 +775,23 @@ const replacer = (_key: string, value: any): any => {
return { [typeMarker]: guidTag, value: value.toString() };
}
if (Array.isArray(value)) {
if (value.every(isPrimitive)) {
return value;
} else {
const newValue = [];
for (let i = 0; i < value.length; i++) {
newValue[i] = replacer(i.toString(), value[i]);
let replaceNeeded = false;
for (let i = 0; i < value.length; i++) {
if (!replaceNeeded && !isPrimitive(value[i])) {
replaceNeeded = true;
}
if (replaceNeeded) {
value[i] = replacer(i.toString(), value[i]);
}
return newValue;
}
return value;
}
if (typeof value === "object" && value !== null) {
for (let k in value) {
if (value.hasOwnProperty(k) && !isPrimitive(value[k])) {
if (!isPrimitive(value[k])) {
const obj: Record<any, any> = {};
for (let k in value) {
if (value.hasOwnProperty(k)) {
obj[k] = replacer(k, value[k]);
}
obj[k] = replacer(k, value[k]);
}
return obj;
}
Expand All @@ -809,6 +809,8 @@ const replacer = (_key: string, value: any): any => {
* @returns The modified value for the property, or the original value if not a marked type.
*/
const reviver = (_key: string, value: any): any => {
if (_key === "") return value;

if (value && typeof value === "object") {
if (value[typeMarker]) {
switch (value[typeMarker]) {
Expand All @@ -827,35 +829,29 @@ const reviver = (_key: string, value: any): any => {
}
const map = new Map();
for (let k in value.value) {
if (value.value.hasOwnProperty(k)) {
const trueKey = castScalarByTag(k, keyTag);
map.set(trueKey, reviver(k, value.value[k]));
}
const trueKey = castScalarByTag(k, keyTag);
map.set(trueKey, reviver(k, value.value[k]));
}
return map;
case guidTag:
return Guid.parseGuid(value.value);
case mapGuidTag:
const guidMap = new GuidMap();
for (let k in value.value) {
if (value.value.hasOwnProperty(k)) {
guidMap.set(Guid.parseGuid(k), reviver(k, value.value[k]));
}
guidMap.set(Guid.parseGuid(k), reviver(k, value.value[k]));
}
return guidMap;
default:
throw new BebopRuntimeError(`Unknown type marker: ${value[typeMarker]}`)
}
} else {
if (Object.values(value).every(isPrimitive)) {
return value;
} else {
for (let k in value) {
if (value.hasOwnProperty(k)) {
value[k] = reviver(k, value[k]);
}
for (let k in value) {
const v = value[k];
if (!isPrimitive(v)) {
value[k] =reviver(k, v);
}
}
return value;
}
}
return value;
Expand Down

0 comments on commit 86d041d

Please sign in to comment.