Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json pretty printing #439

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading