Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4581024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:51:55+00:00 2026-05-21T20:51:55+00:00

Using Google App Engine (python SDK), I created a custom JSONProperty() as a subclass

  • 0

Using Google App Engine (python SDK), I created a custom JSONProperty() as a subclass of db.TextProperty(). My goal is to store a python dict on the fly as JSON and retrieve it easily. I followed various examples found via Google and setting up the custom Property class and methods is pretty easy.

However, some of my dict values (strings) are encoded in utf-8. When saving the model into the datastore, I get a dreaded Unicode error (for datastore text property default encoding is ASCII). Subclassing db.BlobProperty didn’t solve the issue.

Basically, my code does the following thing : store Resource entities into the datastore (with URL as a StringProperty and POST/GET payloads stored in a dict as a JSONProperty), fetch them later (code not included). I choose not to use pickle for storing payloads because I’m a JSON freak and have no use storing objects.

Custom JSONProperty :

class JSONProperty(db.TextProperty):
    def get_value_for_datastore(self, model_instance):
        value = super(JSONProperty, self).get_value_for_datastore(model_instance)
        return json.dumps(value)

    def make_value_from_datastore(self, value):
        if value is None:
            return {}
        if isinstance(value, basestring):
            return json.loads(value)
        return value

Putting model into datastore :

res = Resource()
res.init_payloads()
res.url = "http://www.somesite.com/someform/"
res.param = { 'name': "SomeField", 'default': u"éàôfoobarç" }
res.put()

This will throw a UnicodeDecodeError related to ASCII encoding. Maybe it’s worth noting that I only get this error (everytime) on production server. I’m using python 2.5.2 on dev.

Traceback (most recent call last):
File “/base/data/home/apps/delpythian/1.350065314722833389/core/handlers/ResetHandler.py”, line 68, in _res_one
return res_one.put()
File “/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/init.py”, line 984, in put
return datastore.Put(self._entity, config=config)
File “/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore.py”, line 455, in Put
return _GetConnection().async_put(config, entities, extra_hook).get_result()
File “/base/python_runtime/python_lib/versions/1/google/appengine/datastore/datastore_rpc.py”, line 1219, in async_put
for pbs in pbsgen:
File “/base/python_runtime/python_lib/versions/1/google/appengine/datastore/datastore_rpc.py”, line 1070, in __generate_pb_lists
pb = value_to_pb(value)
File “/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore.py”, line 239, in entity_to_pb
return entity._ToPb()
File “/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore.py”, line 841, in _ToPb
properties = datastore_types.ToPropertyPb(name, values)
File “/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore_types.py”, line 1672, in ToPropertyPb
pbvalue = pack_prop(name, v, pb.mutable_value())
File “/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore_types.py”, line 1485, in PackString
pbvalue.set_stringvalue(unicode(value).encode(‘utf-8’))
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc3 in position 32: ordinal not in range(128)

My question is the following : is there a way to subclass a db.TextProperty() class and set/enforce a custom encoding ? Or am I doing something wrong ? I try to avoid using str() and follow the “Decode early, Unicode everywhere, encode late” rule.

Update : added code and stacktrace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:51:56+00:00Added an answer on May 21, 2026 at 8:51 pm

    Here’s a minimal example of moving a unicode string from a dictionary to a serialized JSON string to a TextProperty:

    class Thing(db.Model):
      json = db.TextProperty()
    
    class MainHandler(webapp.RequestHandler):
      def get(self):
        data = {'word': u"r\xe9sum\xe9"}
        json = simplejson.dumps(data, ensure_ascii=False)
        Thing(json=json).put()
    

    This works for me in both dev and prod.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Google App Engine SDK and Python, I'm facing an issue : I'm unable
I'm using Google App Engine and Django templates. I have a table that I
As the title suggests, I'm using Google App Engine and Django. I have quite
I've been using Pydev/Eclipse to develop Google App Engine (GAE) applications but I've been
I have a simple Google App Engine app, that I wrote using ordinary strings.
How can I read the colors of an image with python using google app
I am using google's appengine api from google.appengine.api import urlfetch to fetch a webpage.
I am using a perl script to POST to Google Appengine application. I post
Using google's Protocul Buffers , I have a service already written in Java which
When using Google Chrome, I want to debug some JavaScript code. How can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.