首页 > 解决方案 > 在 protobuf 中使用空括号验证对象

问题描述

我有一个 json 文件转换为 protobuf 格式,在某些对象中有空括号。您能帮我识别 protobuf 中的空对象(第一种情况)吗?我尝试使用字节大小,但对于一个好的对象,大小为 0,就像一个空的。

加载数据

val json = // readFromJson
val protobufBuilder = Converted.newBuilder()
JsonFormat.parser.ignoringUnknownFields.merge(json, protobufBuilder)
val converted = protobufBuilder.build()

输入

{
“a”: 0,
“someObject”: {}
}

代码

println(converted.hasSomeObject)  // true
converted.getSomeObjectBytes.size() // 0

输入

{
“a”: 0,
“someObject”: {
    “type”: 1,
    “name”: “”Jack
}
}

斯卡拉

println(converted.hasSomeObject)  // true
converted.getSomeObjectBytes.size() // 0

输入

{
“a”: 0
}

斯卡拉

println(converted.hasSomeObject)  // false
converted.getSomeObjectBytes.size() // 0

标签: jsonscalaprotobuf-java

解决方案


推荐阅读