ts-serializable 4.3.2 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -298,7 +298,9 @@ The library provides standalone utility functions `fromJSON` and `toJSON` that c
298
298
 
299
299
  fromJSON Function:
300
300
 
301
- The `fromJSON` function populates an existing object instance with data from JSON, using decorator metadata for type conversion.
301
+ The `fromJSON` function deserializes JSON data into a class instance. It can accept either an existing object instance or a class constructor.
302
+
303
+ **Usage with instance:**
302
304
 
303
305
  ```typescript
304
306
  import { fromJSON, jsonProperty } from "ts-serializable";
@@ -320,6 +322,7 @@ const json = {
320
322
  releaseDate: "2024-01-15T10:00:00.000Z"
321
323
  };
322
324
 
325
+ // Pass an existing instance
323
326
  const product = new Product();
324
327
  fromJSON(product, json);
325
328
 
@@ -328,13 +331,26 @@ console.log(product.price); // 999.99
328
331
  console.log(product.releaseDate instanceof Date); // true
329
332
  ```
330
333
 
334
+ **Usage with class constructor:**
335
+
336
+ ```typescript
337
+ // Pass a class constructor - the function will create an instance automatically
338
+ const product = fromJSON(Product, json);
339
+
340
+ console.log(product instanceof Product); // true
341
+ console.log(product.name); // "Laptop"
342
+ console.log(product.price); // 999.99
343
+ ```
344
+
331
345
  Benefits:
332
346
 
333
347
  - Works with plain classes (no need to extend `Serializable`)
348
+ - Accepts both instance and constructor for flexibility
334
349
  - Respects all decorators (`@jsonProperty`, `@jsonName`, `@jsonIgnore`)
335
350
  - Supports naming strategies
336
351
  - Handles nested objects and arrays
337
352
  - Type-safe deserialization
353
+ - Perfect for generic programming patterns
338
354
 
339
355
  toJSON Function:
340
356
 
@@ -398,13 +414,34 @@ class ApiRequest {
398
414
  public userTags: string[] = [];
399
415
  }
400
416
 
401
- // Deserialize from API response
417
+ // Deserialize from API response using constructor
402
418
  const apiData = {
403
419
  request_id: "REQ-12345",
404
420
  user_name: "john_doe",
405
421
  user_tags: ["premium", "verified"]
406
422
  };
407
423
 
424
+ // Using class constructor - creates new instance automatically
425
+ const request = fromJSON(ApiRequest, apiData);
426
+
427
+ console.log(request instanceof ApiRequest); // true
428
+ console.log(request.requestId); // "REQ-12345"
429
+ console.log(request.userName); // "john_doe"
430
+
431
+ // Serialize for sending to API
432
+ const jsonToSend = toJSON(request);
433
+ console.log(jsonToSend);
434
+ // Output: {
435
+ // request_id: "REQ-12345",
436
+ // user_name: "john_doe",
437
+ // user_tags: ["premium", "verified"]
438
+ // }
439
+ ```
440
+
441
+ **Alternative approach using instance:**
442
+
443
+ ```typescript
444
+ // Using instance
408
445
  const request = new ApiRequest();
409
446
  fromJSON(request, apiData);
410
447
 
@@ -822,9 +859,22 @@ console.log(team.members[0] instanceof User); // true
822
859
 
823
860
  ### Standalone Functions
824
861
 
825
- - **`fromJSON<T>(obj: T, json: object, settings?: Partial<SerializationSettings>): T`**
862
+ - **`fromJSON<T>(obj: T | (new () => T), json: object, settings?: Partial<SerializationSettings>): T`**
863
+
864
+ Deserializes JSON into an object instance. Accepts either:
865
+ - An existing object instance to populate
866
+ - A class constructor to create a new instance
867
+
868
+ **Examples:**
869
+
870
+ ```typescript
871
+ // With instance
872
+ const product = new Product();
873
+ fromJSON(product, jsonData);
826
874
 
827
- Deserializes JSON into an existing object instance.
875
+ // With constructor
876
+ const product = fromJSON(Product, jsonData);
877
+ ```
828
878
 
829
879
  - **`toJSON(obj: Serializable | object): Record<string, unknown>`**
830
880
 
@@ -53,4 +53,4 @@ import { SerializationSettings } from "../models/SerializationSettings.js";
53
53
  * fromJSON(product, { title: "Laptop" });
54
54
  * ```
55
55
  */
56
- export declare const fromJSON: <T extends (Serializable | object)>(obj: T, json: object, settings?: Partial<SerializationSettings>) => T;
56
+ export declare const fromJSON: <T extends (Serializable | object)>(obj: T | (new () => T), json: object, settings?: Partial<SerializationSettings>) => T;
@@ -58,9 +58,14 @@ import { onWrongType } from "./OnWrongType.js";
58
58
  * fromJSON(product, { title: "Laptop" });
59
59
  * ```
60
60
  */
61
- // eslint-disable-next-line max-statements
61
+ // eslint-disable-next-line max-statements, max-lines-per-function
62
62
  export const fromJSON = (obj, json, settings) => {
63
63
  const unknownJson = json;
64
+ if (typeof obj === "function") {
65
+ const ClassConstructor = obj;
66
+ // eslint-disable-next-line no-param-reassign
67
+ obj = new ClassConstructor();
68
+ }
64
69
  if (unknownJson === null ||
65
70
  Array.isArray(unknownJson) ||
66
71
  typeof unknownJson !== "object") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-serializable",
3
- "version": "4.3.2",
3
+ "version": "4.4.0",
4
4
  "author": "Eugene Labutin",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/LabEG/Serializable#readme",