pawitp / protobuf-decoder

JavaScript-based web UI to decode ad-hoc Protobuf data
https://protobuf-decoder.netlify.app/
MIT License
392 stars 91 forks source link

Add export to .json option #81

Open felipeccalegari opened 7 months ago

felipeccalegari commented 7 months ago

Can you add an option to export the output into a .json file, please?

konsumer commented 5 months ago

I made these libs:

One issue with raw proto-to-json, in general, is that every wire-type can actually be several different scalar-types, so it's impossible to really guess the format, reliably. There are a few assumptions you can make to roughly get the value, but it will almost always be off on some fields:

Part of what is great about this tool is that it just shows the possible types, so you can guess by looking at it, but there is no way to automate that.

Another issue is that JSON doesn't directly map to protobuf. For example, you can have a repeated field at the same level, which can't really be described as an associative array (object) in JSON. like this:

10:
  1: 100
  2: https://example.org/cat.png
10:
  1: 200
  2: https://example.org//dog.png

It cannot be described like this in JSON (invalid):

{
  "10": {
    "1": 100,
    "2": "https://example.org/cat.png"
  },
  "10": {
    "1": 200,
    "2": "https://example.org/dog.png"
  }
}

so you have to do things like make it an array:

{
  "10": [
    {
      "1": 100,
      "2": "https://example.org/cat.png"
    },
    {
      "1": 200,
      "2": "https://example.org/dog.png"
    }
  ]
}

or some other schema (like a flat array with objects that have field-number in it.) I get around this in my libs, by trying to guess and using arrays if multiples are found, or just always use arrays (so they can be traversed in the same way every time.)

I think it's very doable to get "json from raw protobuf" in at least a format that is readable, but these issues make a 1-to-1 translation impossible without the schema, at least automatically.

You can use my tools to generate (an approximation) in JSON (using rawproto or rawprotoparse), or to protoquery individual fields (where you have to provide the type you expect) or use a custom traversal function (in rawprotoparse.) protoquery also has something I call a "tree" which is a root-message that can be traversed, like an AST, but it's less like a plain JSON object, and more of a description of the bytes all broken-down.

I am currently working on protoquery, and I think I will end up merging these projects into 1 thing (so you can query, or try to get "best guess" JSON, or generate a guess at the proto SDL.)