|
8 | 8 |
|
9 | 9 | import type {AcceptedTypes} from "../models/AcceptedType.js"; |
10 | 10 | import {SerializationSettings} from "../models/SerializationSettings.js"; |
| 11 | +import {classToFormData} from "../utils/ClassToFormData.js"; |
| 12 | +import {getPropertyName} from "../utils/GetProperyName.js"; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * Class how help you deserialize object to classes. |
@@ -136,22 +138,43 @@ export class Serializable { |
136 | 138 | * @memberof Serializable |
137 | 139 | */ |
138 | 140 | public toJSON (): Record<string, unknown> { |
139 | | - const fromJson: this = {...this}; |
140 | 141 | const toJson: Record<string, unknown> = {}; |
| 142 | + const keys = Reflect.ownKeys(this); |
141 | 143 |
|
142 | | - for (const prop in fromJson) { |
143 | | - // Json.hasOwnProperty(prop) - preserve for deserialization for other classes with methods |
144 | | - if (fromJson.hasOwnProperty(prop) && this.hasOwnProperty(prop)) { |
145 | | - if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, prop) !== true) { |
146 | | - const toProp = this.getJsonPropertyName(prop); |
147 | | - Reflect.set(toJson, toProp, Reflect.get(fromJson, prop)); |
| 144 | + for (const key of keys) { |
| 145 | + if (typeof key === "symbol") { |
| 146 | + // eslint-disable-next-line no-continue |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + if (this.hasOwnProperty(key)) { |
| 151 | + if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, key) !== true) { |
| 152 | + const toProp = this.getJsonPropertyName(key); |
| 153 | + Reflect.set(toJson, toProp, Reflect.get(this, key)); |
148 | 154 | } |
149 | 155 | } |
150 | 156 | } |
151 | 157 |
|
152 | 158 | return toJson; |
153 | 159 | } |
154 | 160 |
|
| 161 | + /** |
| 162 | + * Serialize class to FormData. |
| 163 | + * |
| 164 | + * Can be used for prepare ajax form with files. |
| 165 | + * Send files via ajax json its heavy task, because need convert file to base 64 format, |
| 166 | + * user interface can be freeze on many seconds on this operation if file is too big. |
| 167 | + * Ajax forms its lightweight alternative. |
| 168 | + * |
| 169 | + * @param {string} formPrefix Prefix for form property names |
| 170 | + * @param {FormData} formData Can be update an existing FormData |
| 171 | + * @returns {FormData} |
| 172 | + * @memberof Serializable |
| 173 | + */ |
| 174 | + public toFormData (formPrefix?: string, formData?: FormData): FormData { |
| 175 | + return classToFormData(this, formPrefix, formData); |
| 176 | + } |
| 177 | + |
155 | 178 | /** |
156 | 179 | * Process serialization for @jsonIgnore decorator |
157 | 180 | * |
@@ -288,29 +311,16 @@ export class Serializable { |
288 | 311 | return Reflect.get(this, prop); |
289 | 312 | } |
290 | 313 |
|
291 | | - protected getJsonPropertyName (thisProperty: string, settings?: Partial<SerializationSettings>): string { |
292 | | - if (Reflect.hasMetadata("ts-serializable:jsonName", this.constructor.prototype, thisProperty)) { |
293 | | - return Reflect.getMetadata("ts-serializable:jsonName", this.constructor.prototype, thisProperty) as string; |
294 | | - } |
295 | | - |
296 | | - if (settings?.namingStrategy) { |
297 | | - return settings.namingStrategy.toJsonName(thisProperty); |
298 | | - } |
299 | | - |
300 | | - if (Reflect.hasMetadata("ts-serializable:jsonObject", this.constructor)) { |
301 | | - const objectSettings: Partial<SerializationSettings> = Reflect.getMetadata( |
302 | | - "ts-serializable:jsonObject", |
303 | | - this.constructor |
304 | | - ) as Partial<SerializationSettings>; |
305 | | - return objectSettings.namingStrategy?.toJsonName(thisProperty) ?? thisProperty; |
306 | | - } |
307 | | - |
308 | | - if (Serializable.defaultSettings.namingStrategy) { |
309 | | - const {namingStrategy} = Serializable.defaultSettings; |
310 | | - return namingStrategy.toJsonName(thisProperty) ?? thisProperty; |
311 | | - } |
312 | | - |
313 | | - return thisProperty; |
| 314 | + /** |
| 315 | + * Extract correct name for property. |
| 316 | + * Considers decorators for transforming the property name. |
| 317 | + * |
| 318 | + * @param {string} property Source name of property |
| 319 | + * @param {Partial<SerializationSettings>} settings Serialization settings |
| 320 | + * @returns |
| 321 | + */ |
| 322 | + protected getJsonPropertyName (property: string, settings?: Partial<SerializationSettings>): string { |
| 323 | + return getPropertyName(this, property, settings); |
314 | 324 | } |
315 | 325 |
|
316 | 326 | } |
0 commit comments