Created
November 9, 2017 02:15
-
-
Save n4to4/1cdd6bab2506b0d027fb237ce026fc8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.trueaccord.scalapb.compiler.Version.protobufVersion | |
| PB.targets in Compile := Seq( | |
| PB.gens.java(protobufVersion) -> ((sourceManaged in Compile).value / "protobuf-java"), | |
| scalapb.gen(javaConversions=true) -> ((sourceManaged in Compile).value / "protobuf-scala") | |
| ) | |
| libraryDependencies += "com.google.protobuf" % "protobuf-java-util" % "3.4.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Main extends App { | |
| import com.google.protobuf.util.JsonFormat | |
| import example.user.User | |
| val user = User(id = 1, name = "foo") | |
| println(user) | |
| val userJson = JsonFormat.printer.print(User.toJavaProto(user)) | |
| println(userJson) | |
| import com.google.protobuf.MessageOrBuilder | |
| import com.google.protobuf.util.JsonFormat | |
| import com.trueaccord.scalapb.JavaProtoSupport | |
| implicit class GeneratedMessageOps[A](val self: A) extends AnyVal { | |
| def toJsonString[B <: MessageOrBuilder](implicit A: JavaProtoSupport[A, B]): String = | |
| JsonFormat.printer.print(A.toJavaProto(self)) | |
| } | |
| println(User(id = 2, name = "bar").toJsonString) | |
| def jsonStringToUser(json: String): User = { | |
| val parser = JsonFormat.parser() | |
| val builder = example.UserOuterClass.User.newBuilder() | |
| // val registry = JsonFormat.TypeRegistry.newBuilder().add(User.descriptor).build() | |
| // val parser = JsonFormat.parser().usingTypeRegistry(registry) | |
| parser.merge(json, builder) | |
| User.fromJavaProto(builder.build()) | |
| } | |
| val user2 = jsonStringToUser(userJson) | |
| println("user2") | |
| println(user2) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.12") | |
| libraryDependencies += "com.trueaccord.scalapb" %% "compilerplugin" % "0.6.6" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| syntax = "proto3"; | |
| package example; | |
| message User { | |
| int64 id = 1; | |
| string name = 2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment