Skip to content
This repository was archived by the owner on Dec 27, 2021. It is now read-only.

Commit b29046f

Browse files
committed
Make it possible to use json marshallers ✨
1 parent 11890b9 commit b29046f

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

core/lagompb-core/src/main/scala/io/superflat/lagompb/ApiSerializer.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ sealed trait GenericSerializers[A <: GeneratedMessage] {
3131
def deserializer(implicit
3232
A: GeneratedMessageCompanion[A]
3333
): NegotiatedDeserializer[A, ByteString] = { (wire: ByteString) =>
34-
35-
ProtosRegistry.parser.fromJsonString(wire.utf8String)
34+
ProtosRegistry.fromJson(wire.utf8String)
3635
}
3736

3837
def negotiateResponse(
@@ -57,7 +56,7 @@ sealed trait GenericSerializers[A <: GeneratedMessage] {
5756
MessageProtocol(Some("application/json"))
5857

5958
override def serialize(message: A): ByteString =
60-
ByteString(ProtosRegistry.printer.print(message))
59+
ByteString(ProtosRegistry.toJson(message))
6160
}
6261

6362
def serializerProtobuf: NegotiatedSerializer[A, ByteString] =

core/lagompb-core/src/main/scala/io/superflat/lagompb/ProtosRegistry.scala

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,36 @@ object ProtosRegistry {
5454
new Parser().withTypeRegistry(typeRegistry)
5555

5656
private[lagompb] lazy val printer: Printer =
57-
new Printer().includingDefaultValueFields
57+
new Printer().includingDefaultValueFields.formattingLongAsNumber
5858
.withTypeRegistry(typeRegistry)
5959

60+
/**
61+
* Converts a scalapb GeneratedMessage to printable Json string using the available
62+
* registry
63+
* @param message the proto message
64+
* @return the json string
65+
*/
66+
def toJson(message: GeneratedMessage): String = {
67+
printer.print(message)
68+
}
69+
70+
/**
71+
* Converts a json string to a scalapb message
72+
*
73+
* @param jsonString the json string
74+
* @param A the scalapb message
75+
* @tparam A the scala type of the proto message to parse the json string into
76+
* @return the scalapb message parsed from the json string
77+
*/
78+
def fromJson[A <: GeneratedMessage](
79+
jsonString: String
80+
)(implicit A: GeneratedMessageCompanion[A]): A = {
81+
parser.fromJsonString(jsonString)
82+
}
83+
6084
/**
6185
* Gets the maybe scalapb GeneratedMessageCompanion object defining an Any protobuf message
86+
*
6287
* @param any the protobuf message
6388
* @return the maybe scalapb GeneratedMessageCompanion object
6489
*/

core/lagompb-core/src/test/scala/io/superflat/lagompb/ProtosRegistrySpec.scala

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,41 @@ import scalapb.{GeneratedMessage, GeneratedMessageCompanion}
88

99
class ProtosRegistrySpec extends BaseSpec {
1010

11-
"Loading GeneratedFileObject" should {
11+
"The Protos registry" should {
1212

13-
"succeed" in {
13+
"load protos definitions as expected" in {
1414
val fos = ProtosRegistry.registry
1515
fos.size should be >= 1
1616
fos should contain(CoreProto)
1717
}
1818

19-
"should be loaded" in {
19+
"load message companions as expected" in {
2020
val size: Int = ProtosRegistry.companions.size
2121
size should be >= 1
2222
}
2323

24-
"Contains the companions" in {
24+
"help build the message companions map as expected" in {
2525
val map: Map[String, GeneratedMessageCompanion[_ <: GeneratedMessage]] =
2626
ProtosRegistry.companionsMap
2727
map.keySet should contain("lagompb.v1.TestCommand")
2828
}
2929

30-
"Gets scalapb GeneratedMessageCompanion object" in {
30+
"help get scalapb GeneratedMessageCompanion object" in {
3131
val any = Any.pack(TestCommand.defaultInstance)
3232
ProtosRegistry.getCompanion(any) should be(Symbol("defined"))
3333
}
34+
35+
"help convert a proto message to json" in {
36+
val testCommand = TestCommand.defaultInstance.withCompanyUuid("123").withName("test")
37+
val json = ProtosRegistry.toJson(testCommand)
38+
json shouldBe """{"companyUuid":"123","name":"test"}""".stripMargin
39+
}
40+
41+
"help convert a json string to a proto message" in {
42+
val jsonString = """{"companyUuid":"123","name":"test"}""".stripMargin
43+
val testCommand = ProtosRegistry.fromJson[TestCommand](jsonString)
44+
testCommand.companyUuid shouldBe "123"
45+
testCommand.name shouldBe "test"
46+
}
3447
}
3548
}

0 commit comments

Comments
 (0)