Skip to content

Commit 65e4aa1

Browse files
Copilotstreamich
andcommitted
Make EjsonEncoder and EjsonDecoder extend JsonEncoder/JsonDecoder for code reuse
Co-authored-by: streamich <[email protected]>
1 parent 838feff commit 65e4aa1

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/ejson2/EjsonDecoder.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
BsonTimestamp,
1515
} from '../bson/values';
1616
import {Reader} from '@jsonjoy.com/util/lib/buffers/Reader';
17+
import {JsonDecoder} from '../json/JsonDecoder';
1718
import {readKey} from '../json/JsonDecoder';
1819
import type {BinaryJsonDecoder} from '../types';
1920

@@ -22,19 +23,9 @@ export interface EjsonDecoderOptions {
2223
legacy?: boolean;
2324
}
2425

25-
export class EjsonDecoder implements BinaryJsonDecoder {
26-
public reader = new Reader();
27-
28-
constructor(private options: EjsonDecoderOptions = {}) {}
29-
30-
public read(uint8: Uint8Array): unknown {
31-
this.reader.reset(uint8);
32-
return this.readAny();
33-
}
34-
35-
public decode(uint8: Uint8Array): unknown {
36-
this.reader.reset(uint8);
37-
return this.readAny();
26+
export class EjsonDecoder extends JsonDecoder {
27+
constructor(private options: EjsonDecoderOptions = {}) {
28+
super();
3829
}
3930

4031
/**

src/ejson2/EjsonEncoder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export class EjsonEncoder extends JsonEncoder {
4141
return new TextDecoder().decode(bytes);
4242
}
4343

44+
public writeUnknown(value: unknown): void {
45+
this.writeNull();
46+
}
47+
4448
public writeAny(value: unknown): void {
4549
if (value === null || value === undefined) {
4650
if (value === undefined) {
@@ -421,7 +425,6 @@ export class EjsonEncoder extends JsonEncoder {
421425
this.writeStr(value.subtype.toString(16).padStart(2, '0'));
422426
writer.u16(0x7d7d); // }}
423427
}
424-
}
425428

426429
private writeBsonCodeAsEjson(value: BsonJavascriptCode): void {
427430
// Write {"$code":"..."}

0 commit comments

Comments
 (0)