11package lc .core
22
3- import com .github .plokhotnyuk .jsoniter_scala .macros ._
4- import com .github .plokhotnyuk .jsoniter_scala .core ._
3+ import zio .blocks .schema ._
4+ import zio .blocks .schema .json ._
5+ import zio .blocks .schema .codec .BinaryCodec
6+ import java .nio .ByteBuffer
57
68trait ByteConvert { def toBytes (): Array [Byte ] }
79case class Size (height : Int , width : Int )
810
911case class Parameters (level : String , media : String , input_type : String , size : String )
1012object Parameters {
11- implicit val codec : JsonValueCodec [Parameters ] = JsonCodecMaker .make
13+ implicit val schema : Schema [Parameters ] = Schema .derived
14+ implicit val codec : BinaryCodec [Parameters ] = schema.derive(JsonFormat .deriver)
1215}
1316
14- case class Id (id : String ) extends ByteConvert { def toBytes (): Array [Byte ] = { writeToArray(this ) } }
17+ object BufferEncoder {
18+ def encode [A ](value : A , codec : BinaryCodec [A ]): Array [Byte ] = {
19+ // Start with 1KB, if it fails, try with 10KB, 100KB, etc up to 1MB
20+ var size = 1024
21+ var result : Array [Byte ] = null
22+ while (result == null && size <= 1048576 ) {
23+ try {
24+ val buf = ByteBuffer .allocate(size)
25+ codec.encode(value, buf)
26+ buf.flip()
27+ result = new Array [Byte ](buf.remaining())
28+ buf.get(result)
29+ } catch {
30+ case _ : java.nio.BufferOverflowException =>
31+ size *= 10
32+ }
33+ }
34+ if (result == null ) {
35+ throw new Exception (" Buffer overflow encoding object" )
36+ }
37+ result
38+ }
39+ }
40+
41+ case class Id (id : String ) extends ByteConvert {
42+ def toBytes (): Array [Byte ] = {
43+ BufferEncoder .encode(this , Id .codec)
44+ }
45+ }
1546object Id {
16- implicit val codec : JsonValueCodec [Id ] = JsonCodecMaker .make
47+ implicit val schema : Schema [Id ] = Schema .derived
48+ implicit val codec : BinaryCodec [Id ] = schema.derive(JsonFormat .deriver)
1749}
1850
1951case class Image (image : Array [Byte ]) extends ByteConvert { def toBytes (): Array [Byte ] = { image } }
2052
2153case class Answer (answer : String , id : String )
2254object Answer {
23- implicit val codec : JsonValueCodec [Answer ] = JsonCodecMaker .make
55+ implicit val schema : Schema [Answer ] = Schema .derived
56+ implicit val codec : BinaryCodec [Answer ] = schema.derive(JsonFormat .deriver)
2457}
2558
26- case class Success (result : String ) extends ByteConvert { def toBytes (): Array [Byte ] = { writeToArray(this ) } }
27- object Success {
28- implicit val codec : JsonValueCodec [Success ] = JsonCodecMaker .make
59+ case class Success (result : String ) extends ByteConvert {
60+ def toBytes (): Array [Byte ] = {
61+ BufferEncoder .encode(this , Success .codec)
62+ }
2963}
30-
31- case class Error (message : String ) extends ByteConvert { def toBytes (): Array [Byte ] = { writeToArray(this ) } }
32- object Error {
33- implicit val codec : JsonValueCodec [Error ] = JsonCodecMaker .make
64+ object Success {
65+ implicit val schema : Schema [Success ] = Schema .derived
66+ implicit val codec : BinaryCodec [Success ] = schema.derive(JsonFormat .deriver)
3467}
3568
36- case class JSONString (string : String )
37-
38- object JSONString {
39- implicit val codec : JsonValueCodec [JSONString ] = new JsonValueCodec [JSONString ] {
40- def decodeValue (in : JsonReader , default : JSONString ): JSONString = {
41- val raw = in.readRawValAsBytes()
42- JSONString (new String (raw, " UTF-8" ))
43- }
44-
45- def encodeValue (x : JSONString , out : JsonWriter ): Unit = {
46- out.writeRawVal(x.string.getBytes(" UTF-8" ))
47- }
48-
49- def nullValue : JSONString = null .asInstanceOf [JSONString ]
69+ case class Error (message : String ) extends ByteConvert {
70+ def toBytes (): Array [Byte ] = {
71+ BufferEncoder .encode(this , Error .codec)
5072 }
5173}
74+ object Error {
75+ implicit val schema : Schema [Error ] = Schema .derived
76+ implicit val codec : BinaryCodec [Error ] = schema.derive(JsonFormat .deriver)
77+ }
5278
5379case class CaptchaConfig (
5480 name : String ,
5581 allowedLevels : List [String ],
5682 allowedMedia : List [String ],
5783 allowedInputType : List [String ],
5884 allowedSizes : List [String ],
59- config : JSONString
85+ config : zio.blocks.schema.json. Json . Object
6086)
6187
6288case class AppConfig (
@@ -77,7 +103,8 @@ case class AppConfig(
77103 )
78104}
79105object AppConfig {
80- implicit val codec : JsonValueCodec [AppConfig ] = JsonCodecMaker .make
106+ implicit val schema : Schema [AppConfig ] = Schema .derived
107+ implicit val codec : BinaryCodec [AppConfig ] = schema.derive(JsonFormat .deriver)
81108}
82109case class ConfigField (
83110 port : Option [Int ] = None ,
0 commit comments