skip serializing option
This commit is contained in:
parent
0c654000ec
commit
dec78265a3
2 changed files with 48 additions and 12 deletions
|
@ -1,7 +1,7 @@
|
|||
import { CaseConvention, convertCase } from './case'
|
||||
import { Deserializer } from './de'
|
||||
import { Serializer } from './ser'
|
||||
import { isFunction, isNumber, isString, Morphism, Nullable } from './utils'
|
||||
import { isFunction, isNumber, isString } from './utils'
|
||||
|
||||
|
||||
export interface RenameOptions {
|
||||
|
@ -47,7 +47,7 @@ export interface PropertyOptions {
|
|||
rename?: RenameOptions | string
|
||||
//serializeWith?: CustomSerializer
|
||||
//deserializeWith: CustomDeserializer
|
||||
skip?: SkipOptions | boolean
|
||||
skip?: SkipOptions | ConditionalSkipOptions | boolean
|
||||
}
|
||||
|
||||
export const Stage = Object.freeze({
|
||||
|
@ -133,23 +133,55 @@ export class SerdeOptions {
|
|||
}
|
||||
}
|
||||
|
||||
getCustomImpl(property: string, stage: Stage) {
|
||||
//getCustomImpl(property: string, stage: Stage) {
|
||||
// const options = this.properties.get(property)
|
||||
// if (options != null) {
|
||||
// if (stage === Stage.Serialize && isFunction(options.serializeWith)) {
|
||||
// return options.serializeWith
|
||||
// } else if (stage === Stage.Deserialize && isFunction(options.deserializeWith)) {
|
||||
// return options.deserializeWith
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//getSerializer(property: string): Nullable<CustomSerializer> {
|
||||
// return this.getCustomImpl(property, Stage.Serialize) as CustomSerializer
|
||||
//}
|
||||
|
||||
//getDeserializer(property: string): Nullable<CustomDeserializer> {
|
||||
// return this.getCustomImpl(property, Stage.Deserialize) as CustomDeserializer
|
||||
//}
|
||||
|
||||
private isConditionalSkip(skip: any): skip is ConditionalSkipOptions {
|
||||
return 'if' in skip && isFunction(skip.if)
|
||||
}
|
||||
|
||||
shouldSkip(property: string, value: any, stage: Stage): boolean {
|
||||
const options = this.properties.get(property)
|
||||
if (options != null) {
|
||||
if (stage === Stage.Serialize && isFunction(options.serializeWith)) {
|
||||
return options.serializeWith
|
||||
} else if (stage === Stage.Deserialize && isFunction(options.deserializeWith)) {
|
||||
return options.deserializeWith
|
||||
if (options != null && options.skip != null) {
|
||||
if (typeof options.skip === 'boolean') {
|
||||
return options.skip
|
||||
} else if (this.isConditionalSkip(options.skip)) {
|
||||
return options.skip.if(value)
|
||||
} else if (stage === Stage.Serialize && typeof options.skip.serializing === 'boolean') {
|
||||
return options.skip.serializing
|
||||
} else if (stage === Stage.Serialize && this.isConditionalSkip(options.skip.serializing)) {
|
||||
return options.skip.serializing.if(value)
|
||||
} else if (stage === Stage.Deserialize && typeof options.skip.deserializing === 'boolean') {
|
||||
return options.skip.deserializing
|
||||
} else if (stage === Stage.Deserialize && this.isConditionalSkip(options.skip.deserializing)) {
|
||||
return options.skip.deserializing.if(value)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
getSerializer(property: string): Nullable<CustomSerializer> {
|
||||
return this.getCustomImpl(property, Stage.Serialize) as CustomSerializer
|
||||
shouldSkipSerializing(property: string, value: any): boolean {
|
||||
return this.shouldSkip(property, value, Stage.Serialize)
|
||||
}
|
||||
|
||||
getDeserializer(property: string): Nullable<CustomDeserializer> {
|
||||
return this.getCustomImpl(property, Stage.Deserialize) as CustomDeserializer
|
||||
shouldSkipDeserializing(property: string, value: any): boolean {
|
||||
return this.shouldSkip(property, value, Stage.Deserialize)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,10 @@ function serializeEntries<T, K extends string, V extends Serializable, E extends
|
|||
const objectSerializer = serializer.serializeObject!()
|
||||
|
||||
for (const [key, val] of value) {
|
||||
if (options?.shouldSkipSerializing(key, val)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const name = options?.getPropertyName(key as string, Stage.Serialize) ?? key
|
||||
state = objectSerializer.serializeKey(name)
|
||||
state = objectSerializer.serializeValue(val)
|
||||
|
|
Loading…
Add table
Reference in a new issue