Skip to content

Commit

Permalink
Add JSON deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Mar 17, 2023
1 parent 9d686de commit fadee71
Show file tree
Hide file tree
Showing 17 changed files with 463 additions and 91 deletions.
4 changes: 4 additions & 0 deletions packages/seroval/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ALL_ENABLED } from './compat';
import getIdentifier from './get-identifier';
import { AsyncServerValue } from './types';

interface IndexAssignment {
t: 'index';
Expand Down Expand Up @@ -47,6 +48,8 @@ export interface SerializationContext {
assignments: Assignment[];
// Supported features
features: number;

valueMap: Map<number, AsyncServerValue>;
}

export interface Options {
Expand Down Expand Up @@ -81,6 +84,7 @@ export function createSerializationContext(options: SerializationOptions): Seria
refSize: 0,
features: options.features,
markedRefs: new Set(options.markedRefs),
valueMap: new Map(),
};
}

Expand Down
7 changes: 6 additions & 1 deletion packages/seroval/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createSerializationContext,
} from './context';
import parseAsync from './tree/async';
import deserializeTree from './tree/deserialize';
import serializeTree, { resolvePatches } from './tree/serialize';
import parseSync from './tree/sync';
import { SerovalNode } from './tree/types';
Expand Down Expand Up @@ -141,7 +142,11 @@ export function compileJSON(source: SerovalJSON): string {
}

export function fromJSON<T extends AsyncServerValue>(source: SerovalJSON): T {
return deserialize<T>(compileJSON(source));
const serial = createSerializationContext({
features: source.f,
markedRefs: source.m,
});
return deserializeTree(serial, source.t) as T;
}

export default serialize;
11 changes: 11 additions & 0 deletions packages/seroval/src/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ export default function quote(str: string) {
}
return result;
}

export function invQuote(str: string): string {
return str
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\')
.replace(/\\x3C/g, '<')
.replace(/\\n/g, '\n')
.replace(/\\r/g, '\r')
.replace(/\\u2028/g, '\u2028')
.replace(/\\u2029/g, '\u2029');
}
15 changes: 8 additions & 7 deletions packages/seroval/src/tree/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
UNDEFINED_NODE,
} from './primitives';
import {
getErrorConstructor,
getErrorConstructorName,
getErrorOptions,
getIterableOptions,
isIterable,
Expand Down Expand Up @@ -95,7 +95,7 @@ async function generateArrayNode(
t: SerovalNodeType.Array,
i: id,
s: undefined,
l: undefined,
l: current.length,
c: undefined,
m: undefined,
d: undefined,
Expand Down Expand Up @@ -173,7 +173,7 @@ async function generateSetNode(
t: SerovalNodeType.Set,
i: id,
s: undefined,
l: undefined,
l: len,
c: undefined,
m: undefined,
d: undefined,
Expand Down Expand Up @@ -225,18 +225,19 @@ async function generateIterableNode(
): Promise<SerovalIterableNode> {
assert(ctx.features & Feature.Symbol, 'Unsupported type "Iterable"');
const options = getIterableOptions(current);
const array = Array.from(current);
return {
t: SerovalNodeType.Iterable,
i: id,
s: undefined,
l: undefined,
l: array.length,
c: undefined,
m: undefined,
// Parse options first before the items
d: options
? await generateProperties(ctx, options as Record<string, AsyncServerValue>)
: undefined,
a: await generateNodeList(ctx, Array.from(current)),
a: await generateNodeList(ctx, array),
n: undefined,
};
}
Expand Down Expand Up @@ -299,7 +300,7 @@ async function generateAggregateErrorNode(
t: SerovalNodeType.AggregateError,
i: id,
s: undefined,
l: undefined,
l: current.errors.length,
c: undefined,
m: current.message,
d: optionsNode,
Expand All @@ -322,7 +323,7 @@ async function generateErrorNode(
i: id,
s: undefined,
l: undefined,
c: getErrorConstructor(current),
c: getErrorConstructorName(current),
m: current.message,
d: optionsNode,
a: undefined,
Expand Down
Loading

0 comments on commit fadee71

Please sign in to comment.