FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codablejson/codablejson/Coder.ts at main · pie6k/codablejson · GitHub
pie6k
codablejson
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codablejson
/
codablejson
/
Coder.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
300 lines (222 loc) · 8.33 KB
Breadcrumbs
codablejson
/
codablejson
/
Coder.ts
Copy path
File metadata and controls
300 lines (222 loc) · 8.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import
*
as
builtinTypesMap
from
"./builtin"
;
import
{
CodableType
,
CodableTypeOptions
,
codableType
,
defaultCodableReader
,
getIsCodableType
}
from
"./CodableType"
;
import
{
DecodeContext
,
DecodeOptions
}
from
"./DecodeContext"
;
import
{
EncodeContext
,
EncodeOptions
}
from
"./EncodeContext"
;
import
{
Path
,
ROOT_PATH
,
splitPath
}
from
"./utils/path"
;
import
{
Transformer
,
getIsSuperJSONResult
,
getSuperjsonTransformer
}
from
"./compat"
;
import
{
getCodableClassType
,
getIsCodableClass
}
from
"./decorators/registry"
;
import
{
getIsObject
,
getIsRecord
}
from
"./is"
;
import
{
$$externalReference
}
from
"./ExternalReference"
;
import
{
AnyClass
}
from
"./decorators/types"
;
import
{
JSONValue
}
from
"./types"
;
import
{
assertGet
}
from
"./utils/assert"
;
import
{
consumeArray
}
from
"./utils/misc"
;
import
{
decodeInput
}
from
"./decode"
;
import
{
getIsTagKey
}
from
"./format"
;
import
{
performEncode
}
from
"./encode"
;
import
{
resolveCodableDependencies
}
from
"./dependencies"
;
import
{
temporalTypes
}
from
"./temporal"
;
const
typeCandidates
:
Array
<
CodableType
|
Array
<
CodableType
>
>
=
[
...
Object
.
values
(
builtinTypesMap
)
,
...
Object
.
values
(
temporalTypes
??
{
}
)
,
$$externalReference
,
]
;
const
BUILTIN_TYPES
=
typeCandidates
.
map
(
(
codableTypeOrCodableTypes
)
=>
{
if
(
Array
.
isArray
(
codableTypeOrCodableTypes
)
)
{
return
codableTypeOrCodableTypes
;
}
return
[
codableTypeOrCodableTypes
]
;
}
)
.
flat
(
)
.
filter
(
getIsCodableType
)
;
const
DEFAULT_TYPES
=
[
...
BUILTIN_TYPES
]
.
filter
(
getIsCodableType
)
;
function
getSortedTypes
(
types
:
CodableType
[
]
)
{
return
types
.
sort
(
(
a
,
b
)
=>
{
return
b
.
priority
-
a
.
priority
;
}
)
;
}
function
fillMapWithTypes
(
map
:
Map
<
string
,
CodableType
>
,
types
:
CodableType
[
]
)
{
map
.
clear
(
)
;
for
(
const
type
of
types
)
{
if
(
map
.
has
(
type
.
name
)
)
{
throw
new
Error
(
`Coder type "
${
type
.
name
}
" already registered`
)
;
}
map
.
set
(
type
.
name
,
type
)
;
}
}
function
createTypesMap
(
types
:
CodableType
[
]
)
{
const
sortedTypes
=
getSortedTypes
(
types
)
;
const
map
=
new
Map
<
string
,
CodableType
>
(
)
;
fillMapWithTypes
(
map
,
sortedTypes
)
;
return
map
;
}
type
CodableTypeOrClass
=
CodableType
|
AnyClass
;
function
updateTypesOrderByPriority
(
currentTypes
:
Map
<
string
,
CodableType
>
)
{
const
sortedTypes
=
getSortedTypes
(
[
...
currentTypes
.
values
(
)
]
)
;
const
needsReordering
=
sortedTypes
.
some
(
(
type
)
=>
!
type
.
hasDefaultPriority
)
;
if
(
!
needsReordering
)
return
;
fillMapWithTypes
(
currentTypes
,
sortedTypes
)
;
}
export
class
Coder
{
private
readonly
typesMap
=
new
Map
<
string
,
CodableType
>
(
)
;
private
readonly
codableTypeByClassMap
=
new
Map
<
AnyClass
,
CodableType
>
(
)
;
constructor
(
extraTypes
:
CodableTypeOrClass
[
]
=
[
]
)
{
this
.
typesMap
=
createTypesMap
(
[
...
DEFAULT_TYPES
]
)
;
this
.
register
(
...
extraTypes
)
;
}
get
transformer
(
)
:
Transformer
{
return
{
serialize
:
(
value
:
any
)
=>
this
.
encode
(
value
)
,
deserialize
:
(
value
:
any
)
=>
this
.
decode
(
value
)
,
}
;
}
private
refreshCodableTypeByClassMap
(
)
{
this
.
codableTypeByClassMap
.
clear
(
)
;
for
(
const
type
of
this
.
typesMap
.
values
(
)
)
{
if
(
type
.
classes
)
{
for
(
const
Class
of
type
.
classes
)
{
this
.
codableTypeByClassMap
.
set
(
Class
,
type
)
;
}
}
}
}
private
organizeTypes
(
)
{
updateTypesOrderByPriority
(
this
.
typesMap
)
;
this
.
refreshCodableTypeByClassMap
(
)
;
}
getTypeByName
(
name
:
string
)
:
CodableType
|
null
{
return
this
.
typesMap
.
get
(
name
)
??
null
;
}
private
getHasType
(
type
:
CodableType
)
:
boolean
{
const
existingType
=
this
.
getTypeByName
(
type
.
name
)
;
return
type
===
existingType
;
}
register
(
...
typesOrClasses
:
CodableTypeOrClass
[
]
)
{
if
(
typesOrClasses
.
length
===
0
)
return
;
if
(
this
.
isDefault
)
{
throw
new
Error
(
"Cannot register types on the default coder. Create a custom coder instance using `new Coder()` and register types on that instance."
,
)
;
}
const
dependenciesToRegister
=
new
Set
<
CodableType
>
(
)
;
for
(
const
typeOrClass
of
typesOrClasses
)
{
const
codableType
=
getIsCodableClass
(
typeOrClass
)
?
assertGet
(
getCodableClassType
(
typeOrClass
)
,
`Codable class "
${
typeOrClass
.
name
}
" not registered`
)
:
typeOrClass
;
if
(
this
.
getHasType
(
codableType
)
)
continue
;
if
(
this
.
typesMap
.
has
(
codableType
.
name
)
)
{
throw
new
Error
(
`Other codable type with name "
${
codableType
.
name
}
" already registered`
)
;
}
this
.
typesMap
.
set
(
codableType
.
name
,
codableType
)
;
for
(
const
dependency
of
resolveCodableDependencies
(
codableType
)
)
{
dependenciesToRegister
.
add
(
dependency
)
;
}
}
this
.
register
(
...
dependenciesToRegister
)
;
this
.
organizeTypes
(
)
;
}
addType
<
Item
,
Data
>
(
name
:
string
,
canEncode
:
(
value
:
unknown
)
=>
value
is
Item
,
encode
:
(
data
:
Item
)
=>
Data
,
decode
:
(
data
:
Data
)
=>
Item
,
options
?:
CodableTypeOptions
<
Item
>
,
)
{
return
this
.
register
(
codableType
(
name
,
canEncode
,
encode
,
decode
,
options
)
)
;
}
encode
<
T
>
(
value
:
T
,
options
?:
EncodeOptions
)
:
JSONValue
{
const
encodeContext
=
new
EncodeContext
(
this
,
options
)
;
const
result
=
performEncode
(
value
,
encodeContext
)
;
encodeContext
.
finalize
(
)
;
return
result
;
}
decode
<
T
>
(
value
:
JSONValue
,
options
?:
DecodeOptions
)
:
T
{
if
(
getIsSuperJSONResult
(
value
)
)
{
const
transformer
=
getSuperjsonTransformer
(
)
;
if
(
transformer
)
return
transformer
.
deserialize
(
value
)
as
T
;
console
.
warn
(
`Seems you are decoding SuperJSON encoded data. Please enable compatibility mode by importing "codablejson/superjson" somewhere early in your code.`
,
)
;
}
const
context
=
new
DecodeContext
(
this
,
options
)
;
const
result
=
decodeInput
<
T
>
(
value
,
context
,
this
,
ROOT_PATH
)
;
this
.
resolvePendingReferencesInOutput
(
result
,
context
)
;
return
result
;
}
stringify
<
T
>
(
value
:
T
,
prettyPrint
?:
boolean
)
:
string
{
return
JSON
.
stringify
(
this
.
encode
(
value
)
,
null
,
prettyPrint
?
2
:
undefined
)
;
}
parse
<
T
>
(
value
:
string
)
:
T
{
return
this
.
decode
(
JSON
.
parse
(
value
)
)
;
}
clone
<
T
>
(
value
:
T
)
:
T
{
return
this
.
decode
<
T
>
(
this
.
encode
(
value
)
)
;
}
getMatchingTypeForObject
<
T
extends
object
>
(
input
:
T
)
:
CodableType
<
T
>
|
null
{
const
codableTypeByClass
=
this
.
codableTypeByClassMap
.
get
(
input
.
constructor
as
AnyClass
)
;
if
(
codableTypeByClass
?.
canHandle
(
input
)
)
{
return
codableTypeByClass
;
}
for
(
const
type
of
this
.
typesMap
.
values
(
)
)
{
if
(
type
.
canHandle
(
input
)
)
{
return
type
;
}
}
return
null
;
}
get
isDefault
(
)
{
return
this
===
codablejson
;
}
private
resolvePendingReferencesInOutput
(
output
:
any
,
context
:
DecodeContext
)
{
for
(
const
[
refId
,
paths
]
of
context
.
pendingReferences
)
{
const
referenceTarget
=
context
.
resolvedRefs
.
get
(
refId
)
;
if
(
!
referenceTarget
)
continue
;
for
(
const
path
of
paths
)
{
const
didUpdate
=
this
.
updateValueInOutput
(
output
,
path
,
referenceTarget
)
;
if
(
!
didUpdate
)
{
console
.
warn
(
`Failed to update value at path
${
path
}
`
)
;
}
}
}
}
private
updateValueInOutput
(
root
:
object
,
path
:
Path
,
value
:
unknown
)
{
const
segmentsConsumer
=
consumeArray
(
splitPath
(
path
)
,
getIsTagKey
)
;
let
current
=
root
;
while
(
true
)
{
const
matchingType
=
this
.
getMatchingTypeForObject
(
current
)
;
const
reader
=
matchingType
?.
reader
??
defaultCodableReader
;
const
{
get
,
set
}
=
reader
(
current
,
segmentsConsumer
)
;
if
(
segmentsConsumer
.
isDone
)
{
// We are at the end of the path, so we can set the value
return
set
(
value
)
;
}
// Read the value and keep traversing the path
const
result
=
get
(
)
;
if
(
!
getIsObject
(
result
)
)
{
console
.
warn
(
`Expected object at path
${
path
}
, got
${
typeof
result
}
`
)
;
return
false
;
}
current
=
result
;
}
}
}
export
function
createCoder
(
extraTypes
:
CodableType
[
]
=
[
]
)
{
return
new
Coder
(
extraTypes
)
;
}
export
const
codablejson
=
createCoder
(
)
;
export
function
decode
<
T
>
(
value
:
JSONValue
,
options
?:
DecodeOptions
)
:
T
{
return
codablejson
.
decode
(
value
,
options
)
;
}
export
function
encode
<
T
>
(
value
:
T
)
:
JSONValue
{
return
codablejson
.
encode
(
value
)
;
}
export
function
stringify
<
T
>
(
value
:
T
,
prettyPrint
?:
boolean
)
:
string
{
return
codablejson
.
stringify
(
value
,
prettyPrint
)
;
}
export
function
parse
<
T
>
(
value
:
string
)
:
T
{
return
codablejson
.
parse
(
value
)
;
}
export
function
clone
<
T
>
(
value
:
T
)
:
T
{
return
codablejson
.
clone
(
value
)
;
}
Back
|
FazBrowse Home
|
New Git URL