0

My requirement is to share the data JSON to proto-buf. I have JSON data. How to write a schema genric for handling all types of value.

{
  "data": [
    {
      "bool": true
    },
    {
      "string": "abc"
    },
    {
      "int": 22
    },
    {
      "json_object" : {
        "id": 1,
        "email": "example@gmail.com"
      }
    },
    {
      "json_array" : [
          {
            "name":"xyz"
          },
          {
            "age":2
          }
        ]
    }
  ]
}

If, I am using Map<string, string> map. It's supported string value only. If, I am using Map<string, google.protobuf.Value> map. The Value is not supported by JSON and JSONArray.

How do I handle all types in a single schema?. Thanks

Alex Sparrow
  • 197
  • 2
  • 7

1 Answers1

0

IIUC you're asking how to support arbitrary JSON using Protocol Buffers.

Because Protocol Buffers require a schema, the easiest way to represent generic|arbitrary|schemaless types using Protocol Buffers is to encapsulate the arbitrary type using a type that supports arbitrary content, i.e. using Protocol Buffers string or bytes types.

NOTE string and bytes are restricted to 2^32 bytes

If your JSON content is a manageable (few) subset of possible types, you may want to consider representing each possibility as a distinct Protocol Buffer Message.

If your JSON content is many possible types but enumerable (you can name them), you may want to consider Any.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88