From d668510a9149ba57622ac3c4090f4516f3669ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Wed, 4 Jul 2018 22:46:24 +0200 Subject: [PATCH 1/8] added propSchema customAsync. It allows asynchronous custom property deserialization. Especially, it allows to wait for resolution of references inside custom properties. --- CHANGELOG.md | 4 ++ README.md | 94 ++++++++++++++++++++++++++++++---------- package.json | 4 +- serializr.d.ts | 6 ++- src/serializr.js | 1 + src/types/customAsync.js | 53 ++++++++++++++++++++++ 6 files changed, 136 insertions(+), 26 deletions(-) create mode 100644 src/types/customAsync.js diff --git a/CHANGELOG.md b/CHANGELOG.md index cf0b0dc..1f82ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.2.1 + +* Introduced `customAsync`, to support asynchronous custom deserialization + # 1.1.12 * Improved documentation examples, see #43 by @brikou diff --git a/README.md b/README.md index b864029..5ca4d37 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl ## ModelSchema -[src/serializr.js:52-52](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/serializr.js#L52-L52 "Source code on GitHub") +[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/serializr.js#L52-L52 "Source code on GitHub") JSDOC type defintions for usage w/o typescript. @@ -341,7 +341,7 @@ Returns **any** void ## createSimpleSchema -[src/api/createSimpleSchema.js:17-24](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") +[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") Creates a model schema that (de)serializes from / to plain javascript objects. Its factory method is: `() => ({})` @@ -366,7 +366,7 @@ Returns **[object](#object)** model schema ## createModelSchema -[src/api/createModelSchema.js:29-47](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") +[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") Creates a model schema that (de)serializes an object created by a constructor function (class). The created model schema is associated by the targeted type as default model schema, see setDefaultModelSchema. @@ -399,7 +399,7 @@ Returns **[object](#object)** model schema ## getDefaultModelSchema -[src/api/getDefaultModelSchema.js:9-18](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") +[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") Returns the standard model schema associated with a class / constructor function @@ -411,7 +411,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## setDefaultModelSchema -[src/api/setDefaultModelSchema.js:15-18](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") +[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") Sets the default model schema for class / constructor function. Everywhere where a model schema is required as argument, this class / constructor function @@ -429,7 +429,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## serializable -[src/api/serializable.js:93-103](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/api/serializable.js#L93-L103 "Source code on GitHub") +[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/serializable.js#L93-L103 "Source code on GitHub") Decorator that defines a new property mapping on the default model schema for the class it is used in. @@ -453,7 +453,7 @@ Returns **PropertyDescriptor** ## serialize -[src/core/serialize.js:16-34](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/serialize.js#L16-L34 "Source code on GitHub") +[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/serialize.js#L16-L34 "Source code on GitHub") Serializes an object (graph) into json using the provided model schema. The model schema can be omitted if the object type has a default model schema associated with it. @@ -468,7 +468,7 @@ Returns **[object](#object)** serialized representation of the object ## serializeAll -[src/core/serialize.js:89-100](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/serialize.js#L89-L100 "Source code on GitHub") +[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/serialize.js#L89-L100 "Source code on GitHub") The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. @@ -478,7 +478,7 @@ The `serializeAll` decorator can be used on a class to signal that all primitive ## deserialize -[src/core/deserialize.js:38-56](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/deserialize.js#L38-L56 "Source code on GitHub") +[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/deserialize.js#L38-L56 "Source code on GitHub") Deserializes a json structor into an object graph. This process might be asynchronous (for example if there are references with an asynchronous @@ -497,7 +497,7 @@ Returns **([object](#object) \| [array](https://developer.mozilla.org/en-US/docs ## update -[src/core/update.js:21-42](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/core/update.js#L21-L42 "Source code on GitHub") +[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/update.js#L21-L42 "Source code on GitHub") Similar to deserialize, but updates an existing object instance. Properties will always updated entirely, but properties not present in the json will be kept as is. @@ -513,7 +513,7 @@ Further this method behaves similar to deserialize. ## primitive -[src/types/primitive.js:17-29](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/primitive.js#L17-L29 "Source code on GitHub") +[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/primitive.js#L17-L29 "Source code on GitHub") Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. @@ -532,7 +532,7 @@ Returns **[ModelSchema](#modelschema)** ## identifier -[src/types/identifier.js:42-56](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/identifier.js#L42-L56 "Source code on GitHub") +[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/identifier.js#L42-L56 "Source code on GitHub") Similar to primitive, but this field will be marked as the identifier for the given Model type. This is used by for example `reference()` to serialize the reference @@ -572,13 +572,13 @@ Returns **PropSchema** ## date -[src/types/date.js:8-23](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/date.js#L8-L23 "Source code on GitHub") +[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/date.js#L8-L23 "Source code on GitHub") Similar to primitive, serializes instances of Date objects ## alias -[src/types/alias.js:20-31](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/alias.js#L20-L31 "Source code on GitHub") +[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/alias.js#L20-L31 "Source code on GitHub") Alias indicates that this model property should be named differently in the generated json. Alias should be the outermost propschema. @@ -603,7 +603,7 @@ Returns **PropSchema** ## custom -[src/types/custom.js:36-45](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/custom.js#L36-L45 "Source code on GitHub") +[src/types/custom.js:36-45](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/custom.js#L36-L45 "Source code on GitHub") Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -643,9 +643,59 @@ t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }); Returns **PropSchema** +## customAsync + +[src/types/customAsync.js:44-53](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/customAsync.js#L44-L53 "Source code on GitHub") + +Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. +In comparison to the 'custom' propSchema it is possible to wait for resolution of references during the deserialization process. The context.await function can + +The `customAsync` function takes two parameters, the `serializer` function and the `deserializer` function. + +The `serializer` function has the signature: +`(value, key, obj) => void` + +When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. + +The `deserializer` function has the signature: +`(value, callback, context, oldValue) => void` + +The `callback` function has the signature: +`(error, result) => void` + +When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, callback, contextObj, oldValue)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)). + +**Parameters** + +- `serializer` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that takes a model value and turns it into a json value +- `deserializer` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that takes a json value and turns it into a model value. It also receives a callback function to notify the caller about the completion of the deserialization. It also takes context argument, which can allow you to deserialize based on the context of other parameters. + +**Examples** + +```javascript +var schema = _.createSimpleSchema({ + a: _.customAsync( + function(v) { + return v + 2; + }, + function(v, callback, context, oldValue) { + somePromise(v, context, oldValue).then((result) => { + callback(null, result) + }.catch((err) => { + callback(err) + } + } + ), +}); +t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); +t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }); +``` + +Returns **PropSchema** + ## object -[src/types/object.js:34-52](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/object.js#L34-L52 "Source code on GitHub") +[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/object.js#L34-L52 "Source code on GitHub") `object` indicates that this property contains an object that needs to be (de)serialized using its own model schema. @@ -682,7 +732,7 @@ Returns **PropSchema** ## reference -[src/types/reference.js:65-98](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/reference.js#L65-L98 "Source code on GitHub") +[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/reference.js#L65-L98 "Source code on GitHub") `reference` can be used to (de)serialize references that point to other models. @@ -746,7 +796,7 @@ Returns **PropSchema** ## list -[src/types/list.js:33-54](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/list.js#L33-L54 "Source code on GitHub") +[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/list.js#L33-L54 "Source code on GitHub") List indicates that this property contains a list of things. Accepts a sub model schema to serialize the contents @@ -784,7 +834,7 @@ Returns **PropSchema** ## map -[src/types/map.js:13-62](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/map.js#L13-L62 "Source code on GitHub") +[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/map.js#L13-L62 "Source code on GitHub") Similar to list, but map represents a string keyed dynamic collection. This can be both plain objects (default) or ES6 Map like structures. @@ -796,7 +846,7 @@ This will be inferred from the initial value of the targetted attribute. ## mapAsArray -[src/types/mapAsArray.js:15-52](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") +[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the Customer object has the id stored on itself. @@ -811,7 +861,7 @@ For ES6 maps this has the benefit of being allowed to have non-string keys in th ## raw -[src/types/raw.js:15-24](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/raw.js#L15-L24 "Source code on GitHub") +[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/raw.js#L15-L24 "Source code on GitHub") Indicates that this field is only need to putted in the serialized json or deserialized instance, without any transformations. Stay with its original value @@ -831,7 +881,7 @@ Returns **[ModelSchema](#modelschema)** ## SKIP -[src/constants.js:20-20](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/constants.js#L20-L20 "Source code on GitHub") +[src/constants.js:20-20](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/constants.js#L20-L20 "Source code on GitHub") In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. diff --git a/package.json b/package.json index a88e812..4e46853 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serializr", - "version": "1.2.0", + "version": "1.2.1", "description": "Serialize and deserialize complex object graphs to JSON", "main": "lib/serializr.js", "module": "lib/es/serializr.js", @@ -57,4 +57,4 @@ "typescript": "^2.1.4", "uglify-js": "^2.6.4" } -} \ No newline at end of file +} diff --git a/serializr.d.ts b/serializr.d.ts index 99168bd..0faa36a 100644 --- a/serializr.d.ts +++ b/serializr.d.ts @@ -71,8 +71,10 @@ export function map(propSchema: PropSchema): PropSchema; export function mapAsArray(propSchema: PropSchema, keyPropertyName: string): PropSchema; -export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any) => any): PropSchema; +export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context?: any, oldValue?: any) => any): PropSchema; + +export function customAsync(serializer: (value: any) => any, deserializer: (jsonValue: any, callback: (err: any, result: any) => void, context?: any, oldValue?: any) => any): PropSchema; export function serializeAll(clazz: T): T -export const SKIP: {} \ No newline at end of file +export const SKIP: {} diff --git a/src/serializr.js b/src/serializr.js index 270f0ad..b18166d 100644 --- a/src/serializr.js +++ b/src/serializr.js @@ -67,6 +67,7 @@ export { default as identifier } from "./types/identifier" export { default as date } from "./types/date" export { default as alias } from "./types/alias" export { default as custom } from "./types/custom" +export { default as customAsync } from "./types/customAsync" export { default as object } from "./types/object" export { default as reference } from "./types/reference" export { default as list } from "./types/list" diff --git a/src/types/customAsync.js b/src/types/customAsync.js new file mode 100644 index 0000000..4bc1902 --- /dev/null +++ b/src/types/customAsync.js @@ -0,0 +1,53 @@ +import { invariant } from "../utils/utils" + +/** + * Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. + * In comparison to the 'custom' propSchema it is possible to wait for resolution of references during the deserialization process. The context.await function can + + * The `customAsync` function takes two parameters, the `serializer` function and the `deserializer` function. + + * The `serializer` function has the signature: + * `(value, key, obj) => void` + + * When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. + + * The `deserializer` function has the signature: + * `(value, callback, context, oldValue) => void` + + * The `callback` function has the signature: + * `(error, result) => void` + + * When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, callback, contextObj, oldValue)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)). + * + * @example + * var schema = _.createSimpleSchema({ + * a: _.customAsync( + * function(v) { + * return v + 2; + * }, + * function(v, callback, context, oldValue) { + * somePromise(v, context, oldValue).then((result) => { + * callback(null, result) + * }.catch((err) => { + * callback(err) + * } + * } + * ), + * }); + * t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); + * t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }); + * + * @param {function} serializer function that takes a model value and turns it into a json value + * @param {function} deserializer function that takes a json value and turns it into a model value. It also receives a callback function to notify the caller about the completion of the deserialization. It also takes context argument, which can allow you to deserialize based on the context of other parameters. + * @returns {PropSchema} + */ +export default function customAsync(serializer, deserializer) { + invariant(typeof serializer === "function", "first argument should be function") + invariant(typeof deserializer === "function", "second argument should be function") + return { + serializer: serializer, + deserializer: function (jsonValue, done, context, oldValue) { + deserializer(jsonValue, done, context, oldValue) + } + } +} From 2b154f7156a77360e9babbd368dd075a646d4a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Fri, 6 Jul 2018 23:34:47 +0200 Subject: [PATCH 2/8] added propSchema customAsync. It allows asynchronous custom property deserialization. Especially, it allows to wait for resolution of references inside custom properties. --- README.md | 48 ++++++++++++++++++++-------------------- src/types/customAsync.js | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 5ca4d37..16bdae3 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl ## ModelSchema -[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/serializr.js#L52-L52 "Source code on GitHub") +[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/serializr.js#L52-L52 "Source code on GitHub") JSDOC type defintions for usage w/o typescript. @@ -341,7 +341,7 @@ Returns **any** void ## createSimpleSchema -[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") +[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") Creates a model schema that (de)serializes from / to plain javascript objects. Its factory method is: `() => ({})` @@ -366,7 +366,7 @@ Returns **[object](#object)** model schema ## createModelSchema -[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") +[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") Creates a model schema that (de)serializes an object created by a constructor function (class). The created model schema is associated by the targeted type as default model schema, see setDefaultModelSchema. @@ -399,7 +399,7 @@ Returns **[object](#object)** model schema ## getDefaultModelSchema -[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") +[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") Returns the standard model schema associated with a class / constructor function @@ -411,7 +411,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## setDefaultModelSchema -[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") +[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") Sets the default model schema for class / constructor function. Everywhere where a model schema is required as argument, this class / constructor function @@ -429,7 +429,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## serializable -[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/api/serializable.js#L93-L103 "Source code on GitHub") +[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/serializable.js#L93-L103 "Source code on GitHub") Decorator that defines a new property mapping on the default model schema for the class it is used in. @@ -453,7 +453,7 @@ Returns **PropertyDescriptor** ## serialize -[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/serialize.js#L16-L34 "Source code on GitHub") +[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/serialize.js#L16-L34 "Source code on GitHub") Serializes an object (graph) into json using the provided model schema. The model schema can be omitted if the object type has a default model schema associated with it. @@ -468,7 +468,7 @@ Returns **[object](#object)** serialized representation of the object ## serializeAll -[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/serialize.js#L89-L100 "Source code on GitHub") +[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/serialize.js#L89-L100 "Source code on GitHub") The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. @@ -478,7 +478,7 @@ The `serializeAll` decorator can be used on a class to signal that all primitive ## deserialize -[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/deserialize.js#L38-L56 "Source code on GitHub") +[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/deserialize.js#L38-L56 "Source code on GitHub") Deserializes a json structor into an object graph. This process might be asynchronous (for example if there are references with an asynchronous @@ -497,7 +497,7 @@ Returns **([object](#object) \| [array](https://developer.mozilla.org/en-US/docs ## update -[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/core/update.js#L21-L42 "Source code on GitHub") +[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/update.js#L21-L42 "Source code on GitHub") Similar to deserialize, but updates an existing object instance. Properties will always updated entirely, but properties not present in the json will be kept as is. @@ -513,7 +513,7 @@ Further this method behaves similar to deserialize. ## primitive -[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/primitive.js#L17-L29 "Source code on GitHub") +[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/primitive.js#L17-L29 "Source code on GitHub") Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. @@ -532,7 +532,7 @@ Returns **[ModelSchema](#modelschema)** ## identifier -[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/identifier.js#L42-L56 "Source code on GitHub") +[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/identifier.js#L42-L56 "Source code on GitHub") Similar to primitive, but this field will be marked as the identifier for the given Model type. This is used by for example `reference()` to serialize the reference @@ -572,13 +572,13 @@ Returns **PropSchema** ## date -[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/date.js#L8-L23 "Source code on GitHub") +[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/date.js#L8-L23 "Source code on GitHub") Similar to primitive, serializes instances of Date objects ## alias -[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/alias.js#L20-L31 "Source code on GitHub") +[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/alias.js#L20-L31 "Source code on GitHub") Alias indicates that this model property should be named differently in the generated json. Alias should be the outermost propschema. @@ -603,7 +603,7 @@ Returns **PropSchema** ## custom -[src/types/custom.js:36-45](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/custom.js#L36-L45 "Source code on GitHub") +[src/types/custom.js:36-45](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/custom.js#L36-L45 "Source code on GitHub") Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -645,10 +645,10 @@ Returns **PropSchema** ## customAsync -[src/types/customAsync.js:44-53](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/customAsync.js#L44-L53 "Source code on GitHub") +[src/types/customAsync.js:44-53](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/customAsync.js#L44-L53 "Source code on GitHub") Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. -In comparison to the 'custom' propSchema it is possible to wait for resolution of references during the deserialization process. The context.await function can +In comparison to the 'custom' propSchema it is possible to wait for asynchronous deserialization steps. This allows e.g. resolution of references during the deserialization process (using context.await.. The `customAsync` function takes two parameters, the `serializer` function and the `deserializer` function. @@ -695,7 +695,7 @@ Returns **PropSchema** ## object -[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/object.js#L34-L52 "Source code on GitHub") +[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/object.js#L34-L52 "Source code on GitHub") `object` indicates that this property contains an object that needs to be (de)serialized using its own model schema. @@ -732,7 +732,7 @@ Returns **PropSchema** ## reference -[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/reference.js#L65-L98 "Source code on GitHub") +[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/reference.js#L65-L98 "Source code on GitHub") `reference` can be used to (de)serialize references that point to other models. @@ -796,7 +796,7 @@ Returns **PropSchema** ## list -[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/list.js#L33-L54 "Source code on GitHub") +[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/list.js#L33-L54 "Source code on GitHub") List indicates that this property contains a list of things. Accepts a sub model schema to serialize the contents @@ -834,7 +834,7 @@ Returns **PropSchema** ## map -[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/map.js#L13-L62 "Source code on GitHub") +[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/map.js#L13-L62 "Source code on GitHub") Similar to list, but map represents a string keyed dynamic collection. This can be both plain objects (default) or ES6 Map like structures. @@ -846,7 +846,7 @@ This will be inferred from the initial value of the targetted attribute. ## mapAsArray -[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") +[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the Customer object has the id stored on itself. @@ -861,7 +861,7 @@ For ES6 maps this has the benefit of being allowed to have non-string keys in th ## raw -[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/types/raw.js#L15-L24 "Source code on GitHub") +[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/raw.js#L15-L24 "Source code on GitHub") Indicates that this field is only need to putted in the serialized json or deserialized instance, without any transformations. Stay with its original value @@ -881,7 +881,7 @@ Returns **[ModelSchema](#modelschema)** ## SKIP -[src/constants.js:20-20](https://github.com/evoye/serializr/blob/b5101dd812e4b2d245bfda4ffcc343c76014f96a/src/constants.js#L20-L20 "Source code on GitHub") +[src/constants.js:20-20](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/constants.js#L20-L20 "Source code on GitHub") In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. diff --git a/src/types/customAsync.js b/src/types/customAsync.js index 4bc1902..b6f740b 100644 --- a/src/types/customAsync.js +++ b/src/types/customAsync.js @@ -2,7 +2,7 @@ import { invariant } from "../utils/utils" /** * Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. - * In comparison to the 'custom' propSchema it is possible to wait for resolution of references during the deserialization process. The context.await function can + * In comparison to the 'custom' propSchema it is possible to wait for asynchronous deserialization steps. This allows e.g. resolution of references during the deserialization process (using context.await.. * The `customAsync` function takes two parameters, the `serializer` function and the `deserializer` function. From d9c6a398d8b8fc3471fc4270b5df05bde221d05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Sat, 7 Jul 2018 08:32:21 +0200 Subject: [PATCH 3/8] added test coverage for custom and customAsync propSchema --- README.md | 52 ++++++++++---------- src/types/customAsync.js | 6 ++- test/typescript/ts.ts | 101 +++++++++++++++++++++++++++++---------- 3 files changed, 106 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 16bdae3..737de9b 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl ## ModelSchema -[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/serializr.js#L52-L52 "Source code on GitHub") +[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/serializr.js#L52-L52 "Source code on GitHub") JSDOC type defintions for usage w/o typescript. @@ -341,7 +341,7 @@ Returns **any** void ## createSimpleSchema -[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") +[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") Creates a model schema that (de)serializes from / to plain javascript objects. Its factory method is: `() => ({})` @@ -366,7 +366,7 @@ Returns **[object](#object)** model schema ## createModelSchema -[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") +[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") Creates a model schema that (de)serializes an object created by a constructor function (class). The created model schema is associated by the targeted type as default model schema, see setDefaultModelSchema. @@ -399,7 +399,7 @@ Returns **[object](#object)** model schema ## getDefaultModelSchema -[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") +[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") Returns the standard model schema associated with a class / constructor function @@ -411,7 +411,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## setDefaultModelSchema -[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") +[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") Sets the default model schema for class / constructor function. Everywhere where a model schema is required as argument, this class / constructor function @@ -429,7 +429,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## serializable -[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/api/serializable.js#L93-L103 "Source code on GitHub") +[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/serializable.js#L93-L103 "Source code on GitHub") Decorator that defines a new property mapping on the default model schema for the class it is used in. @@ -453,7 +453,7 @@ Returns **PropertyDescriptor** ## serialize -[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/serialize.js#L16-L34 "Source code on GitHub") +[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/serialize.js#L16-L34 "Source code on GitHub") Serializes an object (graph) into json using the provided model schema. The model schema can be omitted if the object type has a default model schema associated with it. @@ -468,7 +468,7 @@ Returns **[object](#object)** serialized representation of the object ## serializeAll -[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/serialize.js#L89-L100 "Source code on GitHub") +[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/serialize.js#L89-L100 "Source code on GitHub") The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. @@ -478,7 +478,7 @@ The `serializeAll` decorator can be used on a class to signal that all primitive ## deserialize -[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/deserialize.js#L38-L56 "Source code on GitHub") +[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/deserialize.js#L38-L56 "Source code on GitHub") Deserializes a json structor into an object graph. This process might be asynchronous (for example if there are references with an asynchronous @@ -497,7 +497,7 @@ Returns **([object](#object) \| [array](https://developer.mozilla.org/en-US/docs ## update -[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/core/update.js#L21-L42 "Source code on GitHub") +[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/update.js#L21-L42 "Source code on GitHub") Similar to deserialize, but updates an existing object instance. Properties will always updated entirely, but properties not present in the json will be kept as is. @@ -513,7 +513,7 @@ Further this method behaves similar to deserialize. ## primitive -[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/primitive.js#L17-L29 "Source code on GitHub") +[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/primitive.js#L17-L29 "Source code on GitHub") Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. @@ -532,7 +532,7 @@ Returns **[ModelSchema](#modelschema)** ## identifier -[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/identifier.js#L42-L56 "Source code on GitHub") +[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/identifier.js#L42-L56 "Source code on GitHub") Similar to primitive, but this field will be marked as the identifier for the given Model type. This is used by for example `reference()` to serialize the reference @@ -572,13 +572,13 @@ Returns **PropSchema** ## date -[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/date.js#L8-L23 "Source code on GitHub") +[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/date.js#L8-L23 "Source code on GitHub") Similar to primitive, serializes instances of Date objects ## alias -[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/alias.js#L20-L31 "Source code on GitHub") +[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/alias.js#L20-L31 "Source code on GitHub") Alias indicates that this model property should be named differently in the generated json. Alias should be the outermost propschema. @@ -603,7 +603,7 @@ Returns **PropSchema** ## custom -[src/types/custom.js:36-45](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/custom.js#L36-L45 "Source code on GitHub") +[src/types/custom.js:36-45](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/custom.js#L36-L45 "Source code on GitHub") Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -645,7 +645,7 @@ Returns **PropSchema** ## customAsync -[src/types/customAsync.js:44-53](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/customAsync.js#L44-L53 "Source code on GitHub") +[src/types/customAsync.js:46-55](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/customAsync.js#L46-L55 "Source code on GitHub") Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. In comparison to the 'custom' propSchema it is possible to wait for asynchronous deserialization steps. This allows e.g. resolution of references during the deserialization process (using context.await.. @@ -680,7 +680,7 @@ var schema = _.createSimpleSchema({ }, function(v, callback, context, oldValue) { somePromise(v, context, oldValue).then((result) => { - callback(null, result) + callback(null, result - 2) }.catch((err) => { callback(err) } @@ -688,14 +688,16 @@ var schema = _.createSimpleSchema({ ), }); t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); -t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }); +_.deserialize(s, { a: 6 }, (err, res) => { + t.deepEqual(res.a, 4) +}; ``` Returns **PropSchema** ## object -[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/object.js#L34-L52 "Source code on GitHub") +[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/object.js#L34-L52 "Source code on GitHub") `object` indicates that this property contains an object that needs to be (de)serialized using its own model schema. @@ -732,7 +734,7 @@ Returns **PropSchema** ## reference -[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/reference.js#L65-L98 "Source code on GitHub") +[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/reference.js#L65-L98 "Source code on GitHub") `reference` can be used to (de)serialize references that point to other models. @@ -796,7 +798,7 @@ Returns **PropSchema** ## list -[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/list.js#L33-L54 "Source code on GitHub") +[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/list.js#L33-L54 "Source code on GitHub") List indicates that this property contains a list of things. Accepts a sub model schema to serialize the contents @@ -834,7 +836,7 @@ Returns **PropSchema** ## map -[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/map.js#L13-L62 "Source code on GitHub") +[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/map.js#L13-L62 "Source code on GitHub") Similar to list, but map represents a string keyed dynamic collection. This can be both plain objects (default) or ES6 Map like structures. @@ -846,7 +848,7 @@ This will be inferred from the initial value of the targetted attribute. ## mapAsArray -[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") +[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the Customer object has the id stored on itself. @@ -861,7 +863,7 @@ For ES6 maps this has the benefit of being allowed to have non-string keys in th ## raw -[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/types/raw.js#L15-L24 "Source code on GitHub") +[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/raw.js#L15-L24 "Source code on GitHub") Indicates that this field is only need to putted in the serialized json or deserialized instance, without any transformations. Stay with its original value @@ -881,7 +883,7 @@ Returns **[ModelSchema](#modelschema)** ## SKIP -[src/constants.js:20-20](https://github.com/evoye/serializr/blob/d668510a9149ba57622ac3c4090f4516f3669ca0/src/constants.js#L20-L20 "Source code on GitHub") +[src/constants.js:20-20](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/constants.js#L20-L20 "Source code on GitHub") In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. diff --git a/src/types/customAsync.js b/src/types/customAsync.js index b6f740b..ab31cb0 100644 --- a/src/types/customAsync.js +++ b/src/types/customAsync.js @@ -27,7 +27,7 @@ import { invariant } from "../utils/utils" * }, * function(v, callback, context, oldValue) { * somePromise(v, context, oldValue).then((result) => { - * callback(null, result) + * callback(null, result - 2) * }.catch((err) => { * callback(err) * } @@ -35,7 +35,9 @@ import { invariant } from "../utils/utils" * ), * }); * t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); - * t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }); + * _.deserialize(s, { a: 6 }, (err, res) => { + * t.deepEqual(res.a, 4) + * }; * * @param {function} serializer function that takes a model value and turns it into a json value * @param {function} deserializer function that takes a json value and turns it into a model value. It also receives a callback function to notify the caller about the completion of the deserialization. It also takes context argument, which can allow you to deserialize based on the context of other parameters. diff --git a/test/typescript/ts.ts b/test/typescript/ts.ts index c9ebe3c..4cffe3c 100644 --- a/test/typescript/ts.ts +++ b/test/typescript/ts.ts @@ -1,4 +1,17 @@ -import {serializable, alias, list, object, identifier, reference, primitive, serialize, deserialize, serializeAll} from "../../"; +import { + serializable, + alias, + list, + object, + identifier, + reference, + primitive, + serialize, + deserialize, + serializeAll, + custom, + customAsync +} from "../../"; import {observable, autorun} from "mobx"; declare var require; @@ -26,19 +39,19 @@ test("should work in typescript", t => { }); t.equal(called, 1); - t.deepEqual(res, { x: 3, y: 4, z: 5}); + t.deepEqual(res, {x: 3, y: 4, z: 5}); a.z++; // no autorun t.equal(a.z, 6); a.y++; t.equal(called, 2); - t.deepEqual(res, { x: 3, y: 5, z: 6}); + t.deepEqual(res, {x: 3, y: 5, z: 6}); a.x++; t.equal(called, 3); - t.deepEqual(res, { x: 4, y: 5, z: 6}); + t.deepEqual(res, {x: 4, y: 5, z: 6}); - const b = deserialize(A, { x: 1, y: 2, z: 3}); + const b = deserialize(A, {x: 1, y: 2, z: 3}); t.deepEqual(serialize(b), {x: 1, y: 2, z: 3}); t.ok(b instanceof A); @@ -46,28 +59,28 @@ test("should work in typescript", t => { }); test("typescript class with constructor params", t => { - class Rectangle { - @serializable - public someNumber: number; + class Rectangle { + @serializable + public someNumber: number; - @serializable(alias("identifier", identifier())) - public id: string; + @serializable(alias("identifier", identifier())) + public id: string; - @serializable(alias("width", true)) - public width: number + @serializable(alias("width", true)) + public width: number - @serializable(alias("height", true)) - public height: number + @serializable(alias("height", true)) + public height: number - constructor(id: string, width: number, height: number) { - this.id = id; - this.width = width; - this.height = height; - } + constructor(id: string, width: number, height: number) { + this.id = id; + this.width = width; + this.height = height; + } - public getArea(): number { - return this.width * this.height; - } + public getArea(): number { + return this.width * this.height; + } } const a = new Rectangle("A", 10, 20); @@ -136,6 +149,42 @@ test("[ts] it should handle prototypes", t => { t.end(); }); +test("[ts] custom prop schemas", t => { + function customSerializer(v) { + return v + } + + function customDeserializer(jsonValue, context, oldValue) { + return jsonValue + } + + function customAsyncDeserializer(jsonValue, done, context, oldValue) { + done(null, jsonValue) + } + + class A { + @serializable(custom(customSerializer, customDeserializer)) a = "hoi"; + @serializable(customAsync(customSerializer, customAsyncDeserializer)) a2 = "oeps"; + } + + let result = serialize(new A()) + const initial = { + a: "hoi", a2: "oeps" + } + const updated = { + a: "all", a2: "new" + } + t.deepEqual(result, initial) + + + deserialize(A, updated, (err, resultObj) => { + err ? t.end(err) : null + result = serialize(resultObj) + t.deepEqual(result, updated) + t.end() + }) +}); + test.skip("[ts] it should handle not yet defined modelschema's for classes", t => { // classes are declared as var, not as function, so aren't hoisted :'( class Comment { @@ -154,8 +203,8 @@ test.skip("[ts] it should handle not yet defined modelschema's for classes", t = const json = { ref: 1, child: [ - { id: 2, title: "foo" }, - { id: 1, title: "bar "} + {id: 2, title: "foo"}, + {id: 1, title: "bar "} ] }; const m = deserialize(Message, json); @@ -179,9 +228,9 @@ test("@serializeAll (babel)", t => { (store as any).c = 5; (store as any).d = {}; - t.deepEqual(serialize(store), { a: 3, c: 5}) + t.deepEqual(serialize(store), {a: 3, c: 5}) - const store2 = deserialize(Store, { a: 2, b: 3, c: 4}) + const store2 = deserialize(Store, {a: 2, b: 3, c: 4}) t.equal(store2.a, 2) t.equal(store2.b, 3) t.equal((store2 as any).c, 4) From a942bfaa0030c4d954cc5ab6ee46d57f5bfe0c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Sat, 7 Jul 2018 21:16:31 +0200 Subject: [PATCH 4/8] added optional callback to custom propScheme --- README.md | 80 +++++++++++++++++++++++++--------------- serializr.d.ts | 3 +- src/types/custom.js | 49 ++++++++++++++++++++---- src/types/customAsync.js | 4 +- src/utils/utils.js | 4 ++ test/typescript/ts.ts | 5 ++- 6 files changed, 103 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 737de9b..4b28b6c 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl ## ModelSchema -[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/serializr.js#L52-L52 "Source code on GitHub") +[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/serializr.js#L52-L52 "Source code on GitHub") JSDOC type defintions for usage w/o typescript. @@ -341,7 +341,7 @@ Returns **any** void ## createSimpleSchema -[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") +[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") Creates a model schema that (de)serializes from / to plain javascript objects. Its factory method is: `() => ({})` @@ -366,7 +366,7 @@ Returns **[object](#object)** model schema ## createModelSchema -[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") +[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") Creates a model schema that (de)serializes an object created by a constructor function (class). The created model schema is associated by the targeted type as default model schema, see setDefaultModelSchema. @@ -399,7 +399,7 @@ Returns **[object](#object)** model schema ## getDefaultModelSchema -[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") +[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") Returns the standard model schema associated with a class / constructor function @@ -411,7 +411,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## setDefaultModelSchema -[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") +[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") Sets the default model schema for class / constructor function. Everywhere where a model schema is required as argument, this class / constructor function @@ -429,7 +429,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## serializable -[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/api/serializable.js#L93-L103 "Source code on GitHub") +[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/serializable.js#L93-L103 "Source code on GitHub") Decorator that defines a new property mapping on the default model schema for the class it is used in. @@ -453,7 +453,7 @@ Returns **PropertyDescriptor** ## serialize -[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/serialize.js#L16-L34 "Source code on GitHub") +[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/serialize.js#L16-L34 "Source code on GitHub") Serializes an object (graph) into json using the provided model schema. The model schema can be omitted if the object type has a default model schema associated with it. @@ -468,7 +468,7 @@ Returns **[object](#object)** serialized representation of the object ## serializeAll -[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/serialize.js#L89-L100 "Source code on GitHub") +[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/serialize.js#L89-L100 "Source code on GitHub") The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. @@ -478,7 +478,7 @@ The `serializeAll` decorator can be used on a class to signal that all primitive ## deserialize -[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/deserialize.js#L38-L56 "Source code on GitHub") +[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/deserialize.js#L38-L56 "Source code on GitHub") Deserializes a json structor into an object graph. This process might be asynchronous (for example if there are references with an asynchronous @@ -497,7 +497,7 @@ Returns **([object](#object) \| [array](https://developer.mozilla.org/en-US/docs ## update -[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/core/update.js#L21-L42 "Source code on GitHub") +[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/update.js#L21-L42 "Source code on GitHub") Similar to deserialize, but updates an existing object instance. Properties will always updated entirely, but properties not present in the json will be kept as is. @@ -513,7 +513,7 @@ Further this method behaves similar to deserialize. ## primitive -[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/primitive.js#L17-L29 "Source code on GitHub") +[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/primitive.js#L17-L29 "Source code on GitHub") Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. @@ -532,7 +532,7 @@ Returns **[ModelSchema](#modelschema)** ## identifier -[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/identifier.js#L42-L56 "Source code on GitHub") +[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/identifier.js#L42-L56 "Source code on GitHub") Similar to primitive, but this field will be marked as the identifier for the given Model type. This is used by for example `reference()` to serialize the reference @@ -572,13 +572,13 @@ Returns **PropSchema** ## date -[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/date.js#L8-L23 "Source code on GitHub") +[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/date.js#L8-L23 "Source code on GitHub") Similar to primitive, serializes instances of Date objects ## alias -[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/alias.js#L20-L31 "Source code on GitHub") +[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/alias.js#L20-L31 "Source code on GitHub") Alias indicates that this model property should be named differently in the generated json. Alias should be the outermost propschema. @@ -603,7 +603,7 @@ Returns **PropSchema** ## custom -[src/types/custom.js:36-45](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/custom.js#L36-L45 "Source code on GitHub") +[src/types/custom.js:59-78](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/custom.js#L59-L78 "Source code on GitHub") Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -614,8 +614,11 @@ The `serializer` function has the signature: When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. -The `deserializer` function has the signature: -`(value, context) => void` +The `deserializer` function has the signature for synchronous processing +`(value, context, oldValue) => void` + +For asynchronous processing the function expects the following signature +`(value, context, oldValue, callback) => void` When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, contextObj)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)). @@ -627,7 +630,7 @@ When deserializing the object `{b: 2}` the `deserializer` function will be calle **Examples** ```javascript -var schema = _.createSimpleSchema({ +var schemaDefault = _.createSimpleSchema({ a: _.custom( function(v) { return v + 2; @@ -637,15 +640,34 @@ var schema = _.createSimpleSchema({ } ), }); -t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); -t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }); +t.deepEqual(_.serialize(schemaDefault, { a: 4 }), { a: 6 }); +t.deepEqual(_.deserialize(schemaDefault, { a: 6 }), { a: 4 }); + +var schemaWithAsyncProps = _.createSimpleSchema({ + a: _.customAsync( + function(v) { + return v + 2; + }, + function(v, context, oldValue, callback) { + somePromise(v, context, oldValue).then((result) => { + callback(null, result - 2) + }.catch((err) => { + callback(err) + } + } + ), +}); +t.deepEqual(_.serialize(schemaWithAsyncProps, { a: 4 }), { a: 6 }); +_.deserialize(schemaWithAsyncProps, { a: 6 }, (err, res) => { + t.deepEqual(res.a, 4) +}; ``` Returns **PropSchema** ## customAsync -[src/types/customAsync.js:46-55](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/customAsync.js#L46-L55 "Source code on GitHub") +[src/types/customAsync.js:46-55](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/customAsync.js#L46-L55 "Source code on GitHub") Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. In comparison to the 'custom' propSchema it is possible to wait for asynchronous deserialization steps. This allows e.g. resolution of references during the deserialization process (using context.await.. @@ -678,7 +700,7 @@ var schema = _.createSimpleSchema({ function(v) { return v + 2; }, - function(v, callback, context, oldValue) { + function(v, context, oldValue, callback) { somePromise(v, context, oldValue).then((result) => { callback(null, result - 2) }.catch((err) => { @@ -697,7 +719,7 @@ Returns **PropSchema** ## object -[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/object.js#L34-L52 "Source code on GitHub") +[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/object.js#L34-L52 "Source code on GitHub") `object` indicates that this property contains an object that needs to be (de)serialized using its own model schema. @@ -734,7 +756,7 @@ Returns **PropSchema** ## reference -[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/reference.js#L65-L98 "Source code on GitHub") +[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/reference.js#L65-L98 "Source code on GitHub") `reference` can be used to (de)serialize references that point to other models. @@ -798,7 +820,7 @@ Returns **PropSchema** ## list -[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/list.js#L33-L54 "Source code on GitHub") +[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/list.js#L33-L54 "Source code on GitHub") List indicates that this property contains a list of things. Accepts a sub model schema to serialize the contents @@ -836,7 +858,7 @@ Returns **PropSchema** ## map -[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/map.js#L13-L62 "Source code on GitHub") +[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/map.js#L13-L62 "Source code on GitHub") Similar to list, but map represents a string keyed dynamic collection. This can be both plain objects (default) or ES6 Map like structures. @@ -848,7 +870,7 @@ This will be inferred from the initial value of the targetted attribute. ## mapAsArray -[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") +[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the Customer object has the id stored on itself. @@ -863,7 +885,7 @@ For ES6 maps this has the benefit of being allowed to have non-string keys in th ## raw -[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/types/raw.js#L15-L24 "Source code on GitHub") +[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/raw.js#L15-L24 "Source code on GitHub") Indicates that this field is only need to putted in the serialized json or deserialized instance, without any transformations. Stay with its original value @@ -883,7 +905,7 @@ Returns **[ModelSchema](#modelschema)** ## SKIP -[src/constants.js:20-20](https://github.com/evoye/serializr/blob/2b154f7156a77360e9babbd368dd075a646d4a1e/src/constants.js#L20-L20 "Source code on GitHub") +[src/constants.js:20-20](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/constants.js#L20-L20 "Source code on GitHub") In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. diff --git a/serializr.d.ts b/serializr.d.ts index 0faa36a..12bf158 100644 --- a/serializr.d.ts +++ b/serializr.d.ts @@ -72,8 +72,9 @@ export function map(propSchema: PropSchema): PropSchema; export function mapAsArray(propSchema: PropSchema, keyPropertyName: string): PropSchema; export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context?: any, oldValue?: any) => any): PropSchema; +export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context: any, oldValue: any, callback: (err: any, result: any) => void) => any): PropSchema; -export function customAsync(serializer: (value: any) => any, deserializer: (jsonValue: any, callback: (err: any, result: any) => void, context?: any, oldValue?: any) => any): PropSchema; +export function customAsync(serializer: (value: any) => any, deserializer: (jsonValue: any, context: any, oldValue: any, callback: (err: any, result: any) => void) => any): PropSchema; export function serializeAll(clazz: T): T diff --git a/src/types/custom.js b/src/types/custom.js index 7ba26e2..daa58cb 100644 --- a/src/types/custom.js +++ b/src/types/custom.js @@ -1,4 +1,4 @@ -import { invariant } from "../utils/utils" +import {invariant, isPromise} from "../utils/utils" /** * Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -10,13 +10,16 @@ import { invariant } from "../utils/utils" * When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. - * The `deserializer` function has the signature: - * `(value, context) => void` + * The `deserializer` function has the signature for synchronous processing + * `(value, context, oldValue) => void` + + * For asynchronous processing the function expects the following signature + * `(value, context, oldValue, callback) => void` * When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, contextObj)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)). * * @example - * var schema = _.createSimpleSchema({ + * var schemaDefault = _.createSimpleSchema({ * a: _.custom( * function(v) { * return v + 2; @@ -26,8 +29,28 @@ import { invariant } from "../utils/utils" * } * ), * }); - * t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); - * t.deepEqual(_.deserialize(s, { a: 6 }), { a: 4 }); + * t.deepEqual(_.serialize(schemaDefault, { a: 4 }), { a: 6 }); + * t.deepEqual(_.deserialize(schemaDefault, { a: 6 }), { a: 4 }); + * + * var schemaWithAsyncProps = _.createSimpleSchema({ + * a: _.customAsync( + * function(v) { + * return v + 2; + * }, + * function(v, context, oldValue, callback) { + * somePromise(v, context, oldValue).then((result) => { + * callback(null, result - 2) + * }.catch((err) => { + * callback(err) + * } + * } + * ), + * }); + * t.deepEqual(_.serialize(schemaWithAsyncProps, { a: 4 }), { a: 6 }); + * _.deserialize(schemaWithAsyncProps, { a: 6 }, (err, res) => { + * t.deepEqual(res.a, 4) + * }; + * * @param {function} serializer function that takes a model value and turns it into a json value * @param {function} deserializer function that takes a json value and turns it into a model value. It also takes context argument, which can allow you to deserialize based on the context of other parameters. @@ -35,11 +58,21 @@ import { invariant } from "../utils/utils" */ export default function custom(serializer, deserializer) { invariant(typeof serializer === "function", "first argument should be function") - invariant(typeof deserializer === "function", "second argument should be function") + invariant((typeof deserializer === "function") || isPromise(deserializer), "second argument should be a function or promise") return { serializer: serializer, deserializer: function (jsonValue, done, context, oldValue) { - done(null, deserializer(jsonValue, context, oldValue)) + if (deserializer.length === 4) { + deserializer(jsonValue, context, oldValue, done) + } else if (isPromise(deserializer)) { + deserializer(jsonValue, context, oldValue).then((result) => { + done(null, result) + }).catch((err) => { + done(err) + }) + } else { + done(null, deserializer(jsonValue, context, oldValue)) + } } } } diff --git a/src/types/customAsync.js b/src/types/customAsync.js index ab31cb0..2f3e068 100644 --- a/src/types/customAsync.js +++ b/src/types/customAsync.js @@ -25,7 +25,7 @@ import { invariant } from "../utils/utils" * function(v) { * return v + 2; * }, - * function(v, callback, context, oldValue) { + * function(v, context, oldValue, callback) { * somePromise(v, context, oldValue).then((result) => { * callback(null, result - 2) * }.catch((err) => { @@ -49,7 +49,7 @@ export default function customAsync(serializer, deserializer) { return { serializer: serializer, deserializer: function (jsonValue, done, context, oldValue) { - deserializer(jsonValue, done, context, oldValue) + deserializer(jsonValue, context, oldValue, done) } } } diff --git a/src/utils/utils.js b/src/utils/utils.js index befdb87..ec55a70 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -78,6 +78,10 @@ export function isMapLike(thing) { return thing && typeof thing.keys === "function" && typeof thing.clear === "function" } +export function isPromise(obj) { + return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function" +} + export function getIdentifierProp(modelSchema) { invariant(isModelSchema(modelSchema)) // optimization: cache this lookup diff --git a/test/typescript/ts.ts b/test/typescript/ts.ts index 4cffe3c..fa96cfb 100644 --- a/test/typescript/ts.ts +++ b/test/typescript/ts.ts @@ -158,13 +158,14 @@ test("[ts] custom prop schemas", t => { return jsonValue } - function customAsyncDeserializer(jsonValue, done, context, oldValue) { + function customAsyncDeserializer(jsonValue, context, oldValue, done) { + console.log(context, oldValue, done) done(null, jsonValue) } class A { @serializable(custom(customSerializer, customDeserializer)) a = "hoi"; - @serializable(customAsync(customSerializer, customAsyncDeserializer)) a2 = "oeps"; + @serializable(custom(customSerializer, customAsyncDeserializer)) a2 = "oeps"; } let result = serialize(new A()) From dbe85c9cb86e8ace5b41cc93dbb58583c78679e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Sat, 7 Jul 2018 21:29:22 +0200 Subject: [PATCH 5/8] removed customAsync, custom deserializer with callback should be used --- src/types/customAsync.js | 55 ---------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 src/types/customAsync.js diff --git a/src/types/customAsync.js b/src/types/customAsync.js deleted file mode 100644 index 2f3e068..0000000 --- a/src/types/customAsync.js +++ /dev/null @@ -1,55 +0,0 @@ -import { invariant } from "../utils/utils" - -/** - * Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. - * In comparison to the 'custom' propSchema it is possible to wait for asynchronous deserialization steps. This allows e.g. resolution of references during the deserialization process (using context.await.. - - * The `customAsync` function takes two parameters, the `serializer` function and the `deserializer` function. - - * The `serializer` function has the signature: - * `(value, key, obj) => void` - - * When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. - - * The `deserializer` function has the signature: - * `(value, callback, context, oldValue) => void` - - * The `callback` function has the signature: - * `(error, result) => void` - - * When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, callback, contextObj, oldValue)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)). - * - * @example - * var schema = _.createSimpleSchema({ - * a: _.customAsync( - * function(v) { - * return v + 2; - * }, - * function(v, context, oldValue, callback) { - * somePromise(v, context, oldValue).then((result) => { - * callback(null, result - 2) - * }.catch((err) => { - * callback(err) - * } - * } - * ), - * }); - * t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); - * _.deserialize(s, { a: 6 }, (err, res) => { - * t.deepEqual(res.a, 4) - * }; - * - * @param {function} serializer function that takes a model value and turns it into a json value - * @param {function} deserializer function that takes a json value and turns it into a model value. It also receives a callback function to notify the caller about the completion of the deserialization. It also takes context argument, which can allow you to deserialize based on the context of other parameters. - * @returns {PropSchema} - */ -export default function customAsync(serializer, deserializer) { - invariant(typeof serializer === "function", "first argument should be function") - invariant(typeof deserializer === "function", "second argument should be function") - return { - serializer: serializer, - deserializer: function (jsonValue, done, context, oldValue) { - deserializer(jsonValue, context, oldValue, done) - } - } -} From 97a818c936d62cc916057579f26979decedf7f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Sat, 7 Jul 2018 21:34:24 +0200 Subject: [PATCH 6/8] fix --- README.md | 96 ++++++++++--------------------------------- serializr.d.ts | 2 - src/serializr.js | 1 - test/typescript/ts.ts | 3 +- 4 files changed, 23 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 4b28b6c..8b40ca6 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl ## ModelSchema -[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/serializr.js#L52-L52 "Source code on GitHub") +[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/serializr.js#L52-L52 "Source code on GitHub") JSDOC type defintions for usage w/o typescript. @@ -341,7 +341,7 @@ Returns **any** void ## createSimpleSchema -[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") +[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") Creates a model schema that (de)serializes from / to plain javascript objects. Its factory method is: `() => ({})` @@ -366,7 +366,7 @@ Returns **[object](#object)** model schema ## createModelSchema -[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") +[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") Creates a model schema that (de)serializes an object created by a constructor function (class). The created model schema is associated by the targeted type as default model schema, see setDefaultModelSchema. @@ -399,7 +399,7 @@ Returns **[object](#object)** model schema ## getDefaultModelSchema -[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") +[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") Returns the standard model schema associated with a class / constructor function @@ -411,7 +411,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## setDefaultModelSchema -[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") +[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") Sets the default model schema for class / constructor function. Everywhere where a model schema is required as argument, this class / constructor function @@ -429,7 +429,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## serializable -[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/api/serializable.js#L93-L103 "Source code on GitHub") +[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/serializable.js#L93-L103 "Source code on GitHub") Decorator that defines a new property mapping on the default model schema for the class it is used in. @@ -453,7 +453,7 @@ Returns **PropertyDescriptor** ## serialize -[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/serialize.js#L16-L34 "Source code on GitHub") +[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/serialize.js#L16-L34 "Source code on GitHub") Serializes an object (graph) into json using the provided model schema. The model schema can be omitted if the object type has a default model schema associated with it. @@ -468,7 +468,7 @@ Returns **[object](#object)** serialized representation of the object ## serializeAll -[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/serialize.js#L89-L100 "Source code on GitHub") +[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/serialize.js#L89-L100 "Source code on GitHub") The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. @@ -478,7 +478,7 @@ The `serializeAll` decorator can be used on a class to signal that all primitive ## deserialize -[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/deserialize.js#L38-L56 "Source code on GitHub") +[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/deserialize.js#L38-L56 "Source code on GitHub") Deserializes a json structor into an object graph. This process might be asynchronous (for example if there are references with an asynchronous @@ -497,7 +497,7 @@ Returns **([object](#object) \| [array](https://developer.mozilla.org/en-US/docs ## update -[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/core/update.js#L21-L42 "Source code on GitHub") +[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/update.js#L21-L42 "Source code on GitHub") Similar to deserialize, but updates an existing object instance. Properties will always updated entirely, but properties not present in the json will be kept as is. @@ -513,7 +513,7 @@ Further this method behaves similar to deserialize. ## primitive -[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/primitive.js#L17-L29 "Source code on GitHub") +[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/primitive.js#L17-L29 "Source code on GitHub") Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. @@ -532,7 +532,7 @@ Returns **[ModelSchema](#modelschema)** ## identifier -[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/identifier.js#L42-L56 "Source code on GitHub") +[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/identifier.js#L42-L56 "Source code on GitHub") Similar to primitive, but this field will be marked as the identifier for the given Model type. This is used by for example `reference()` to serialize the reference @@ -572,13 +572,13 @@ Returns **PropSchema** ## date -[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/date.js#L8-L23 "Source code on GitHub") +[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/date.js#L8-L23 "Source code on GitHub") Similar to primitive, serializes instances of Date objects ## alias -[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/alias.js#L20-L31 "Source code on GitHub") +[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/alias.js#L20-L31 "Source code on GitHub") Alias indicates that this model property should be named differently in the generated json. Alias should be the outermost propschema. @@ -603,7 +603,7 @@ Returns **PropSchema** ## custom -[src/types/custom.js:59-78](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/custom.js#L59-L78 "Source code on GitHub") +[src/types/custom.js:59-78](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/custom.js#L59-L78 "Source code on GitHub") Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -665,61 +665,9 @@ _.deserialize(schemaWithAsyncProps, { a: 6 }, (err, res) => { Returns **PropSchema** -## customAsync - -[src/types/customAsync.js:46-55](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/customAsync.js#L46-L55 "Source code on GitHub") - -Can be used to create asynchronous custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. -In comparison to the 'custom' propSchema it is possible to wait for asynchronous deserialization steps. This allows e.g. resolution of references during the deserialization process (using context.await.. - -The `customAsync` function takes two parameters, the `serializer` function and the `deserializer` function. - -The `serializer` function has the signature: -`(value, key, obj) => void` - -When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. - -The `deserializer` function has the signature: -`(value, callback, context, oldValue) => void` - -The `callback` function has the signature: -`(error, result) => void` - -When deserializing the object `{b: 2}` the `deserializer` function will be called with `deserializer(2, callback, contextObj, oldValue)` ([contextObj reference](https://github.com/mobxjs/serializr#deserialization-context)). - -**Parameters** - -- `serializer` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that takes a model value and turns it into a json value -- `deserializer` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that takes a json value and turns it into a model value. It also receives a callback function to notify the caller about the completion of the deserialization. It also takes context argument, which can allow you to deserialize based on the context of other parameters. - -**Examples** - -```javascript -var schema = _.createSimpleSchema({ - a: _.customAsync( - function(v) { - return v + 2; - }, - function(v, context, oldValue, callback) { - somePromise(v, context, oldValue).then((result) => { - callback(null, result - 2) - }.catch((err) => { - callback(err) - } - } - ), -}); -t.deepEqual(_.serialize(s, { a: 4 }), { a: 6 }); -_.deserialize(s, { a: 6 }, (err, res) => { - t.deepEqual(res.a, 4) -}; -``` - -Returns **PropSchema** - ## object -[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/object.js#L34-L52 "Source code on GitHub") +[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/object.js#L34-L52 "Source code on GitHub") `object` indicates that this property contains an object that needs to be (de)serialized using its own model schema. @@ -756,7 +704,7 @@ Returns **PropSchema** ## reference -[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/reference.js#L65-L98 "Source code on GitHub") +[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/reference.js#L65-L98 "Source code on GitHub") `reference` can be used to (de)serialize references that point to other models. @@ -820,7 +768,7 @@ Returns **PropSchema** ## list -[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/list.js#L33-L54 "Source code on GitHub") +[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/list.js#L33-L54 "Source code on GitHub") List indicates that this property contains a list of things. Accepts a sub model schema to serialize the contents @@ -858,7 +806,7 @@ Returns **PropSchema** ## map -[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/map.js#L13-L62 "Source code on GitHub") +[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/map.js#L13-L62 "Source code on GitHub") Similar to list, but map represents a string keyed dynamic collection. This can be both plain objects (default) or ES6 Map like structures. @@ -870,7 +818,7 @@ This will be inferred from the initial value of the targetted attribute. ## mapAsArray -[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") +[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the Customer object has the id stored on itself. @@ -885,7 +833,7 @@ For ES6 maps this has the benefit of being allowed to have non-string keys in th ## raw -[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/types/raw.js#L15-L24 "Source code on GitHub") +[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/raw.js#L15-L24 "Source code on GitHub") Indicates that this field is only need to putted in the serialized json or deserialized instance, without any transformations. Stay with its original value @@ -905,7 +853,7 @@ Returns **[ModelSchema](#modelschema)** ## SKIP -[src/constants.js:20-20](https://github.com/evoye/serializr/blob/d9c6a398d8b8fc3471fc4270b5df05bde221d05a/src/constants.js#L20-L20 "Source code on GitHub") +[src/constants.js:20-20](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/constants.js#L20-L20 "Source code on GitHub") In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. diff --git a/serializr.d.ts b/serializr.d.ts index 12bf158..f8538c3 100644 --- a/serializr.d.ts +++ b/serializr.d.ts @@ -74,8 +74,6 @@ export function mapAsArray(propSchema: PropSchema, keyPropertyName: string): Pro export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context?: any, oldValue?: any) => any): PropSchema; export function custom(serializer: (value: any) => any, deserializer: (jsonValue: any, context: any, oldValue: any, callback: (err: any, result: any) => void) => any): PropSchema; -export function customAsync(serializer: (value: any) => any, deserializer: (jsonValue: any, context: any, oldValue: any, callback: (err: any, result: any) => void) => any): PropSchema; - export function serializeAll(clazz: T): T export const SKIP: {} diff --git a/src/serializr.js b/src/serializr.js index b18166d..270f0ad 100644 --- a/src/serializr.js +++ b/src/serializr.js @@ -67,7 +67,6 @@ export { default as identifier } from "./types/identifier" export { default as date } from "./types/date" export { default as alias } from "./types/alias" export { default as custom } from "./types/custom" -export { default as customAsync } from "./types/customAsync" export { default as object } from "./types/object" export { default as reference } from "./types/reference" export { default as list } from "./types/list" diff --git a/test/typescript/ts.ts b/test/typescript/ts.ts index fa96cfb..4338dc8 100644 --- a/test/typescript/ts.ts +++ b/test/typescript/ts.ts @@ -9,8 +9,7 @@ import { serialize, deserialize, serializeAll, - custom, - customAsync + custom } from "../../"; import {observable, autorun} from "mobx"; From 27e5a4ce9cd83f6fa0211f738517b61a2b1a4458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Tue, 17 Jul 2018 18:43:28 +0200 Subject: [PATCH 7/8] final polish, removed build error --- README.md | 44 ++++++++++++++++++++++---------------------- src/types/custom.js | 12 +++--------- src/utils/utils.js | 4 ---- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 8b40ca6..212f620 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl ## ModelSchema -[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/serializr.js#L52-L52 "Source code on GitHub") +[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/serializr.js#L52-L52 "Source code on GitHub") JSDOC type defintions for usage w/o typescript. @@ -341,7 +341,7 @@ Returns **any** void ## createSimpleSchema -[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") +[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") Creates a model schema that (de)serializes from / to plain javascript objects. Its factory method is: `() => ({})` @@ -366,7 +366,7 @@ Returns **[object](#object)** model schema ## createModelSchema -[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") +[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") Creates a model schema that (de)serializes an object created by a constructor function (class). The created model schema is associated by the targeted type as default model schema, see setDefaultModelSchema. @@ -399,7 +399,7 @@ Returns **[object](#object)** model schema ## getDefaultModelSchema -[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") +[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") Returns the standard model schema associated with a class / constructor function @@ -411,7 +411,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## setDefaultModelSchema -[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") +[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") Sets the default model schema for class / constructor function. Everywhere where a model schema is required as argument, this class / constructor function @@ -429,7 +429,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## serializable -[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/api/serializable.js#L93-L103 "Source code on GitHub") +[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/serializable.js#L93-L103 "Source code on GitHub") Decorator that defines a new property mapping on the default model schema for the class it is used in. @@ -453,7 +453,7 @@ Returns **PropertyDescriptor** ## serialize -[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/serialize.js#L16-L34 "Source code on GitHub") +[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/serialize.js#L16-L34 "Source code on GitHub") Serializes an object (graph) into json using the provided model schema. The model schema can be omitted if the object type has a default model schema associated with it. @@ -468,7 +468,7 @@ Returns **[object](#object)** serialized representation of the object ## serializeAll -[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/serialize.js#L89-L100 "Source code on GitHub") +[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/serialize.js#L89-L100 "Source code on GitHub") The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. @@ -478,7 +478,7 @@ The `serializeAll` decorator can be used on a class to signal that all primitive ## deserialize -[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/deserialize.js#L38-L56 "Source code on GitHub") +[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/deserialize.js#L38-L56 "Source code on GitHub") Deserializes a json structor into an object graph. This process might be asynchronous (for example if there are references with an asynchronous @@ -497,7 +497,7 @@ Returns **([object](#object) \| [array](https://developer.mozilla.org/en-US/docs ## update -[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/core/update.js#L21-L42 "Source code on GitHub") +[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/update.js#L21-L42 "Source code on GitHub") Similar to deserialize, but updates an existing object instance. Properties will always updated entirely, but properties not present in the json will be kept as is. @@ -513,7 +513,7 @@ Further this method behaves similar to deserialize. ## primitive -[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/primitive.js#L17-L29 "Source code on GitHub") +[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/primitive.js#L17-L29 "Source code on GitHub") Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. @@ -532,7 +532,7 @@ Returns **[ModelSchema](#modelschema)** ## identifier -[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/identifier.js#L42-L56 "Source code on GitHub") +[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/identifier.js#L42-L56 "Source code on GitHub") Similar to primitive, but this field will be marked as the identifier for the given Model type. This is used by for example `reference()` to serialize the reference @@ -572,13 +572,13 @@ Returns **PropSchema** ## date -[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/date.js#L8-L23 "Source code on GitHub") +[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/date.js#L8-L23 "Source code on GitHub") Similar to primitive, serializes instances of Date objects ## alias -[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/alias.js#L20-L31 "Source code on GitHub") +[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/alias.js#L20-L31 "Source code on GitHub") Alias indicates that this model property should be named differently in the generated json. Alias should be the outermost propschema. @@ -603,7 +603,7 @@ Returns **PropSchema** ## custom -[src/types/custom.js:59-78](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/custom.js#L59-L78 "Source code on GitHub") +[src/types/custom.js:59-72](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/custom.js#L59-L72 "Source code on GitHub") Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -667,7 +667,7 @@ Returns **PropSchema** ## object -[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/object.js#L34-L52 "Source code on GitHub") +[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/object.js#L34-L52 "Source code on GitHub") `object` indicates that this property contains an object that needs to be (de)serialized using its own model schema. @@ -704,7 +704,7 @@ Returns **PropSchema** ## reference -[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/reference.js#L65-L98 "Source code on GitHub") +[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/reference.js#L65-L98 "Source code on GitHub") `reference` can be used to (de)serialize references that point to other models. @@ -768,7 +768,7 @@ Returns **PropSchema** ## list -[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/list.js#L33-L54 "Source code on GitHub") +[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/list.js#L33-L54 "Source code on GitHub") List indicates that this property contains a list of things. Accepts a sub model schema to serialize the contents @@ -806,7 +806,7 @@ Returns **PropSchema** ## map -[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/map.js#L13-L62 "Source code on GitHub") +[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/map.js#L13-L62 "Source code on GitHub") Similar to list, but map represents a string keyed dynamic collection. This can be both plain objects (default) or ES6 Map like structures. @@ -818,7 +818,7 @@ This will be inferred from the initial value of the targetted attribute. ## mapAsArray -[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") +[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the Customer object has the id stored on itself. @@ -833,7 +833,7 @@ For ES6 maps this has the benefit of being allowed to have non-string keys in th ## raw -[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/types/raw.js#L15-L24 "Source code on GitHub") +[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/raw.js#L15-L24 "Source code on GitHub") Indicates that this field is only need to putted in the serialized json or deserialized instance, without any transformations. Stay with its original value @@ -853,7 +853,7 @@ Returns **[ModelSchema](#modelschema)** ## SKIP -[src/constants.js:20-20](https://github.com/evoye/serializr/blob/dbe85c9cb86e8ace5b41cc93dbb58583c78679e6/src/constants.js#L20-L20 "Source code on GitHub") +[src/constants.js:20-20](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/constants.js#L20-L20 "Source code on GitHub") In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. diff --git a/src/types/custom.js b/src/types/custom.js index daa58cb..86fa5a9 100644 --- a/src/types/custom.js +++ b/src/types/custom.js @@ -1,4 +1,4 @@ -import {invariant, isPromise} from "../utils/utils" +import {invariant} from "../utils/utils" /** * Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -10,7 +10,7 @@ import {invariant, isPromise} from "../utils/utils" * When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. - * The `deserializer` function has the signature for synchronous processing + * The `deserializer` function has the following signature for synchronous processing * `(value, context, oldValue) => void` * For asynchronous processing the function expects the following signature @@ -58,18 +58,12 @@ import {invariant, isPromise} from "../utils/utils" */ export default function custom(serializer, deserializer) { invariant(typeof serializer === "function", "first argument should be function") - invariant((typeof deserializer === "function") || isPromise(deserializer), "second argument should be a function or promise") + invariant((typeof deserializer === "function"), "second argument should be a function or promise") return { serializer: serializer, deserializer: function (jsonValue, done, context, oldValue) { if (deserializer.length === 4) { deserializer(jsonValue, context, oldValue, done) - } else if (isPromise(deserializer)) { - deserializer(jsonValue, context, oldValue).then((result) => { - done(null, result) - }).catch((err) => { - done(err) - }) } else { done(null, deserializer(jsonValue, context, oldValue)) } diff --git a/src/utils/utils.js b/src/utils/utils.js index ec55a70..befdb87 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -78,10 +78,6 @@ export function isMapLike(thing) { return thing && typeof thing.keys === "function" && typeof thing.clear === "function" } -export function isPromise(obj) { - return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function" -} - export function getIdentifierProp(modelSchema) { invariant(isModelSchema(modelSchema)) // optimization: cache this lookup From 5e56e0a50a5bfd86508c370388d77aeebad480ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Rose?= Date: Wed, 18 Jul 2018 13:18:29 +0200 Subject: [PATCH 8/8] set version to 1.2.0 --- README.md | 46 +++++++++++++++++++++++----------------------- package.json | 2 +- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 212f620..7071453 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,7 @@ When deserializing a model elememt / property, the following fields are availabl ## ModelSchema -[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/serializr.js#L52-L52 "Source code on GitHub") +[src/serializr.js:52-52](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/serializr.js#L52-L52 "Source code on GitHub") JSDOC type defintions for usage w/o typescript. @@ -341,7 +341,7 @@ Returns **any** void ## createSimpleSchema -[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") +[src/api/createSimpleSchema.js:17-24](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/api/createSimpleSchema.js#L17-L24 "Source code on GitHub") Creates a model schema that (de)serializes from / to plain javascript objects. Its factory method is: `() => ({})` @@ -366,7 +366,7 @@ Returns **[object](#object)** model schema ## createModelSchema -[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") +[src/api/createModelSchema.js:29-47](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/api/createModelSchema.js#L29-L47 "Source code on GitHub") Creates a model schema that (de)serializes an object created by a constructor function (class). The created model schema is associated by the targeted type as default model schema, see setDefaultModelSchema. @@ -399,7 +399,7 @@ Returns **[object](#object)** model schema ## getDefaultModelSchema -[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") +[src/api/getDefaultModelSchema.js:9-18](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/api/getDefaultModelSchema.js#L9-L18 "Source code on GitHub") Returns the standard model schema associated with a class / constructor function @@ -411,7 +411,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## setDefaultModelSchema -[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") +[src/api/setDefaultModelSchema.js:15-18](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/api/setDefaultModelSchema.js#L15-L18 "Source code on GitHub") Sets the default model schema for class / constructor function. Everywhere where a model schema is required as argument, this class / constructor function @@ -429,7 +429,7 @@ Returns **[ModelSchema](#modelschema)** model schema ## serializable -[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/api/serializable.js#L93-L103 "Source code on GitHub") +[src/api/serializable.js:93-103](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/api/serializable.js#L93-L103 "Source code on GitHub") Decorator that defines a new property mapping on the default model schema for the class it is used in. @@ -453,7 +453,7 @@ Returns **PropertyDescriptor** ## serialize -[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/serialize.js#L16-L34 "Source code on GitHub") +[src/core/serialize.js:16-34](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/core/serialize.js#L16-L34 "Source code on GitHub") Serializes an object (graph) into json using the provided model schema. The model schema can be omitted if the object type has a default model schema associated with it. @@ -468,7 +468,7 @@ Returns **[object](#object)** serialized representation of the object ## serializeAll -[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/serialize.js#L89-L100 "Source code on GitHub") +[src/core/serialize.js:89-100](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/core/serialize.js#L89-L100 "Source code on GitHub") The `serializeAll` decorator can be used on a class to signal that all primitive properties should be serialized automatically. @@ -478,7 +478,7 @@ The `serializeAll` decorator can be used on a class to signal that all primitive ## deserialize -[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/deserialize.js#L38-L56 "Source code on GitHub") +[src/core/deserialize.js:38-56](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/core/deserialize.js#L38-L56 "Source code on GitHub") Deserializes a json structor into an object graph. This process might be asynchronous (for example if there are references with an asynchronous @@ -497,7 +497,7 @@ Returns **([object](#object) \| [array](https://developer.mozilla.org/en-US/docs ## update -[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/core/update.js#L21-L42 "Source code on GitHub") +[src/core/update.js:21-42](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/core/update.js#L21-L42 "Source code on GitHub") Similar to deserialize, but updates an existing object instance. Properties will always updated entirely, but properties not present in the json will be kept as is. @@ -513,7 +513,7 @@ Further this method behaves similar to deserialize. ## primitive -[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/primitive.js#L17-L29 "Source code on GitHub") +[src/types/primitive.js:17-29](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/primitive.js#L17-L29 "Source code on GitHub") Indicates that this field contains a primitive value (or Date) which should be serialized literally to json. @@ -532,7 +532,7 @@ Returns **[ModelSchema](#modelschema)** ## identifier -[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/identifier.js#L42-L56 "Source code on GitHub") +[src/types/identifier.js:42-56](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/identifier.js#L42-L56 "Source code on GitHub") Similar to primitive, but this field will be marked as the identifier for the given Model type. This is used by for example `reference()` to serialize the reference @@ -572,13 +572,13 @@ Returns **PropSchema** ## date -[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/date.js#L8-L23 "Source code on GitHub") +[src/types/date.js:8-23](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/date.js#L8-L23 "Source code on GitHub") Similar to primitive, serializes instances of Date objects ## alias -[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/alias.js#L20-L31 "Source code on GitHub") +[src/types/alias.js:20-31](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/alias.js#L20-L31 "Source code on GitHub") Alias indicates that this model property should be named differently in the generated json. Alias should be the outermost propschema. @@ -603,7 +603,7 @@ Returns **PropSchema** ## custom -[src/types/custom.js:59-72](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/custom.js#L59-L72 "Source code on GitHub") +[src/types/custom.js:59-72](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/custom.js#L59-L72 "Source code on GitHub") Can be used to create simple custom propSchema. Multiple things can be done inside of a custom propSchema, like deserializing and serializing other (polymorphic) objects, skipping the serialization of something or checking the context of the obj being (de)serialized. @@ -614,7 +614,7 @@ The `serializer` function has the signature: When serializing the object `{a: 1}` the `serializer` function will be called with `serializer(1, 'a', {a: 1})`. -The `deserializer` function has the signature for synchronous processing +The `deserializer` function has the following signature for synchronous processing `(value, context, oldValue) => void` For asynchronous processing the function expects the following signature @@ -667,7 +667,7 @@ Returns **PropSchema** ## object -[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/object.js#L34-L52 "Source code on GitHub") +[src/types/object.js:34-52](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/object.js#L34-L52 "Source code on GitHub") `object` indicates that this property contains an object that needs to be (de)serialized using its own model schema. @@ -704,7 +704,7 @@ Returns **PropSchema** ## reference -[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/reference.js#L65-L98 "Source code on GitHub") +[src/types/reference.js:65-98](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/reference.js#L65-L98 "Source code on GitHub") `reference` can be used to (de)serialize references that point to other models. @@ -768,7 +768,7 @@ Returns **PropSchema** ## list -[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/list.js#L33-L54 "Source code on GitHub") +[src/types/list.js:33-54](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/list.js#L33-L54 "Source code on GitHub") List indicates that this property contains a list of things. Accepts a sub model schema to serialize the contents @@ -806,7 +806,7 @@ Returns **PropSchema** ## map -[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/map.js#L13-L62 "Source code on GitHub") +[src/types/map.js:13-62](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/map.js#L13-L62 "Source code on GitHub") Similar to list, but map represents a string keyed dynamic collection. This can be both plain objects (default) or ES6 Map like structures. @@ -818,7 +818,7 @@ This will be inferred from the initial value of the targetted attribute. ## mapAsArray -[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") +[src/types/mapAsArray.js:15-52](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/mapAsArray.js#L15-L52 "Source code on GitHub") Similar to map, mapAsArray can be used to serialize a map-like collection where the key is contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the Customer object has the id stored on itself. @@ -833,7 +833,7 @@ For ES6 maps this has the benefit of being allowed to have non-string keys in th ## raw -[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/types/raw.js#L15-L24 "Source code on GitHub") +[src/types/raw.js:15-24](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/types/raw.js#L15-L24 "Source code on GitHub") Indicates that this field is only need to putted in the serialized json or deserialized instance, without any transformations. Stay with its original value @@ -853,7 +853,7 @@ Returns **[ModelSchema](#modelschema)** ## SKIP -[src/constants.js:20-20](https://github.com/evoye/serializr/blob/97a818c936d62cc916057579f26979decedf7f95/src/constants.js#L20-L20 "Source code on GitHub") +[src/constants.js:20-20](https://github.com/evoye/serializr/blob/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/src/constants.js#L20-L20 "Source code on GitHub") In the event that a property needs to be deserialized, but not serialized, you can use the SKIP symbol to omit the property. This has to be used with the custom serializer. diff --git a/package.json b/package.json index 4e46853..cd827a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serializr", - "version": "1.2.1", + "version": "1.2.0", "description": "Serialize and deserialize complex object graphs to JSON", "main": "lib/serializr.js", "module": "lib/es/serializr.js",