Hard to tell without knowing what sort of errors are you getting..
But, have you tried the included Django JSON encoder:
from django.utils import simplejson
object = simplejson.loads("{JSON}")
string = simplejson.dumps(object)
On Nov 7, 3:22 pm, Hans <[EMAIL PROTECTED]> wrote:
> I have the following code to convert common objects to JSON. On my dev
> box it works fine, but on production is simply does not and I can't
> really debug. Any suggestions?
>
> import types
> class TestObject():
> def __init__(self):
> self.dict = {'test class':'jaja'}
> self.name = 'testobject'
> self.lijst = ['ss','s','jaj']
> self.bool = True
>
> class JsonEncoder():
> ATTRIBUTES_TO_SKIP = ['__doc__','__module__']
>
> @staticmethod
> def encode(obj):
> return JsonEncoder._encode(obj, type(obj))
>
> @staticmethod
> def _encode(obj,type,baseNr = 0):
> typeCaseSwitch= {
> types.DictType: JsonEncoder.encodeDict
> ,types.InstanceType:
> JsonEncoder.encodeInstance
> ,types.NoneType: JsonEncoder.encodeNone
> ,types.StringType: JsonEncoder.encodeStr
> ,types.UnicodeType: JsonEncoder.encodeStr
> ,types.ListType: JsonEncoder.encodeList
> ,types.BooleanType: JsonEncoder.encodeBool
> ,types.IntType: JsonEncoder.encodeInt
> ,types.TupleType: JsonEncoder.encodeList
> }
> try:
> return typeCaseSwitch[type](obj)
> except Exception, inst:
> if len(obj.__class__.__bases__) > 0:
> baseType = obj.__class__.__bases__[++baseNr]
> return JsonEncoder._encode(obj, baseType)
>
> @staticmethod
> def encodeDict(obj):
> result = []
> for key in obj:
> result.append(JsonEncoder.encode(key) + ':' +
> JsonEncoder.encode(obj[key]))
> return '{' + ','.join(result) + '}'
>
> @staticmethod
> def encodeList(obj):
> result = []
> for item in obj:
> result.append(JsonEncoder.encode(item))
> return '[' + ','.join(result) + ']' if len(result) > 0 else
> '[]'
>
> @staticmethod
> def encodeBool(obj):
> return str(obj).lower()
>
> @staticmethod
> def encodeInt(obj):
> return str(obj)
>
> @staticmethod
> def encodeStr(obj):
> #obj = obj.replace('/', r'\/')
> obj = obj.replace('"', r'\"')
> obj = obj.replace('\b', r'\b')
> obj = obj.replace('\f', r'\f')
> obj = obj.replace('\n', r'')
> obj = obj.replace('\r', r'')
> obj = obj.replace('\t', r'')
> return '"' + str(obj).replace('\\','\\\\').replace('\"','\\
> \"') + '"'
>
> @staticmethod
> def encodeNone(obj):
> return 'null'
>
> @staticmethod
> def encodeInstance(obj):
> callableAttribs = []
> for attrib in dir(obj):
> if not callable(getattr(obj, attrib)) and attrib not in
> JsonEncoder.ATTRIBUTES_TO_SKIP:
> callableAttribs.append('"' + attrib + '":' +
> JsonEncoder.encode(getattr(obj, attrib)))
> if len(callableAttribs) > 0:
> return '{' + ','.join(callableAttribs) + '}'
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---