From 548c206afb7f5ff0f6f0c8b1ab20b48133f662b9 Mon Sep 17 00:00:00 2001 From: rowan Date: Sat, 17 May 2025 00:14:27 -0500 Subject: [PATCH] property renaming --- src/de/mixin.ts | 2 ++ src/decorator.ts | 8 ++++---- src/ser/impl.ts | 9 +++++---- src/ser/mixin.ts | 7 ++++++- tsconfig.json | 4 ++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/de/mixin.ts b/src/de/mixin.ts index 689bb3c..37d5873 100644 --- a/src/de/mixin.ts +++ b/src/de/mixin.ts @@ -1,3 +1,4 @@ +import { Serde } from '../decorator' import { Constructor, staticImplements } from '../utils' import { GenericVisitor } from './generic' import { Deserialize, Deserializer } from './interface' @@ -10,6 +11,7 @@ export function deserialize(constructor: C) { return deserializer.deserializeAny(visitor) } } + Deserializable[Serde] = (constructor as any)[Serde] return Deserializable } diff --git a/src/decorator.ts b/src/decorator.ts index e17122a..8137719 100644 --- a/src/decorator.ts +++ b/src/decorator.ts @@ -2,7 +2,6 @@ import { ContainerOptions, PropertyOptions, SerdeOptions } from "./options" export const Serde = Symbol('Serde') - function decorateContainer(options: ContainerOptions, constructor: any) { if (constructor[Serde] == null) { constructor[Serde] = new SerdeOptions(constructor, options) @@ -14,11 +13,11 @@ function decorateContainer(options: ContainerOptions, constructor: any) { } function decorateProperty(options: PropertyOptions, target: any, property: PropertyKey) { - let constructor + let constructor: any if (typeof target === 'function') { - constructor = target.constructor - } else { constructor = target + } else { + constructor = target.constructor } if (constructor[Serde] == null) { @@ -26,6 +25,7 @@ function decorateProperty(options: PropertyOptions, target: any, property: Prope } constructor[Serde].properties.set(property, options) + return target } diff --git a/src/ser/impl.ts b/src/ser/impl.ts index 27ab297..64fa155 100644 --- a/src/ser/impl.ts +++ b/src/ser/impl.ts @@ -1,15 +1,16 @@ import { Serde } from '../decorator' -import { SerdeOptions } from '../options' +import { SerdeOptions, Stage } from '../options' import { ifNull, isFunction, isIterable, Nullable, orElse } from '../utils' import { IterableSerializer, ObjectSerializer, Serializable, Serializer } from './interface' const unhandledType = (serializer: any, value: any) => new TypeError(`'${serializer.constructor.name}' has no method for value type '${typeof value}'`) -function serializeEntries>(serializer: ObjectSerializer, value: E) { +function serializeEntries>(serializer: ObjectSerializer, value: E, options?: SerdeOptions) { let state for (const [key, val] of value) { - state = serializer.serializeKey(key) + const name = options?.getPropertyName(key as string, Stage.Serialize) ?? key + state = serializer.serializeKey(name) state = serializer.serializeValue(val) } @@ -17,7 +18,7 @@ function serializeEntries>(serializer: ObjectSerializer, value: R, options?: SerdeOptions) { - return serializeEntries(serializer, Object.entries(value) as Iterable<[K, V]>) + return serializeEntries(serializer, Object.entries(value) as Iterable<[K, V]>, options) } function serializeIter>(serializer: IterableSerializer, value: V) { diff --git a/src/ser/mixin.ts b/src/ser/mixin.ts index 6d2a5c8..bfef85e 100644 --- a/src/ser/mixin.ts +++ b/src/ser/mixin.ts @@ -1,9 +1,10 @@ +import { Serde } from '../decorator' import { Constructor } from '../utils' import { serializeWith } from './impl' import { isGenericSerializer, Serializer } from './interface' export function serialize(constructor: T) { - return class Serializable extends constructor implements Serializable { + class Serializable extends constructor implements Serializable { static name = constructor.name serialize(serializer: Serializer): U { // shortcut for serializers with only the serializeAny method @@ -14,4 +15,8 @@ export function serialize(constructor: T) { } } } + + Serializable[Serde] = (constructor as any)[Serde] + + return Serializable } diff --git a/tsconfig.json b/tsconfig.json index fd0def4..80eabcd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,8 +15,8 @@ "lib": ["es2020", "dom"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "libReplacement": true, /* Enable lib replacement. */ - // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ - // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + //"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */