@@ -15,6 +15,7 @@ export class Encoder<ContextType = undefined> {
15
15
public constructor (
16
16
private readonly extensionCodec : ExtensionCodecType < ContextType > = ExtensionCodec . defaultCodec as any ,
17
17
private readonly context : ContextType = undefined as any ,
18
+ private readonly useBigInt64 = false ,
18
19
private readonly maxDepth = DEFAULT_MAX_DEPTH ,
19
20
private readonly initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE ,
20
21
private readonly sortKeys = false ,
@@ -57,9 +58,15 @@ export class Encoder<ContextType = undefined> {
57
58
} else if ( typeof object === "boolean" ) {
58
59
this . encodeBoolean ( object ) ;
59
60
} else if ( typeof object === "number" ) {
60
- this . encodeNumber ( object ) ;
61
+ if ( ! this . forceIntegerToFloat ) {
62
+ this . encodeNumber ( object ) ;
63
+ } else {
64
+ this . encodeNumberAsFloat ( object ) ;
65
+ }
61
66
} else if ( typeof object === "string" ) {
62
67
this . encodeString ( object ) ;
68
+ } else if ( this . useBigInt64 && typeof object === "bigint" ) {
69
+ this . encodeBigInt64 ( object ) ;
63
70
} else {
64
71
this . encodeObject ( object , depth ) ;
65
72
}
@@ -95,8 +102,9 @@ export class Encoder<ContextType = undefined> {
95
102
this . writeU8 ( 0xc3 ) ;
96
103
}
97
104
}
98
- private encodeNumber ( object : number ) {
99
- if ( Number . isSafeInteger ( object ) && ! this . forceIntegerToFloat ) {
105
+
106
+ private encodeNumber ( object : number ) : void {
107
+ if ( ! this . forceIntegerToFloat && Number . isSafeInteger ( object ) ) {
100
108
if ( object >= 0 ) {
101
109
if ( object < 0x80 ) {
102
110
// positive fixint
@@ -113,10 +121,12 @@ export class Encoder<ContextType = undefined> {
113
121
// uint 32
114
122
this . writeU8 ( 0xce ) ;
115
123
this . writeU32 ( object ) ;
116
- } else {
124
+ } else if ( ! this . useBigInt64 ) {
117
125
// uint 64
118
126
this . writeU8 ( 0xcf ) ;
119
127
this . writeU64 ( object ) ;
128
+ } else {
129
+ this . encodeNumberAsFloat ( object ) ;
120
130
}
121
131
} else {
122
132
if ( object >= - 0x20 ) {
@@ -134,23 +144,40 @@ export class Encoder<ContextType = undefined> {
134
144
// int 32
135
145
this . writeU8 ( 0xd2 ) ;
136
146
this . writeI32 ( object ) ;
137
- } else {
147
+ } else if ( ! this . useBigInt64 ) {
138
148
// int 64
139
149
this . writeU8 ( 0xd3 ) ;
140
150
this . writeI64 ( object ) ;
151
+ } else {
152
+ this . encodeNumberAsFloat ( object ) ;
141
153
}
142
154
}
143
155
} else {
144
- // non-integer numbers
145
- if ( this . forceFloat32 ) {
146
- // float 32
147
- this . writeU8 ( 0xca ) ;
148
- this . writeF32 ( object ) ;
149
- } else {
150
- // float 64
151
- this . writeU8 ( 0xcb ) ;
152
- this . writeF64 ( object ) ;
153
- }
156
+ this . encodeNumberAsFloat ( object ) ;
157
+ }
158
+ }
159
+
160
+ private encodeNumberAsFloat ( object : number ) : void {
161
+ if ( this . forceFloat32 ) {
162
+ // float 32
163
+ this . writeU8 ( 0xca ) ;
164
+ this . writeF32 ( object ) ;
165
+ } else {
166
+ // float 64
167
+ this . writeU8 ( 0xcb ) ;
168
+ this . writeF64 ( object ) ;
169
+ }
170
+ }
171
+
172
+ private encodeBigInt64 ( object : bigint ) : void {
173
+ if ( object >= BigInt ( 0 ) ) {
174
+ // uint 64
175
+ this . writeU8 ( 0xcf ) ;
176
+ this . writeBigUint64 ( object ) ;
177
+ } else {
178
+ // int 64
179
+ this . writeU8 ( 0xd3 ) ;
180
+ this . writeBigInt64 ( object ) ;
154
181
}
155
182
}
156
183
@@ -377,12 +404,14 @@ export class Encoder<ContextType = undefined> {
377
404
378
405
private writeF32 ( value : number ) {
379
406
this . ensureBufferSizeToWrite ( 4 ) ;
407
+
380
408
this . view . setFloat32 ( this . pos , value ) ;
381
409
this . pos += 4 ;
382
410
}
383
411
384
412
private writeF64 ( value : number ) {
385
413
this . ensureBufferSizeToWrite ( 8 ) ;
414
+
386
415
this . view . setFloat64 ( this . pos , value ) ;
387
416
this . pos += 8 ;
388
417
}
@@ -400,4 +429,18 @@ export class Encoder<ContextType = undefined> {
400
429
setInt64 ( this . view , this . pos , value ) ;
401
430
this . pos += 8 ;
402
431
}
432
+
433
+ private writeBigUint64 ( value : bigint ) {
434
+ this . ensureBufferSizeToWrite ( 8 ) ;
435
+
436
+ this . view . setBigUint64 ( this . pos , value ) ;
437
+ this . pos += 8 ;
438
+ }
439
+
440
+ private writeBigInt64 ( value : bigint ) {
441
+ this . ensureBufferSizeToWrite ( 8 ) ;
442
+
443
+ this . view . setBigInt64 ( this . pos , value ) ;
444
+ this . pos += 8 ;
445
+ }
403
446
}
0 commit comments