protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

JsonFormat.Printer serializes custom options, but JsonFormat.Parser fails to deserialize them

Open justinplus opened this issue 5 years ago • 1 comments

Version: v3.11.1 Language: Java

Hi,

I extended the FileOptions as follows.

syntax = "proto3";
import "google/protobuf/descriptor.proto";

package com.test.protobuf.dataset;

extend google.protobuf.FileOptions {
    string message_name = 7001;
}

And I defined a message using the custom file option.

syntax = "proto3";

import "dynamic_data_set_options.proto";

package com.test.protobuf.dataset;

option (message_name) = "Record";

message Record {
    string key = 1;
    string value = 2;
}

When I serialized the DynamicDataSet into JSON by JsonFormat.printer(), its options are serialized as well.

{
  "name": "dynamic_dataset_example.proto",
  "package": "com.test.protobuf.dataset",
  "dependency": ["dynamic_dataset_options.proto"],
  "messageType": [{
    "name": "Record",
    "field": [{
      "name": "key",
      "number": 1,
      "label": "LABEL_REQUIRED",
      "type": "TYPE_STRING",
      "jsonName": "key"
    }, {
      "name": "value",
      "number": 2,
      "label": "LABEL_REQUIRED",
      "type": "TYPE_STRING",
      "jsonName": "value"
    }],
    "nestedType": [{
      "name": "Record"
    }]
  }],
  "options": {
    "messageName": "Record"
  }
}

But when I parsed it back to FileDescriptorProto using JsonFormat.parser(), I got an exception.

com.google.protobuf.InvalidProtocolBufferException: Cannot find field: compressionOption in message google.protobuf.FileOptions

However, since JsonFormat serializes custom options, why does it fails to deserialize them? Or am I missing any thing?

I also noticed this comment in the Java doc.

Utility classes to convert protobuf messages to/from JSON format. The JSON format follows Proto3 JSON specification and only proto3 features are supported. Proto2 only features (e.g., extensions and unknown fields) will be discarded in the conversion. That is, when converting proto2 messages to JSON format, extensions and unknown fields will be treated as if they do not exist. This applies to proto2 messages embedded in proto3 messages as well.

Does JsonFormat not support custom options?

thank you, Justin

justinplus avatar Feb 17 '20 15:02 justinplus

This is unsupported with the current API because it requires an ExtensionRegistry, but extensions aren't really supported in proto3. This needs further thought.

googleberg avatar Sep 01 '22 18:09 googleberg

Any update on this? I am using proto2. And it's kind of weird for not able to use JSON.

wibotwi avatar May 09 '23 00:05 wibotwi

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] avatar Mar 27 '24 10:03 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Apr 12 '24 10:04 github-actions[bot]