JsonFormat.Printer serializes custom options, but JsonFormat.Parser fails to deserialize them
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
This is unsupported with the current API because it requires an ExtensionRegistry, but extensions aren't really supported in proto3. This needs further thought.
Any update on this? I am using proto2. And it's kind of weird for not able to use JSON.
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.
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.