简体   繁体   English

将 protobuf(由 grpc 生成)转换为 Android 中的 JSON

[英]Convert a protobuf ( generated by grpc ) to JSON in Android

I would like to convert a protobuf ( generated by grpc ) to JSON.我想将 protobuf(由 grpc 生成)转换为 JSON。

I know with protobuf you can use: JSonFormat ( https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat ) but this is not included in protobuf-lite ( implementation 'com.google.protobuf:protobuf-lite:3.0.0' ) which it's the one used in Android with the protobuf-gradle-plugin ( https://github.com/google/protobuf-gradle-plugin/issues/276 )我知道您可以使用 protobuf: JSonFormat ( https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat ) 但这不包含在protobuf-lite ( implementation 'com.google.protobuf:protobuf-lite:3.0.0' ),它是 Android 中使用的一个,带有protobuf-gradle-pluginhttps://github.com/google/protobuf-gradle-plugin/issues 276 )

Is there any alternative?有没有其他选择?

I'm using Kotlin, but because the protobuf generated from grpc is in java so I can't use serialisation from Kotlin ( https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/runtime_usage.md#protobuf ) I'm using Kotlin, but because the protobuf generated from grpc is in java so I can't use serialisation from Kotlin ( https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/runtime_usage.md# protobuf )

Any ideas?有任何想法吗?

Thanks谢谢

Ok I think I got it working, this is the solution I got, just if someone gets stack like me:好的,我想我得到了它,这就是我得到的解决方案,只要有人像我一样得到堆栈:

// GRPC
implementation "io.grpc:grpc-okhttp:${grpcVersion}" // CURRENT_GRPC_VERSION
implementation "io.grpc:grpc-stub:${grpcVersion}" // CURRENT_GRPC_VERSION
implementation "io.grpc:grpc-protobuf:$grpcVersion"

// Protobuf
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
implementation "com.google.protobuf:protobuf-java-util:$protobufVersion"



protobuf {
    protoc { artifact = "com.google.protobuf:protoc:$protobufVersion" }
    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" // CURRENT_GRPC_VERSION
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java { }
            }
            task.plugins {
                grpc { }
            }
        }
    }
}

Now I can use the JsonFormat:现在我可以使用 JsonFormat:

val jsonPrinter = JsonFormat.printer().includingDefaultValueFields()


return jsonPrinter.print(myProtobufObject)

sources:来源:

https://github.com/google/protobuf-gradle-plugin#protos-in-dependencies https://github.com/grpc/grpc-java https://github.com/protocolbuffers/protobuf/tree/master/javahttps://github.com/google/protobuf-gradle-plugin#protos-in-dependencies https://github.com/grpc/grpc-java https://github.com/protocolbuffers/protobuf/tree/master/爪哇

https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/JsonFormat

Unfortunately this solution is not compatible with solutions older than Android Oreo:不幸的是,该解决方案与早于 Android Oreo 的解决方案不兼容:

https://github.com/protocolbuffers/protobuf/issues/6718 https://github.com/protocolbuffers/protobuf/issues/6718

Any updates:任何更新:

https://groups.google.com/forum/#!topic/protobuf/EC2TtPixCFY https://groups.google.com/forum/#!topic/protobuf/EC2TtPixCFY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM