【问题标题】:How can we convert .proto file and JSON descriptors?我们如何转换 .proto 文件和 JSON 描述符?
【发布时间】:2021-09-26 23:09:55
【问题描述】:

我想将 .proto 文件转换为 JSON 描述符。

.proto 文件是这样的: https://github.com/protobufjs/protobuf.js/blob/master/google/protobuf/type.proto

JSON 描述符是这样的: https://github.com/protobufjs/protobuf.js/blob/master/google/protobuf/type.json

我想在 bigquery udf 上使用 protobuf 和使用 bq-udf-protobuf 的原因。 https://github.com/salrashid123/bq-udf-protobuf 所以,我想我需要为我的 SQL 准备像 JSON 描述符这样的句子。

【问题讨论】:

    标签: javascript protocol-buffers protobuf.js bigquery-udf


    【解决方案1】:

    Robocco,你可以将你的 proto 文件定义转换成 protobufjs.Root,然后使用 toJSON() 方法获取 JSON 描述符。

    这是来自文档的相关example

    const protobufjs = require("protobufjs")
    
    const root = protobufjs.loadSync('test.proto');
    // Alternative way without loading proto string from file 
    // const root = protobufjs.parse('syntax = "proto3"; ...').root;​
    console.log(JSON.stringify(root.toJSON(), null, 4));
    

    【讨论】:

    • 欢迎来到 Stack Overflow!如果您解释了它的工作原理/原因,您的回答可能对其他人更有帮助:)
    • 虽然此代码可能会解决问题,但 including an explanation 关于如何以及为何解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案添加解释并说明适用的限制和假设。
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2014-04-25
    • 2014-04-07
    • 2011-03-11
    相关资源
    最近更新 更多