You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`json-decoder` allows TypeScript and JavaScript projects to adorn class declarations with JSON decoding decorators to support automatic decoding and marshalling of JSON objects to full prototype objects.
This module is written using [TypeScript](http://typescriptlang.org) and distributed with the following JavaScript support:
21
-
22
-
-[`json-decoder/dist`](./master/node/dist)
23
-
24
-
A TypeScript compiler is not required to consume this module
16
+
| 0.4.x | In Progress |
25
17
26
18
## Usage
27
19
@@ -62,7 +54,7 @@ Prototype properties can be decorated with `@jsonProperty`, inheriting the proto
62
54
> // Mapped 'id' property
63
55
> @jsonProperty
64
56
> public readonly id: number
65
-
>
57
+
>
66
58
> // Mapped 'name' property
67
59
> @jsonProperty
68
60
> public readonly name: string
@@ -74,7 +66,7 @@ To decode an `Account` class, use `JsonDecoder.decode` with a JSON object (or J
74
66
>```Javascript
75
67
> import { JsonDecoder } from 'json-decoder'
76
68
> import { Account } from './account'
77
-
>
69
+
>
78
70
> const json = {
79
71
> id: 1001,
80
72
> name: 'John Smith',
@@ -99,7 +91,7 @@ The following uses the decorator describing the JSON property `accountId` mapped
99
91
> // Alias 'id' to 'accountId' property
100
92
> @jsonAliasProperty('accountId')
101
93
> public readonly id: number
102
-
>
94
+
>
103
95
> // ...
104
96
> }
105
97
>```
@@ -109,7 +101,7 @@ Note the JSON object below is using `accountId` instead of `id`
109
101
>```Javascript
110
102
> import { JsonDecoder } from 'json-decoder'
111
103
> import { Account } from './account'
112
-
>
104
+
>
113
105
> const json = {
114
106
> accountId: 1001,
115
107
> name: 'John Smith',
@@ -148,7 +140,7 @@ The of interest `name` properties can be flatten
148
140
>
149
141
> @jsonAliasProperty('name.fullName')
150
142
> public readonly fullName: string
151
-
>
143
+
>
152
144
> // ...
153
145
> }
154
146
> ```
@@ -175,26 +167,26 @@ The decode could map index `0` of `profiles` by using `profiles@0`. At the same
175
167
> @jsonDecodable()
176
168
> class Account {
177
169
> // ...
178
-
>
170
+
>
179
171
> @jsonAliasProperty('profiles@0')
180
172
> public readonly primaryProfile: number
181
-
>
173
+
>
182
174
> @jsonProperty
183
175
> public readonly profiles: Array<number>
184
-
>
176
+
>
185
177
> // ...
186
178
> }
187
179
> ```
188
180
189
181
Key paths can be a long as the JSON supports. `property.arr@0.anotherProperty.YAP` will attempt to look up `YAP`, the end value. If at any point any sub-property returns `undefined` then `undefined` will be returned. If `null` is returned for any but the last property then `undefined` will be returned, else `null` will be returned.
190
182
191
-
`@` and `.` are synonmous with one another (subject to change) but provide a more readable structure of intent. That is '.' sub-property, '@' index. `a.b.c` is the same as `a@b@c`.
183
+
`@` and `.` are synonmous with one another (subject to change) but provide a more readable structure of intent. That is '.' sub-property, '@' index. `a.b.c` is the same as `a@b@c`.
192
184
193
185
### Root Indexing
194
186
195
187
As a convenience, you may use `@index` as an alias, which will attempt to map a source JSON object that is an array, at `index` to the class prototype property.
196
188
197
-
Using an example of a message protocol from a network request, the message is composed of a `[header, body]` structure.
189
+
Using an example of a message protocol from a network request, the message is composed of a `[header, body]` structure.
198
190
199
191
> ```Javascript
200
192
> [
@@ -211,10 +203,10 @@ Decoding the above message use `@jsonAliasProperty('@1.name')`, where `@1` repre
211
203
> @jsonDecodable()
212
204
> class AccountMessage {
213
205
> // ...
214
-
>
206
+
>
215
207
> @jsonAliasProperty('@1.name')
216
208
> public readonly name: string
217
-
>
209
+
>
218
210
> // ...
219
211
> }
220
212
> ```
@@ -267,15 +259,15 @@ To marshal `profiles` into an `Array` of `Number`
267
259
> @jsonDecodable()
268
260
> class Account {
269
261
> // ...
270
-
>
262
+
>
271
263
> @jsonAliasProperty('profiles', [Number])
272
264
> public readonly profiles: Array<number>
273
-
>
265
+
>
274
266
> // ...
275
267
> }
276
268
> ```
277
269
278
-
Array marshalling can also convert a single property value into an array. If the JSON object only had a single scalar property value instead, it will creating an `Array` of one element.
270
+
Array marshalling can also convert a single property value into an array. If the JSON object only had a single scalar property value instead, it will creating an `Array` of one element.
279
271
280
272
All element marshaling still apply here too
281
273
@@ -303,7 +295,7 @@ Finally, with marshaling arrays, a JSON property with an array can be marshalled
0 commit comments