-
Hi everyone. I'm trying to define a structure which have a field that contains a json object. I don't need it to be modeled with smithy itself, just to hold whatever value I set to it. So far I'm trying to achieve this using a Document type :
Now, in my scala code I have a circe Json object that I want to assign to the field but I cannot find a way to create a Document from it (or from a json string). So, my questions are
Thanks for the help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
let me see if I got it right: given the following Stuff(Json.obj("hello" := 42)) you want the Json to be serialized as a string? {"field": "{\"hello\": 42}"} In this case, your Smithy spec would have to have or do you just want to pass the {"field": {"hello": 42}} In this case, object JsonToDocumentFolder extends Folder[Document]:
override def onNull: Document = Document.nullDoc
override def onString(value: String): Document = Document.fromString(value)
override def onBoolean(value: Boolean): Document = Document.fromBoolean(value)
override def onNumber(value: JsonNumber): Document = value.toInt
.map(Document.fromInt)
.orElse(
value.toLong.map(Document.fromLong)
)
.getOrElse(
Document.fromDouble(value.toDouble)
)
override def onArray(value: Vector[Json]): Document =
Document.array(value.map(_.foldWith(this)))
override def onObject(value: JsonObject): Document = Document.DObject(
value.toMap.map { case (k, v) =>
k -> v.foldWith(this)
}
)
val json: Json = ???
Stuff(json.foldWith(jsonToDocumentFolder)) |
Beta Was this translation helpful? Give feedback.
let me see if I got it right: given the following
you want the Json to be serialized as a string?
In this case, your Smithy spec would have to have
structure Stuff { @required field: String }
, and you'd pass it asStuff(jsonobj.noSpaces /* or some other rendering */)
.or do you just want to pass the
Json
value verbatim and let it be encoded as json?In this case,
Document
is fine, but you'll need to go fromJson
toDocument
. You can implement this on your side: