|
1 |
| -'use strict'; |
| 1 | +import { |
| 2 | + ISerializePropertiesMapper, |
| 3 | + IDeserializePropertiesMapper, |
| 4 | + TJsonaDenormalizedIncludeNames, |
| 5 | + TJsonaNormalizedIncludeNamesTree, |
| 6 | + TJsonaModel, |
| 7 | + TJsonApiBody |
| 8 | +} from './JsonaTypes'; |
| 9 | + |
| 10 | +import {jsonParse} from './utils'; |
| 11 | +import ModelsSerializer from './builders/ModelsSerializer'; |
| 12 | +import JsonDeserializer from './builders/JsonDeserializer'; |
2 | 13 |
|
3 | 14 | import {
|
4 |
| - IJsonaModel, |
5 |
| - IJsonaSerializeParams, |
6 |
| - IJsonaModelsFactory, |
7 |
| - IJsonaIncludeTree, |
8 |
| - IJsonaRequestedFields, |
9 |
| - IJsonApiBody |
10 |
| -} from './JsonaInterfaces'; |
11 |
| - |
12 |
| -import JsonBuilder from './helpers/JsonBuilder'; |
13 |
| -import ModelBuilder from './helpers/ModelBuilder'; |
14 |
| -import SimpleModelsFactory from './helpers/SimpleModelsFactory'; |
15 |
| -import isIncludeTree from './helpers/isIncludeTree'; |
| 15 | + SerializePropertiesMapper, |
| 16 | + DeserializePropertiesMapper |
| 17 | +} from './simplePropertyMappers'; |
16 | 18 |
|
17 | 19 | class Jsona {
|
18 | 20 |
|
19 |
| - protected modelsFactory: IJsonaModelsFactory; |
20 |
| - protected propertiesMapper: IJsonaModel; |
| 21 | + public serializePropertiesMapper: ISerializePropertiesMapper; |
| 22 | + public deserializePropertiesMapper: IDeserializePropertiesMapper; |
21 | 23 |
|
22 |
| - /** |
23 |
| - * Create a Jsona Service instance |
24 |
| - * @param {IJsonaModelsFactory} modelsFactory - The factory, that will provide instance for each of JSON API entities, |
25 |
| - * that will be serialized or deserialized |
26 |
| - */ |
27 |
| - constructor( |
28 |
| - modelsFactory: IJsonaModelsFactory = new SimpleModelsFactory() |
29 |
| - ) { |
30 |
| - this.modelsFactory = modelsFactory; |
31 |
| - } |
| 24 | + constructor({ |
| 25 | + serializePropertiesMapper = new SerializePropertiesMapper(), |
| 26 | + deserializePropertiesMapper = new DeserializePropertiesMapper() |
| 27 | + }) { |
| 28 | + this.serializePropertiesMapper = serializePropertiesMapper; |
| 29 | + this.deserializePropertiesMapper = deserializePropertiesMapper; |
| 30 | + } |
32 | 31 |
|
33 | 32 | /**
|
34 |
| - * Serialize models, and additional data to JSON API compatible object. |
35 |
| - * @param {IJsonaModel} params.item - Filled with data model, |
36 |
| - that will be used to build a "data" and "include" parts. |
37 |
| - * @param {Array<IJsonaModel>} params.collection - Array of filled with data models, |
38 |
| - * that will be used to build a "data" and "include" parts. |
39 |
| - * @param {Object} params.meta - any JSON-compatible data |
40 |
| - * @param {Object} params.error - any JSON-compatible data |
41 |
| - * @param {IJsonaIncludeTree} params.requestedIncludes - object must describe what data should be put in "included" part of JSON. |
42 |
| - * @param {IJsonaRequestedFields} params.requestedFields - object must describe what attributes of entities should be put |
43 |
| - * in "data" and "included" parts of JSON. |
44 |
| - * @param {boolean} params.stringify - Stringify results object or not. |
45 |
| - * @return {IJsonApiBody} |
| 33 | + * serialize |
| 34 | + * Creates JSON, compatible with json:api specification from Jsona model(s). |
46 | 35 | */
|
47 |
| - serialize(params: IJsonaSerializeParams): IJsonApiBody { |
48 |
| - var item: IJsonaModel = params.item; |
49 |
| - var collection: IJsonaModel[] = params.collection; |
50 |
| - var meta: Object = params.meta; |
51 |
| - var error: Object = params.error; |
52 |
| - var requestedIncludes: IJsonaIncludeTree = params.requestedIncludes; |
53 |
| - var withAllIncludes: boolean = params.withAllIncludes; |
54 |
| - var requestedFields: IJsonaRequestedFields = params.requestedFields; |
55 |
| - |
56 |
| - var jsonBuilder = new JsonBuilder(); |
57 |
| - |
58 |
| - !!requestedFields && jsonBuilder.setRequestedFields(requestedFields); |
59 |
| - !!requestedIncludes && jsonBuilder.setRequestedIncludesTree(requestedIncludes); |
60 |
| - !!withAllIncludes && jsonBuilder.setWithAllIncludes(withAllIncludes); |
61 |
| - |
62 |
| - !!item && jsonBuilder.setItem(item); |
63 |
| - !!collection && jsonBuilder.setCollection(collection); |
64 |
| - !!meta && jsonBuilder.setMeta(meta); |
65 |
| - !!error && jsonBuilder.setError(error); |
66 |
| - |
67 |
| - return jsonBuilder.buildBody(); |
68 |
| - } |
69 |
| - |
70 |
| - deserialize(body: IJsonApiBody | string) { |
71 |
| - var modelBuilder = new ModelBuilder(); |
72 |
| - |
73 |
| - if (typeof body === 'string') { |
74 |
| - modelBuilder.setJsonParsedObject(this.jsonParse(body)); |
75 |
| - } else { |
76 |
| - modelBuilder.setJsonParsedObject(body); |
| 36 | + serialize( |
| 37 | + {stuff, includeNames}: { |
| 38 | + stuff: TJsonaModel | Array<TJsonaModel>, |
| 39 | + includeNames: TJsonaDenormalizedIncludeNames | TJsonaNormalizedIncludeNamesTree |
77 | 40 | }
|
78 |
| - |
79 |
| - modelBuilder.setModelsFactory(this.modelsFactory); |
80 |
| - |
81 |
| - var deserialized = { |
82 |
| - hasItem: false, |
83 |
| - hasCollection: false, |
84 |
| - item: null, |
85 |
| - collection: null |
86 |
| - }; |
87 |
| - |
88 |
| - if (modelBuilder.hasItem()) { |
89 |
| - deserialized['hasItem'] = true; |
90 |
| - deserialized['item'] = modelBuilder.buildItem(); |
91 |
| - } else if (modelBuilder.hasCollection()) { |
92 |
| - deserialized['hasCollection'] = true; |
93 |
| - deserialized['collection'] = modelBuilder.buildCollection(); |
| 41 | + ): TJsonApiBody { |
| 42 | + if (!stuff) { |
| 43 | + throw new Error('Jsona can not serialize, stuff is not passed'); |
| 44 | + } |
| 45 | + if (!this.serializePropertiesMapper) { |
| 46 | + throw new Error('Jsona can not serialize, serializePropertiesMapper is not set'); |
94 | 47 | }
|
95 | 48 |
|
96 |
| - return deserialized; |
97 |
| - } |
| 49 | + const jsonBuilder = new ModelsSerializer(this.serializePropertiesMapper); |
98 | 50 |
|
99 |
| - jsonParse(stringified: string): Object { |
100 |
| - var parsed; |
| 51 | + jsonBuilder.setStuff(stuff); |
101 | 52 |
|
102 |
| - try { |
103 |
| - parsed = JSON.parse(stringified); |
104 |
| - } catch (e) { |
105 |
| - parsed = {}; |
| 53 | + if (includeNames) { |
| 54 | + jsonBuilder.setIncludeNames(includeNames); |
106 | 55 | }
|
107 | 56 |
|
108 |
| - return parsed; |
| 57 | + return jsonBuilder.build(); |
109 | 58 | }
|
110 | 59 |
|
111 |
| - jsonStringify(object: string): Object { |
112 |
| - var stringified; |
| 60 | + /** |
| 61 | + * deserialize |
| 62 | + * Creates Jsona model(s) from JSON, compatible with json:api specification. |
| 63 | + */ |
| 64 | + deserialize(body: TJsonApiBody | string): TJsonaModel | Array<TJsonaModel> { |
| 65 | + if (!body) { |
| 66 | + throw new Error('Jsona can not deserialize, body is not passed'); |
| 67 | + } |
| 68 | + if (!this.deserializePropertiesMapper) { |
| 69 | + throw new Error('Jsona can not deserialize, deserializePropertiesMapper is not set'); |
| 70 | + } |
| 71 | + |
| 72 | + const modelBuilder = new JsonDeserializer(this.deserializePropertiesMapper); |
113 | 73 |
|
114 |
| - try { |
115 |
| - stringified = JSON.stringify(object); |
116 |
| - } catch (e) { |
117 |
| - stringified = '{}'; |
| 74 | + if (typeof body === 'string') { |
| 75 | + modelBuilder.setJsonParsedObject(jsonParse(body)); |
| 76 | + } else { |
| 77 | + modelBuilder.setJsonParsedObject(body); |
118 | 78 | }
|
119 | 79 |
|
120 |
| - return stringified; |
| 80 | + return modelBuilder.build(); |
121 | 81 | }
|
122 | 82 |
|
123 |
| - isIncludeTree(some: any): boolean { |
124 |
| - return isIncludeTree(some); |
125 |
| - } |
126 | 83 | }
|
127 | 84 |
|
128 | 85 | export default Jsona;
|
0 commit comments