@@ -2,12 +2,10 @@ package com.github.plokhotnyuk.jsoniter_scala.benchmark
22
33import com .github .plokhotnyuk .jsoniter_scala .benchmark .SuitEnum .SuitEnum
44import 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 }
77import zio .json .{DeriveJsonCodec , ExplicitEmptyCollections , JsonCodec , JsonCodecConfiguration , JsonDecoder , JsonEncoder }
8-
98import java .util .Base64
10- import scala .collection .immutable .ArraySeq
119
1210object ZioJsonCodecs {
1311 implicit val config : JsonCodecConfiguration = JsonCodecConfiguration (
@@ -75,14 +73,37 @@ object ZioJsonCodecs {
7573 genTwitterAPIC3c
7674 }
7775 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)))
86107 DeriveJsonCodec .gen
87108 }
88109 val base64C3c : JsonCodec [Array [Byte ]] = new JsonCodec [Array [Byte ]](
@@ -93,7 +114,7 @@ object ZioJsonCodecs {
93114 },
94115 (trace : List [JsonError ], in : RetractReader ) => Base64 .getDecoder.decode(Lexer .string(trace, in).toString))
95116 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 ),
97118 (trace : List [JsonError ], in : RetractReader ) => {
98119 try {
99120 val i = UnsafeNumbers .bigDecimal_(in, false , Int .MaxValue )
@@ -104,7 +125,10 @@ object ZioJsonCodecs {
104125 }
105126 })
106127 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+ },
108132 (trace : List [JsonError ], in : RetractReader ) => {
109133 try {
110134 val i = BigInt (UnsafeNumbers .bigInteger_(in, false , Int .MaxValue ))
@@ -136,20 +160,18 @@ object ZioJsonCodecs {
136160 implicit val missingRequiredFieldsC3c : JsonCodec [MissingRequiredFields ] = DeriveJsonCodec .gen
137161 implicit val primitivesC3c : JsonCodec [Primitives ] = DeriveJsonCodec .gen
138162 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 ) => {
141165 out.write('"' )
142166 out.write(a.toString)
143167 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+ })
155177}
0 commit comments