-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
787c4ed
commit f554e6f
Showing
15 changed files
with
263 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
modules/neotype-jsoniter/shared/src/main/scala/neotype/jsoniter/Main.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package neotype.jsoniter | ||
|
||
import com.github.plokhotnyuk.jsoniter_scala.core.{JsonReader, JsonValueCodec, JsonWriter} | ||
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker | ||
import neotype.* | ||
|
||
// Newtype | ||
inline given [A, B](using newType: Newtype.WithType[A, B]): JsonValueCodec[B] = new JsonValueCodec[B]: | ||
private val codec = JsonCodecMaker.make[A] | ||
|
||
override def decodeValue(in: JsonReader, default: B): B = | ||
val decoded = codec.decodeValue(in, newType.unwrap(default)) | ||
newType.make(decoded) match | ||
case Left(value) => in.decodeError(s"Failed to decode $value") | ||
case Right(value) => value | ||
|
||
override def encodeValue(x: B, out: JsonWriter): Unit = | ||
codec.encodeValue(newType.unwrap(x), out) | ||
|
||
override def nullValue: B = null.asInstanceOf[B] | ||
|
||
// Newtype.Simple | ||
inline given [A, B](using newType: Newtype.Simple.WithType[A, B]): JsonValueCodec[B] = | ||
newType.applyF(JsonCodecMaker.make[A]) | ||
|
||
// Subtype | ||
inline given [A, B <: A](using subType: Subtype.WithType[A, B]): JsonValueCodec[B] = | ||
new JsonValueCodec[B]: | ||
private val codec = JsonCodecMaker.make[A] | ||
|
||
override def decodeValue(in: JsonReader, default: B): B = | ||
val decoded = codec.decodeValue(in, default) | ||
subType.make(decoded) match | ||
case Left(value) => in.decodeError(s"Failed to decode $value") | ||
case Right(value) => value | ||
|
||
override def encodeValue(x: B, out: JsonWriter): Unit = | ||
codec.encodeValue(x, out) | ||
|
||
override def nullValue: B = null.asInstanceOf[B] | ||
|
||
// Subtype.Simple | ||
inline given [A, B <: A](using subType: Subtype.Simple.WithType[A, B]): JsonValueCodec[B] = | ||
subType.applyF(JsonCodecMaker.make[A]) |
Oops, something went wrong.