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..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/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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/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:36-45](https://github.com/mobxjs/serializr/blob/8f0e459a1354a7f253a5c1c746269bfa71044093/src/types/custom.js#L36-L45 "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,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 following 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** ## 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/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. @@ -682,7 +704,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/27e5a4ce9cd83f6fa0211f738517b61a2b1a4458/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 +768,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/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 @@ -784,7 +806,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/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. @@ -796,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/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/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. @@ -811,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/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/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 @@ -831,7 +853,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/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 a88e812..cd827a6 100644 --- a/package.json +++ b/package.json @@ -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 d822e4c..2252265 100644 --- a/serializr.d.ts +++ b/serializr.d.ts @@ -71,7 +71,8 @@ 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 custom(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..86fa5a9 100644 --- a/src/types/custom.js +++ b/src/types/custom.js @@ -1,4 +1,4 @@ -import { invariant } 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,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 following 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,15 @@ 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"), "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 { + done(null, deserializer(jsonValue, context, oldValue)) + } } } } diff --git a/test/typescript/ts.ts b/test/typescript/ts.ts index c9ebe3c..4338dc8 100644 --- a/test/typescript/ts.ts +++ b/test/typescript/ts.ts @@ -1,4 +1,16 @@ -import {serializable, alias, list, object, identifier, reference, primitive, serialize, deserialize, serializeAll} from "../../"; +import { + serializable, + alias, + list, + object, + identifier, + reference, + primitive, + serialize, + deserialize, + serializeAll, + custom +} from "../../"; import {observable, autorun} from "mobx"; declare var require; @@ -26,19 +38,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 +58,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 +148,43 @@ 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, context, oldValue, done) { + console.log(context, oldValue, done) + done(null, jsonValue) + } + + class A { + @serializable(custom(customSerializer, customDeserializer)) a = "hoi"; + @serializable(custom(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)