diff --git a/dynamodb-json/src/main/scala/zio/dynamodb/json/DynamodbJsonCodec.scala b/dynamodb-json/src/main/scala/zio/dynamodb/json/DynamodbJsonCodec.scala index 7070e2be..52f492d3 100644 --- a/dynamodb-json/src/main/scala/zio/dynamodb/json/DynamodbJsonCodec.scala +++ b/dynamodb-json/src/main/scala/zio/dynamodb/json/DynamodbJsonCodec.scala @@ -30,6 +30,8 @@ private[dynamodb] object DynamodbJsonCodec { } def attributeValueToJsonString(av: AttributeValue): String = encode(av).toJson + + def attributeValueToJsonStringPretty(av: AttributeValue): String = encode(av).toJsonPretty } object Decoder { def createMap(fields: Chunk[(String, Json)], map: AttributeValue.Map): Either[String, AttributeValue.Map] = diff --git a/dynamodb-json/src/main/scala/zio/dynamodb/json/package.scala b/dynamodb-json/src/main/scala/zio/dynamodb/json/package.scala index eb245468..bc9dacaf 100644 --- a/dynamodb-json/src/main/scala/zio/dynamodb/json/package.scala +++ b/dynamodb-json/src/main/scala/zio/dynamodb/json/package.scala @@ -7,12 +7,15 @@ import zio.schema.Schema package object json { implicit class AttrMapJsonOps(am: AttrMap) { - def toJsonString: String = Encoder.attributeValueToJsonString(am.toAttributeValue) + def toJsonString: String = Encoder.attributeValueToJsonString(am.toAttributeValue) + def toJsonStringPretty: String = Encoder.attributeValueToJsonStringPretty(am.toAttributeValue) } implicit class ProductJsonOps[A](a: A) { - def toJsonString[A2 >: A](implicit ev: Schema[A2]): String = + def toJsonString[A2 >: A](implicit ev: Schema[A2]): String = Encoder.attributeValueToJsonString(toItem[A2](a).toAttributeValue) + def toJsonStringPretty[A2 >: A](implicit ev: Schema[A2]): String = + Encoder.attributeValueToJsonStringPretty(toItem[A2](a).toAttributeValue) } def parseItem(json: String): Either[DynamoDBError.ItemError, AttrMap] = diff --git a/examples/src/main/scala/zio/dynamodb/examples/ZioDynamodbJsonExamples.scala b/examples/src/main/scala/zio/dynamodb/examples/ZioDynamodbJsonExamples.scala index a0244118..4d56295c 100644 --- a/examples/src/main/scala/zio/dynamodb/examples/ZioDynamodbJsonExamples.scala +++ b/examples/src/main/scala/zio/dynamodb/examples/ZioDynamodbJsonExamples.scala @@ -5,6 +5,7 @@ import zio.schema.DeriveSchema import zio.schema.annotation.discriminatorName import zio.schema.Schema +import zio.dynamodb.AttrMap /** * zio-dynamodb-json is a new experimental module designed for debugging and troubleshooting purposes - it should not be used in production code. @@ -34,6 +35,7 @@ object ZioDynamodbJsonExample extends App { val preBilled = Invoice.PreBilled("id", "sku") val jsonString = preBilled.toJsonString[Invoice] // requires "import zio.dynamodb.json._" println(jsonString) // {"sku":{"S":"sku"},"id":{"S":"id"},"invoiceType":{"S":"PreBilled"}} + println(preBilled.toJsonStringPretty[Invoice]) // decode the json string to a case class val errorOrInvoice = parse[Invoice](jsonString) @@ -48,4 +50,8 @@ object ZioDynamodbJsonExample extends App { .map(item => item.toJsonString) .map(println) // {"sku":{"S":"sku"},"id":{"S":"id"},"invoiceType":{"S":"PreBilled"}} + // random AttrMap with no Schema + val attrMap = AttrMap("foo" -> "foo", "bar" -> "bar", "baz" -> "baz") + println(attrMap.toJsonString) // {"foo":{"S":"foo"},"bar":{"S":"bar"},"baz":{"S":"baz"}} + println(attrMap.toJsonStringPretty) }