Decoding JSON with Value Field in Protobuf
The specified issue arises when attempting to unmarshal JSON into a Protobuf struct field defined as google.protobuf.Value. The underlying issue is the incompatibility between the encoding/json library and the Protobuf library in handling such fields.
To overcome this, it is recommended to leverage the purpose-built protojson library provided by the Protobuf project. By employing the protojson.Unmarshal function, it becomes possible to correctly decode JSON data containing Value fields into the corresponding Protobuf structs.
Here is an updated code snippet demonstrating the use of the protojson.Unmarshal function:
<code class="go">req := &proto.JobCreateRequest{} err := protojson.Unmarshal(bytes, req)</code>
This modification should resolve the issue and enable you to successfully populate the Data field of the JobCreateRequest struct with the provided JSON data.
The above is the detailed content of How to Decode JSON with Value Fields into Protobuf Structs?. For more information, please follow other related articles on the PHP Chinese website!