@@ -24,8 +24,8 @@ The generator can be used with any tool that can perform system calls to a comma
2424See example under [ example/generate.scala] ( ./example/generate.scala ) .
2525
2626``` scala
27- //> using scala 3.7.3
28- //> using dep dev.rolang::gcp-codegen::0.0.8
27+ //> using scala 3.7.4
28+ //> using dep dev.rolang::gcp-codegen::0.0.12
2929
3030import gcp .codegen .* , java .nio .file .* , GeneratorConfig .*
3131
@@ -55,13 +55,41 @@ See output in `example/out`.
5555
5656| Configuration | Description | Options | Default |
5757| ------------------- | ---------------- | ------- | --- |
58- | -specs | Can be ` stdin ` or a path to the JSON file. | | |
59- | -out-dir | Ouput directory | | |
60- | -out-pkg | Output package | | |
61- | -http-source | Generated http source. | [ Sttp4] ( https://sttp.softwaremill.com/en/stable ) | |
62- | -json-codec | Generated JSON codec | [ Jsoniter] ( https://github.com/plokhotnyuk/jsoniter-scala ) , [ ZioJson] ( https://zio.dev/zio-json ) | |
63- | -array-type | Collection type for JSON arrays | ` List ` , ` Vector ` , ` Array ` , ` ZioChunk ` | ` List ` |
64- | -include-resources | Optional resource filter. | | |
58+ | -specs | Can be ` stdin ` or a path to the JSON file. | | |
59+ | -out-dir | Output directory | | |
60+ | -out-pkg | Output package | | |
61+ | -http-source | Generated http source. | [ Sttp4] ( https://sttp.softwaremill.com/en/stable ) | |
62+ | -json-codec | Generated JSON codec | [ Jsoniter] ( https://github.com/plokhotnyuk/jsoniter-scala ) , [ ZioJson] ( https://zio.dev/zio-json ) | |
63+ | -jsoniter-json-type | In case of Jsoniter a fully qualified name of the custom type that can represent a raw Json value | |
64+ | -array-type | Collection type for JSON arrays | ` List ` , ` Vector ` , ` Array ` , ` ZioChunk ` | ` List ` |
65+ | -include-resources | Optional resource filter. | | |
66+
67+ ##### Jsoniter Json type and codec example
68+ Jsoniter doesn't ship with a type that can represent raw Json values to be used for mapping of ` any ` / ` object ` types,
69+ but it provides methods to read / write raw values as bytes (related [ issue] ( https://github.com/plokhotnyuk/jsoniter-scala/issues/1257 ) ).
70+ Given that we can create a custom type with a codec which can look for example like [ that] ( modules/example-jsoniter-json/shared/src/main/scala/json.scala ) :
71+ ``` scala
72+ package example .jsoniter
73+ import com .github .plokhotnyuk .jsoniter_scala .core .*
74+
75+ opaque type Json = Array [Byte ]
76+ object Json :
77+ def writeToJson [T : JsonValueCodec ](v : T ): Json = writeToArray[T ](v)
78+
79+ given codec : JsonValueCodec [Json ] = new JsonValueCodec [Json ]:
80+ override def decodeValue (in : JsonReader , default : Json ): Json = in.readRawValAsBytes()
81+ override def encodeValue (x : Json , out : JsonWriter ): Unit = out.writeRawVal(x)
82+ override val nullValue : Json = Array [Byte ](0 )
83+
84+ extension (v : Json )
85+ def readAsUnsafe [T : JsonValueCodec ]: T = readFromArray(v)
86+ def readAs [T : JsonValueCodec ]: Either [Throwable , T ] =
87+ try Right (readFromArray(v))
88+ catch case t : Throwable => Left (t)
89+ ```
90+ Then pass it as argument to the code generator like ` -jsoniter-json-type=_root_.example.jsoniter.Json ` .
91+ Since this type and codec can be shared across generated clients it has to be provided (at least for now)
92+ instead of being generated for each client to avoid duplicated / redundant code.
6593
6694##### Examples:
6795
@@ -79,7 +107,7 @@ curl 'https://pubsub.googleapis.com/$discovery/rest?version=v1' > pubsub_v1.json
79107 -specs=./pubsub_v1.json \
80108 -out-pkg=gcp.pubsub.v1 \
81109 -http-source=sttp4 \
82- -json-codec=jsoniter \
110+ -json-codec=ziojson \
83111 -include-resources=' projects.*,!projects.snapshots' # optional filters
84112```
85113
0 commit comments