Git Product home page Git Product logo

Comments (2)

houglum avatar houglum commented on May 4, 2024

Digging a bit further, I'm not certain if this issue should be filed here or in the google/apitools repository.

I've found that some fields actually encode this as expected -- our Object messages contain a separate metadata field -- this is a repeated field containing instances of a separate message type, MetadataValue, which is decorated with an apitools function called MapUnrecognizedFields. It seems that if you a supply a value of None for one of these unrecognized fields, it will be JSON-encoded as null, rather than omitting the key/value pair altogether.

As an example, when encoding an Object message containing one of these (with a key of "foo" and a value of None) to JSON, the value of null will be present, rather than omitting the attribute:

(Pdb) obj.metadata = apitools_messages.Object.MetadataValue()
(Pdb) obj.metadata.additionalProperties = []
(Pdb) obj
<Object
 acl: []
 metadata: <MetadataValue
 additionalProperties: []>>

(Pdb) obj.metadata.additionalProperties.append(apitools_messages.Object.MetadataValue.AdditionalProperty(key='foo', value=None))
(Pdb) obj
<Object
 acl: []
 metadata: <MetadataValue
 additionalProperties: [<AdditionalProperty
 key: 'foo'>]>>

(Pdb) encoding.MessageToJson(obj)
'{"metadata": {"foo": null}}'

Although this encoding module is defined within apitools here, and looks to be its own extension of protorpc's ProtoJson class, so probably outside the scope of this repository.

from protorpc.

houglum avatar houglum commented on May 4, 2024

Upon further investigation, it looks like apitools has an IncludeFields method that takes care of this:
https://github.com/google/apitools/blob/2bfa257a65e335ef67afc9f135c7796f47f85535/apitools/base/py/base_api.py#L356

from protorpc.

Related Issues (17)

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.