@@ -16,8 +16,8 @@ It's advantages:
16
16
* Discriminator support for sum types derivation
17
17
18
18
Read more about library usage bellow:
19
- - [ Scala 3] ( https://github.com/tethys-json/tethys?tab=readme-ov-file #scala-3)
20
- - [ Scala 2] ( https://github.com/tethys-json/tethys?tab=readme-ov-file #scala-2)
19
+ - [ Scala 3] ( #scala-3 )
20
+ - [ Scala 2] ( #scala-2 )
21
21
22
22
23
23
# Scala 3
@@ -36,7 +36,7 @@ libraryDependencies ++= Seq(
36
36
tethys provides extension methods allowing you to read and write JSON
37
37
38
38
They look something like this:
39
- ``` scala 3
39
+ ``` scala
40
40
package tethys
41
41
42
42
extension [A ](value : A )
@@ -74,7 +74,7 @@ You can create new instances for your types using:
74
74
1 . ** contramap** on already existing writer
75
75
2 . ** map** on already existing reader
76
76
77
- ``` scala 3
77
+ ``` scala
78
78
import tethys .*
79
79
80
80
case class StringWrapper (value : String ) extends AnyVal
@@ -90,7 +90,7 @@ given JsonReader[StringWrapper] =
90
90
91
91
To build JsonWriter for case class you can use ` obj ` method on its companion object.
92
92
93
- ``` scala 3
93
+ ``` scala
94
94
import tethys .*
95
95
96
96
case class MobileSession (
@@ -104,26 +104,23 @@ object MobileSession:
104
104
.addField(" id" )(_.id)
105
105
.addField(" deviceId" )(_.deviceId)
106
106
.addField(" userId" )(_.userId)
107
-
108
107
```
109
108
110
109
You can concat multiple ** JsonObjectWriter** .
111
110
Combining concatenation with derivation allows to create ** JsonWriter** for sealed trait.
112
111
To derive JsonWriter for sealed trait you need to have ** JsonObjectWriter** instances for all subtypes in scope
113
112
114
- ``` scala 3
115
-
113
+ ``` scala
116
114
given JsonWriter [Session ] =
117
115
JsonWriter .obj[Session ].addField(" typ" )(_.typ) ++ JsonObjectWriter .derived[Session ]
118
-
119
116
```
120
117
121
118
122
119
### JsonReader
123
120
124
121
To build JsonReader for case class you can use ` builder ` method on its companion object.
125
122
126
- ``` scala 3
123
+ ``` scala
127
124
import tethys .*
128
125
129
126
case class MobileSession (
@@ -138,13 +135,11 @@ import tethys.*
138
135
.addField[String ](" deviceId" )
139
136
.addField[java.lang.UUID ](" userId" )
140
137
.buildReader(MobileSession (_, _, _))
141
-
142
-
143
138
```
144
139
145
140
To build JsonReader for sealed trait you can use ` selectReader ` after adding some field:
146
141
147
- ``` scala 3
142
+ ``` scala
148
143
import tethys .*
149
144
150
145
object Session :
@@ -157,15 +152,13 @@ import tethys.*
157
152
case " web" => webReader
158
153
case " mobile" => mobileReader
159
154
}
160
-
161
-
162
155
```
163
156
164
157
165
158
## Derivation
166
159
167
160
All examples consider you made this imports:
168
- ``` scala 3
161
+ ``` scala
169
162
import tethys .*
170
163
import tethys .jackson .* // or tethys.jackson.pretty.* for pretty printing
171
164
```
@@ -174,7 +167,7 @@ import tethys.jackson.* // or tethys.jackson.pretty.* for pretty printing
174
167
### Basic enums
175
168
1 . ** StringEnumJsonWriter** and ** StringEnumJsonReader**
176
169
177
- ``` scala 3
170
+ ``` scala
178
171
enum SessionType derives StringEnumJsonWriter , StringEnumJsonReader :
179
172
case Mobile , Web
180
173
@@ -188,7 +181,7 @@ session.asJson == json
188
181
```
189
182
2 . ** OrdinalEnumJsonWriter** and ** OrdinalEnumJsonReader**
190
183
191
- ``` scala 3
184
+ ``` scala
192
185
enum SessionType derives OrdinalEnumJsonWriter , OrdinalEnumJsonReader :
193
186
case Mobile , Web
194
187
@@ -203,7 +196,7 @@ session.asJson == json
203
196
204
197
### Case classes
205
198
206
- ``` scala 3
199
+ ``` scala
207
200
case class Session (
208
201
id : Long ,
209
202
userId : String
@@ -224,7 +217,7 @@ Discriminator for **JsonWriter** is optional.
224
217
If you don't need readers/writers for subtypes, you can omit them,
225
218
they will be derived recursively for your trait/enum.
226
219
227
- ``` scala 3
220
+ ``` scala
228
221
import tethys .selector
229
222
230
223
sealed trait UserAccount (@ selector val typ : String ) derives JsonReader , JsonObjectWriter
@@ -257,21 +250,21 @@ account.asJson == json
257
250
3 . To configure ** JsonWriter** use ** WriterBuilder**
258
251
4 . Configuration can be provided:
259
252
* ** directly to derived method**
260
- ``` scala 3
253
+ ``` scala
261
254
given JsonWriter [UserAccount .Customer ] =
262
255
JsonObjectWriter .derived {
263
256
WriterBuilder [UserAccount .Customer ]
264
257
}
265
258
```
266
259
* ** as an inline given to derives**
267
- ``` scala 3
260
+ ``` scala
268
261
object Customer :
269
262
inline given WriterBuilder [UserAccount .Customer ] =
270
263
WriterBuilder [UserAccount .Customer ]
271
264
```
272
265
P.S. There are empty ** WriterBuilder** in the examples to simplify demonstration of two approaches. You shouldn't use empty one
273
266
5 . ** WriterBuilder** features
274
- ``` scala 3
267
+ ``` scala
275
268
case class Foo (a : Int , b : String , c : Any , d : Boolean , e : Double )
276
269
277
270
inline given WriterBuilder [Foo ] =
@@ -297,7 +290,7 @@ inline given WriterBuilder[Foo] =
297
290
}
298
291
```
299
292
6 . ** ReaderBuilder** features
300
- ``` scala 3
293
+ ``` scala
301
294
302
295
inline given ReaderBuilder [Foo ] =
303
296
ReaderBuilder [Foo ]
@@ -321,12 +314,12 @@ inline given ReaderBuilder[Foo] =
321
314
### Configuration via ** JsonConfiguration**
322
315
1 . To configure both ** JsonWriter** and ** JsonReader** you can use ** JsonConfiguration**
323
316
2 . ** JsonConfiguration** can be provided as an inline given to derives
324
- ``` scala 3
317
+ ``` scala
325
318
inline given JsonConfiguration = JsonConfiguration .default
326
319
```
327
320
3 . ** JsonConfiguration** will be applied recursively to all nested readers/writers
328
321
* Product types
329
- ``` scala 3
322
+ ``` scala
330
323
import tethys .*
331
324
import tethys .jackson .*
332
325
@@ -341,10 +334,9 @@ inline given JsonConfiguration = JsonConfiguration.default
341
334
342
335
json.jsonAs[Outer ] == Right (outer)
343
336
outer.asJson == json
344
-
345
337
```
346
338
* Sum types
347
- ``` scala 3
339
+ ``` scala
348
340
import tethys .*
349
341
import tethys .jackson .*
350
342
@@ -367,7 +359,7 @@ inline given JsonConfiguration = JsonConfiguration.default
367
359
secondJson.jsonAs[Choice ] == second
368
360
```
369
361
4 . ** WriterBuilder** and ** ReaderBuilder** settings have higher priority than ** JsonConfiguration** settings
370
- ``` scala 3
362
+ ``` scala
371
363
import tethys .*
372
364
import tethys .jackson .*
373
365
@@ -392,11 +384,9 @@ val json = """{"ID": 5, "PHONENUMBER": "+123"}"""
392
384
393
385
json.jsonAs[Customer ] == Right (customer)
394
386
customer.asJson == json
395
-
396
387
```
397
388
5 . ** JsonConfiguration** features
398
- ``` scala 3
399
-
389
+ ``` scala
400
390
inline given JsonConfiguration =
401
391
JsonConfiguration
402
392
// default config, entrypoint for configuration
@@ -420,7 +410,7 @@ so tethys can offer you **circe** and **json4s** AST support
420
410
libraryDependencies += " com.tethys-json" %% " tethys-circe" % tethysVersion
421
411
```
422
412
423
- ``` scala 3
413
+ ``` scala
424
414
import tethys .*
425
415
import tethys .jackson .*
426
416
import tethys .circe .*
@@ -465,7 +455,6 @@ libraryDependencies += "com.tethys-json" %% "tethys-enumeratum" % tethysVersion
465
455
466
456
enumeratum module provides a bunch of mixins for your Enum classes.
467
457
``` scala
468
-
469
458
import enumeratum .{Enum , EnumEntry }
470
459
import tethys .enumeratum .*
471
460
@@ -482,7 +471,6 @@ case object Direction extends Enum[Direction]
482
471
483
472
val values = findValues
484
473
}
485
-
486
474
```
487
475
488
476
### Migration notes
@@ -503,7 +491,7 @@ Scala 3 derivation API in **0.29.0** has a lot of deprecations and is not fully
503
491
* select some field from your model
504
492
* provide type to method and name of field as string parameter
505
493
506
- ``` scala 3
494
+ ``` scala
507
495
ReaderBuilder [SimpleType ]
508
496
.extract(_.i).from(_.d).and[Double ](" e" )((d, e) => (d + e).toInt)
509
497
```
0 commit comments