@@ -2,12 +2,10 @@ package com.github.plokhotnyuk.jsoniter_scala.benchmark
2
2
3
3
import com .github .plokhotnyuk .jsoniter_scala .benchmark .SuitEnum .SuitEnum
4
4
import zio .json .JsonDecoder .JsonError
5
- import zio .json .internal .Lexer .{ NumberMaxBits , error }
6
- import zio .json .internal .{Lexer , RetractReader , StringMatrix , UnsafeNumbers , Write }
5
+ import zio .json .internal .Lexer .error
6
+ import zio .json .internal .{Lexer , RetractReader , SafeNumbers , StringMatrix , UnsafeNumbers , Write }
7
7
import zio .json .{DeriveJsonCodec , ExplicitEmptyCollections , JsonCodec , JsonCodecConfiguration , JsonDecoder , JsonEncoder }
8
-
9
8
import java .util .Base64
10
- import scala .collection .immutable .ArraySeq
11
9
12
10
object ZioJsonCodecs {
13
11
implicit val config : JsonCodecConfiguration = JsonCodecConfiguration (
@@ -75,14 +73,37 @@ object ZioJsonCodecs {
75
73
genTwitterAPIC3c
76
74
}
77
75
implicit val anyValsC3cr : JsonCodec [AnyVals ] = {
78
- implicit val c1 : JsonCodec [ByteVal ] = JsonCodec .byte.transform(ByteVal .apply, _.a)
79
- implicit val c2 : JsonCodec [ShortVal ] = JsonCodec .short.transform(ShortVal .apply, _.a)
80
- implicit val c3 : JsonCodec [IntVal ] = JsonCodec .int.transform(IntVal .apply, _.a)
81
- implicit val c4 : JsonCodec [LongVal ] = JsonCodec .long.transform(LongVal .apply, _.a)
82
- implicit val c5 : JsonCodec [BooleanVal ] = JsonCodec .boolean.transform(BooleanVal .apply, _.a)
83
- implicit val c6 : JsonCodec [CharVal ] = JsonCodec .char.transform(CharVal .apply, _.a)
84
- implicit val c7 : JsonCodec [DoubleVal ] = JsonCodec .double.transform(DoubleVal .apply, _.a)
85
- implicit val c8 : JsonCodec [FloatVal ] = JsonCodec .float.transform(FloatVal .apply, _.a)
76
+ implicit val c1 : JsonCodec [ByteVal ] = new JsonCodec [ByteVal ](
77
+ (a : ByteVal , _ : Option [Int ], out : Write ) => SafeNumbers .write(a.a.toInt, out),
78
+ (trace : List [JsonError ], in : RetractReader ) => new ByteVal (Lexer .byte(trace, in)))
79
+ implicit val c2 : JsonCodec [ShortVal ] = new JsonCodec [ShortVal ](
80
+ (a : ShortVal , _ : Option [Int ], out : Write ) => SafeNumbers .write(a.a.toInt, out),
81
+ (trace : List [JsonError ], in : RetractReader ) => new ShortVal (Lexer .short(trace, in)))
82
+ implicit val c3 : JsonCodec [IntVal ] = new JsonCodec [IntVal ](
83
+ (a : IntVal , _ : Option [Int ], out : Write ) => SafeNumbers .write(a.a, out),
84
+ (trace : List [JsonError ], in : RetractReader ) => new IntVal (Lexer .int(trace, in)))
85
+ implicit val c4 : JsonCodec [LongVal ] = new JsonCodec [LongVal ](
86
+ (a : LongVal , _ : Option [Int ], out : Write ) => SafeNumbers .write(a.a, out),
87
+ (trace : List [JsonError ], in : RetractReader ) => new LongVal (Lexer .long(trace, in)))
88
+ implicit val c5 : JsonCodec [BooleanVal ] = new JsonCodec [BooleanVal ](
89
+ (a : BooleanVal , _ : Option [Int ], out : Write ) => {
90
+ if (a.a) out.write('t' , 'r' , 'u' , 'e' )
91
+ else out.write('f' , 'a' , 'l' , 's' , 'e' )
92
+ },
93
+ (trace : List [JsonError ], in : RetractReader ) => new BooleanVal (Lexer .boolean(trace, in)))
94
+ implicit val c6 : JsonCodec [CharVal ] = new JsonCodec [CharVal ](
95
+ (a : CharVal , _ : Option [Int ], out : Write ) => {
96
+ out.write('"' )
97
+ out.write(a.a)
98
+ out.write('"' )
99
+ },
100
+ (trace : List [JsonError ], in : RetractReader ) => new CharVal (Lexer .char(trace, in)))
101
+ implicit val c7 : JsonCodec [DoubleVal ] = new JsonCodec [DoubleVal ](
102
+ (a : DoubleVal , _ : Option [Int ], out : Write ) => SafeNumbers .write(a.a, out),
103
+ (trace : List [JsonError ], in : RetractReader ) => new DoubleVal (Lexer .double(trace, in)))
104
+ implicit val c8 : JsonCodec [FloatVal ] = new JsonCodec [FloatVal ](
105
+ (a : FloatVal , _ : Option [Int ], out : Write ) => SafeNumbers .write(a.a, out),
106
+ (trace : List [JsonError ], in : RetractReader ) => new FloatVal (Lexer .float(trace, in)))
86
107
DeriveJsonCodec .gen
87
108
}
88
109
val base64C3c : JsonCodec [Array [Byte ]] = new JsonCodec [Array [Byte ]](
@@ -93,7 +114,7 @@ object ZioJsonCodecs {
93
114
},
94
115
(trace : List [JsonError ], in : RetractReader ) => Base64 .getDecoder.decode(Lexer .string(trace, in).toString))
95
116
val bigDecimalC3c : JsonCodec [BigDecimal ] = new JsonCodec [BigDecimal ](
96
- (a : BigDecimal , _ : Option [Int ], out : Write ) => out .write(a.toString ),
117
+ (a : BigDecimal , _ : Option [Int ], out : Write ) => SafeNumbers .write(a.bigDecimal, out ),
97
118
(trace : List [JsonError ], in : RetractReader ) => {
98
119
try {
99
120
val i = UnsafeNumbers .bigDecimal_(in, false , Int .MaxValue )
@@ -104,7 +125,10 @@ object ZioJsonCodecs {
104
125
}
105
126
})
106
127
val bigIntC3c : JsonCodec [BigInt ] = new JsonCodec [BigInt ](
107
- (a : BigInt , _ : Option [Int ], out : Write ) => out.write(a.toString),
128
+ (a : BigInt , _ : Option [Int ], out : Write ) => {
129
+ if (a.isValidLong) SafeNumbers .write(a.longValue, out)
130
+ else SafeNumbers .write(a.bigInteger, out)
131
+ },
108
132
(trace : List [JsonError ], in : RetractReader ) => {
109
133
try {
110
134
val i = BigInt (UnsafeNumbers .bigInteger_(in, false , Int .MaxValue ))
@@ -136,20 +160,18 @@ object ZioJsonCodecs {
136
160
implicit val missingRequiredFieldsC3c : JsonCodec [MissingRequiredFields ] = DeriveJsonCodec .gen
137
161
implicit val primitivesC3c : JsonCodec [Primitives ] = DeriveJsonCodec .gen
138
162
implicit val enumADTsC3c : JsonCodec [SuitADT ] = DeriveJsonCodec .gen
139
- implicit val enumsC3c : JsonCodec [SuitEnum ] = new JsonCodec (new JsonEncoder [ SuitEnum ] {
140
- override def unsafeEncode (a : SuitEnum , indent : Option [Int ], out : Write ): Unit = {
163
+ implicit val enumsC3c : JsonCodec [SuitEnum ] = new JsonCodec (
164
+ (a : SuitEnum , _ : Option [Int ], out : Write ) => {
141
165
out.write('"' )
142
166
out.write(a.toString)
143
167
out.write('"' )
144
- }
145
- }, new JsonDecoder [SuitEnum ] {
146
- private [this ] val values = SuitEnum .values.toArray
147
- private [this ] val matrix = new StringMatrix (values.map(_.toString))
148
-
149
- override def unsafeDecode (trace : List [JsonError ], in : RetractReader ): SuitEnum = {
150
- val idx = Lexer .enumeration(trace, in, matrix)
151
- if (idx == - 1 ) Lexer .error(" SuitEnum" , trace)
152
- values(idx)
153
- }
154
- })
168
+ }, {
169
+ val values = SuitEnum .values.toArray
170
+ val matrix = new StringMatrix (values.map(_.toString))
171
+ (trace : List [JsonError ], in : RetractReader ) => {
172
+ val idx = Lexer .enumeration(trace, in, matrix)
173
+ if (idx == - 1 ) Lexer .error(" SuitEnum" , trace)
174
+ values(idx)
175
+ }
176
+ })
155
177
}
0 commit comments