Skip to content

Commit

Permalink
json pretty printing (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
googley42 authored Jun 22, 2024
1 parent c4a5af6 commit 609fc67
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
7 changes: 5 additions & 2 deletions dynamodb-json/src/main/scala/zio/dynamodb/json/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 609fc67

Please sign in to comment.