seroval 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -93,7 +93,7 @@ export function deserialize<T extends AsyncServerValue>(source: string): T {
93
93
  return (0, eval)(source) as T;
94
94
  }
95
95
 
96
- interface SerovalJSON {
96
+ export interface SerovalJSON {
97
97
  t: SerovalNode,
98
98
  r: number,
99
99
  i: boolean,
@@ -104,44 +104,43 @@ interface SerovalJSON {
104
104
  export function toJSON<T extends ServerValue>(
105
105
  source: T,
106
106
  options?: Partial<Options>,
107
- ) {
107
+ ): SerovalJSON {
108
108
  const ctx = createParserContext(options);
109
109
  const [tree, root, isObject] = parseSync(ctx, source);
110
- return JSON.stringify({
110
+ return {
111
111
  t: tree,
112
112
  r: root,
113
113
  i: isObject,
114
114
  f: ctx.features,
115
115
  m: Array.from(ctx.markedRefs),
116
- });
116
+ };
117
117
  }
118
118
 
119
119
  export async function toJSONAsync<T extends AsyncServerValue>(
120
120
  source: T,
121
121
  options?: Partial<Options>,
122
- ) {
122
+ ): Promise<SerovalJSON> {
123
123
  const ctx = createParserContext(options);
124
124
  const [tree, root, isObject] = await parseAsync(ctx, source);
125
- return JSON.stringify({
125
+ return {
126
126
  t: tree,
127
127
  r: root,
128
128
  i: isObject,
129
129
  f: ctx.features,
130
130
  m: Array.from(ctx.markedRefs),
131
- });
131
+ };
132
132
  }
133
133
 
134
- export function compileJSON(source: string): string {
135
- const parsed = JSON.parse(source) as SerovalJSON;
134
+ export function compileJSON(source: SerovalJSON): string {
136
135
  const serial = createSerializationContext({
137
- features: parsed.f,
138
- markedRefs: parsed.m,
136
+ features: source.f,
137
+ markedRefs: source.m,
139
138
  });
140
- const result = serializeTree(serial, parsed.t);
141
- return finalize(serial, parsed.r, parsed.i, result);
139
+ const result = serializeTree(serial, source.t);
140
+ return finalize(serial, source.r, source.i, result);
142
141
  }
143
142
 
144
- export function fromJSON<T extends AsyncServerValue>(source: string): T {
143
+ export function fromJSON<T extends AsyncServerValue>(source: SerovalJSON): T {
145
144
  return deserialize<T>(compileJSON(source));
146
145
  }
147
146