Search code examples
python-2.7google-app-enginegoogle-cloud-datastoreapp-engine-ndbapp-engine-flexible

NDB JsonProperty and Cloud Datastore library compatibility


I have code running on Standar GAE using NDB and code running in flexible env using Google Cloud datastore library. Both accessing to the same entities.

I have problems dealing with ndb.JsonProperty. As far as I read those properties are stored as blob, so I tried to simulate that property using cloud library. Before store the value I do the following:

value_to_store = json.dumps(value, separators=[',',':'])
value_to_store = base64.b64encode(value_to_store)

And the opposite when I read a property:

read_value = base64.b64decode(from_db_value)
read_value = json.loads(read_value)

Everything works fine in this situations:

Insert using NDB ---> Read using Cloud Library
Insert using Cloud Library ---> Read using Cloud Library

But fails when:

Insert using Cloud Library --> Read using NDB

What's the right way to store those kind of properties to make them compatibles with NDB?

Thanks.


Solution

  • Finally I found the solution.

    The point is that NDB is storing the value as blob while the library is storing as string.

    The solution is just do not encode/decode the string value, the library will do it and will store the value as blob, that is what NDB expects.

    Writing:

    value_to_store = json.dumps(value, separators=[',',':'])
    

    Reading:

    read_value = json.loads(read_value)
    

    Easy!