fix ts errors; restructure proxy map access; wip serde skip
This commit is contained in:
parent
b4c7a9ba2b
commit
db57f24b54
6 changed files with 34 additions and 26 deletions
4
dist/ser/impl.d.ts
vendored
4
dist/ser/impl.d.ts
vendored
|
@ -1,4 +1,4 @@
|
||||||
import { Serializer } from './interface';
|
import { ISerializer } from './interface';
|
||||||
import { Nullable } from '../utils';
|
import { Nullable } from '../utils';
|
||||||
import { SerdeOptions } from '../option';
|
import { SerdeOptions } from '../option';
|
||||||
export declare function serialize<T, V, S extends Serializer<T>>(serializer: S, value: V, optionsGetter?: (value: V) => Nullable<SerdeOptions>): T;
|
export declare function serialize<T, V, S extends ISerializer<T>>(serializer: S, value: V, optionsGetter?: (value: V) => Nullable<SerdeOptions>): T;
|
||||||
|
|
7
dist/ser/impl.js
vendored
7
dist/ser/impl.js
vendored
|
@ -9,8 +9,11 @@ class UnhandledTypeError extends TypeError {
|
||||||
}
|
}
|
||||||
function serializeObject(serializer, obj, options) {
|
function serializeObject(serializer, obj, options) {
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
const name = (options === null || options === void 0 ? void 0 : options.getSerializationPropertyName(key)) || key;
|
const value = obj[key];
|
||||||
serializer.serializeEntry(name, obj[key]);
|
if (options === null || options === void 0 ? void 0 : options.shouldSerialize(key, value)) {
|
||||||
|
const name = (options === null || options === void 0 ? void 0 : options.getSerializationPropertyName(key)) || key;
|
||||||
|
serializer.serializeEntry(name, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return serializer.end();
|
return serializer.end();
|
||||||
}
|
}
|
||||||
|
|
2
dist/ser/interface.d.ts
vendored
2
dist/ser/interface.d.ts
vendored
|
@ -19,7 +19,7 @@ export declare abstract class SerializeIterable<T> implements ISerializeIterable
|
||||||
abstract end(): T;
|
abstract end(): T;
|
||||||
}
|
}
|
||||||
export interface ISerializer<T> {
|
export interface ISerializer<T> {
|
||||||
serializeAny(value: any): T;
|
serializeAny?(value: any): T;
|
||||||
serializeBoolean(value: boolean): T;
|
serializeBoolean(value: boolean): T;
|
||||||
serializeNumber(value: number): T;
|
serializeNumber(value: number): T;
|
||||||
serializeBigInt(value: bigint): T;
|
serializeBigInt(value: bigint): T;
|
||||||
|
|
|
@ -32,20 +32,22 @@ class ProxyMapAccess implements IMapAccess {
|
||||||
}
|
}
|
||||||
|
|
||||||
private wrapResponse<T, I extends IteratorResult<T> = IteratorResult<T>>(result: I): I {
|
private wrapResponse<T, I extends IteratorResult<T> = IteratorResult<T>>(result: I): I {
|
||||||
if (result.done) {
|
switch (true) {
|
||||||
return result
|
default:
|
||||||
} else if (isString(result.value)) {
|
case result.done: return result
|
||||||
const key = this.options?.getDeserializationPropertyName(result.value) ?? result.value
|
|
||||||
return IterResult.Next(key) as I
|
case isString(result.value): {
|
||||||
} else if (Array.isArray(result.value)) {
|
const key = this.options?.getDeserializationPropertyName(result.value) ?? result.value
|
||||||
const [alias, value] = result.value
|
return IterResult.Next(key) as I
|
||||||
const key = this.options?.getDeserializationPropertyName(alias) ?? alias
|
}
|
||||||
return IterResult.Next([
|
case Array.isArray(result.value): {
|
||||||
key,
|
const [alias, value] = result.value
|
||||||
value
|
const key = this.options?.getDeserializationPropertyName(alias) ?? alias
|
||||||
]) as I
|
return IterResult.Next([
|
||||||
} else {
|
key,
|
||||||
return result
|
value
|
||||||
|
]) as I
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,26 @@
|
||||||
import { ISerializeObject, Serializer } from './interface'
|
import { ISerializeObject, ISerializer } from './interface'
|
||||||
import { isPlainObject, Nullable } from '../utils'
|
import { isPlainObject, Nullable } from '../utils'
|
||||||
import { SerdeOptions } from '../option'
|
import { SerdeOptions } from '../option'
|
||||||
|
|
||||||
class UnhandledTypeError extends TypeError {
|
class UnhandledTypeError extends TypeError {
|
||||||
constructor(serializer: Serializer<unknown>, value: any) {
|
constructor(serializer: ISerializer<unknown>, value: any) {
|
||||||
super(`unhandled type: '${typeof value}' for serializer ${serializer.constructor.name}`)
|
super(`unhandled type: '${typeof value}' for serializer ${serializer.constructor.name}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function serializeObject<T, V extends object, S extends ISerializeObject<T>>(serializer: S, obj: V, options?: SerdeOptions): T {
|
function serializeObject<T, V extends object, S extends ISerializeObject<T>>(serializer: S, obj: V, options?: SerdeOptions): T {
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
const name = options?.getSerializationPropertyName(key) || key
|
const value = obj[key]
|
||||||
serializer.serializeEntry(name, obj[key])
|
if (options?.shouldSerialize(key, value)) {
|
||||||
|
const name = options?.getSerializationPropertyName(key) || key
|
||||||
|
serializer.serializeEntry(name, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return serializer.end()
|
return serializer.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
function serializeClass<T, V extends object, S extends Serializer<T>>(serializer: S, value: V, options?: SerdeOptions): T {
|
function serializeClass<T, V extends object, S extends ISerializer<T>>(serializer: S, value: V, options?: SerdeOptions): T {
|
||||||
const name = value.constructor.name
|
const name = value.constructor.name
|
||||||
const ser = serializer.serializeClass(name)
|
const ser = serializer.serializeClass(name)
|
||||||
return serializeObject(ser, value, options)
|
return serializeObject(ser, value, options)
|
||||||
|
@ -27,7 +30,7 @@ const defaultGetter = (value: any): Nullable<SerdeOptions> => {
|
||||||
return value.constructor[Symbol.metadata]?.serde
|
return value.constructor[Symbol.metadata]?.serde
|
||||||
}
|
}
|
||||||
|
|
||||||
export function serialize<T, V, S extends Serializer<T>>(serializer: S, value: V, optionsGetter: (value: V) => Nullable<SerdeOptions> = defaultGetter): T {
|
export function serialize<T, V, S extends ISerializer<T>>(serializer: S, value: V, optionsGetter: (value: V) => Nullable<SerdeOptions> = defaultGetter): T {
|
||||||
switch (typeof value) {
|
switch (typeof value) {
|
||||||
case 'string': return serializer.serializeString(value)
|
case 'string': return serializer.serializeString(value)
|
||||||
case 'number': return serializer.serializeNumber(value)
|
case 'number': return serializer.serializeNumber(value)
|
||||||
|
|
|
@ -27,7 +27,7 @@ export abstract class SerializeIterable<T> implements ISerializeIterable<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ISerializer<T> {
|
export interface ISerializer<T> {
|
||||||
serializeAny(value: any): T
|
serializeAny?(value: any): T
|
||||||
serializeBoolean(value: boolean): T
|
serializeBoolean(value: boolean): T
|
||||||
serializeNumber(value: number): T
|
serializeNumber(value: number): T
|
||||||
serializeBigInt(value: bigint): T
|
serializeBigInt(value: bigint): T
|
||||||
|
|
Loading…
Add table
Reference in a new issue