Code Monkey home page Code Monkey logo

Comments (8)

puellanivis avatar puellanivis commented on June 2, 2024

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.

dsnet avatar dsnet commented on June 2, 2024

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.

aktau avatar aktau commented on June 2, 2024

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.

go-aegian avatar go-aegian commented on June 2, 2024

@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.

aktau avatar aktau commented on June 2, 2024

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.

go-aegian avatar go-aegian commented on June 2, 2024

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.

aktau avatar aktau commented on June 2, 2024

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).

from protobuf.

go-aegian avatar go-aegian commented on June 2, 2024

@aktau, thanks for the support and info provided. Closing this question then.

from protobuf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.