@roit/roit-model-mapper
Version:
ROIT model mapper makes it easy to convert any object or JSON to the model
25 lines (19 loc) • 837 B
text/typescript
import { ObjectMapperOptions } from "./ObjectMapperOptions";
import { MapperHandle } from "./MapperHandle";
export class ModelMapper {
private jsonObject: any
private optionsMapper: ObjectMapperOptions
setJsonObject(jsonObject: any) {
this.jsonObject = jsonObject
}
bodyToObject<T>(clazz: { new(): T }, optionsMapper?: ObjectMapperOptions) {
return MapperHandle.deserialize(clazz, this.jsonObject, optionsMapper ? optionsMapper : this.optionsMapper)
}
static deserialize<T>(clazz: { new(): T }, jsonObject: any, optionsMapper?: ObjectMapperOptions) {
const result = MapperHandle.deserialize(clazz, jsonObject, optionsMapper)
if(!result && optionsMapper && optionsMapper.defaultValue) {
return optionsMapper.defaultValue
}
return result
}
}