Comments (8)
In order to parse JSON encoded according to the standardized proto3 JSON mapping, you have to use protojson
. There is no option. The existing standard encoding/json does not allow all the features necessary to be compliant with the standard, and so we only support that encoding via the newer protojson package.
from protobuf.
If the v2 JSON design eventually gets merged into the stdlib, you would be able to do something like:
jsonv2.Marshal(v, jsonv2.MarshalFuncV1(protojson.Marshal))
which would allow you to handled mixed data structures that include both protobuf messages and non-protobuf Go types.
from protobuf.
Attempting to unmarshal a byte stream that was marshaled by (stdlib) package json into a protobuf isn't guaranteed to work, as @puellanivis said. You should use protojson all the way if you have proto message types (type TestPB
in your example), or you should use json all the way if you have JSON-annotated structs (type Test
in your example).
It's not clear to me how you're running into the case where you have both type Test
and type TestPB
. What is generating these two messages?
from protobuf.
@aktau look at annotations in TestPB has both json and protobuf for a field thus I don't see why it should be handled "all the way one way or another" as mutually exclusive. Both proto and json should be able to handle the datatype although the problem was some way introduced by a datatype as struct from protobuf, not carefully thought as solution when data mapping this kind of structs
from protobuf.
Yes, TestPB
has annotations for both. I assume this stems from before the time when protojson
existed, and the need for some backwards compatibility there. Regardless of all that, there is no guaranteed interoperability between stdlin package encoding/json and protojson. With proto structs, such as your TestPB
, please use protojson
.
Do you have a problem if you only use package protojson?
from protobuf.
I do and I think it would be good if protojson can unmarshall a string datetime into a timetamp format and fail only if the date/time string is blank/nil/invalid
from protobuf.
I'm still not sure what exactly you'd like, in case it is supported. And I'll note again that we do not support unmarshaling encoding/json
encoded json values with protojson
. Nor do we support unmarshaling into proto types with anything except for the proto
, protojson
or prototext
packages (I believe that's all).
- If you want a
time.Time
out of yourtimestampb
field, you can use theAsTime()
method: https://pkg.go.dev/google.golang.org/protobuf/types/known/timestamppb. - The timestamp type has a special JSON representation that's unlike other proto types, see https://pkg.go.dev/google.golang.org/protobuf/types/known/timestamppb#hdr-JSON_Mapping. Also see the implementation: https://github.com/protocolbuffers/protobuf-go/blob/5e8da9414946bbfc6337916c4ee1ce1ef8b95e86/encoding/protojson/well_known_types.go#L759
- We do not support go-proto plugin to generate
time.Time
types instead of*timestamppb.Timestamp
, if that's what you're asking for.
from protobuf.
@aktau, thanks for the support and info provided. Closing this question then.
from protobuf.
Related Issues (20)
- protobuf/types/known/timestamppb not recognized by gRPC module HOT 9
- Best practices for getting size of Google Protobuf Struct value HOT 5
- [protobuf-go] Add new feature - ignore field when Marshal message HOT 1
- encoding/prototext: Handle well-known Timestamp message as string HOT 1
- protojson: vuln: malicious JSON can trivially induce stack overflow
- protojson: vuln: discard unknown fields can result in stack overflow HOT 1
- API design suggestion: rename UnmarshalOptions to Unmarshaler HOT 4
- nil pointer error inside protobuf library. HOT 1
- Kotlin lite: Tried to write the same file twice
- How to cast descriptorpb.DescriptorProto to protoreflect.MessageDescriptor HOT 2
- Unwanted namespacing warning/ or error HOT 1
- Installation instructions for the common case (nit feature request) HOT 2
- Add a generic proto.Clone HOT 1
- Support for optional fields HOT 2
- Latest version of google.golang.org/protobuf (v1.33.0) is no longer compatible with the latest version of github.com/golang/protobuf (v1.5.3) HOT 7
- Add option to JSON marshal / unmarshal: StripEnumPrefix / AddEnumPrefix HOT 10
- How to properly define protobuf `oneof` fields? HOT 2
- How can I import a pre-compiled proto from another package? HOT 4
- protodesc: feature "message_encoding" (in Protobuf editions) triggers proto2 group validation checks HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf.