json is unicode by default... https://docs.python.org/2/library/json.html#encoders-and-decoders
web is full of examples and even web2py's core deals with it :P https://github.com/web2py/web2py/blob/master/gluon/scheduler.py#L171 On Sunday, March 30, 2014 8:54:16 PM UTC+2, mweissen wrote: > > Validator: only a typo, I have used IS_JSON() > > Without json.dumps / json.loads: same results. > In my emails I have shown a simplified code. This is a part of the real > code: > > --------------------------------------------------------------- > *db.py:* > > db.define_table("antworten", > .... > Field('nn','json', requires=IS_JSON()), > ) > > --------------------------------------------------------------- > *This function writes to the database:* > > def testantworten0(): > d={"f":"testantworten2", "vars":dict(x=3)} > print str(d),* type(d["f"])* > v=db.antworten.insert( nn=d, ) > return dict(form=v) > > The result of the print statement: > {'vars': {'x': 3}, 'f': 'testantworten2'}* <type 'str'>* > > --------------------------------------------------------------- > *This function reads from the database:* > > def antwortAuswerten(typ, antwort, absender='%', doNext=False): > q=db((db.antworten.typ==typ) & db.antworten.absender.like(absender)) > for res in q.select(): > print res.nn, type(res.nn) > print res.nn['f'],* type(res.nn['f'])* > > And it prints: > > {u'vars': {u'x': 3}, u'f': u'testantworten2'} <type 'dict'> > testantworten2* <type 'unicode'>* > > --------------------------------------------------------------- > > I put a dict with a *str* into my table and I get back a *unicode.* > Is this the expected behavior? > > > > > 2014-03-30 20:02 GMT+02:00 Anthony <[email protected] <javascript:>>: > >> First, you don't need to explicitly specify the validator, but if you do, >> it should be IS_JSON(). Second, once you select a record, you do not need >> to pass the resulting value to json.loads() -- web2py will automatically >> convert the stored JSON value to a Python object (that's the point of the >> JSON field type). >> >> Anthony >> >> >> On Sunday, March 30, 2014 12:59:11 PM UTC-4, mweissen wrote: >> >>> Sorry, I did not test all cases. >>> E.g.: >>> >>> Field ('url', 'json', requires=IS_JSON) >>> ... >>> res = db(db.mydb.id==1).select().first() >>> print json.loads(res.url) >>> >>> >>> Again res.url is something like >>> {'u'f : u'testfunction' ... } >>> >>> and URL(**res.url) fails. >>> >>> >>> >>> 2014-03-30 18:21 GMT+02:00 Martin Weissenboeck >>> <[email protected]<javascript:> >>> >: >>> >>>> That's the answer! I have forgotten the "json" type parameter, I had >>>> only requires=IS_JSON() >>>> I have tried it again and now it works. Thank you! >>>> >>>> >>>> >>>> >>>> 2014-03-30 15:10 GMT+02:00 Anthony <[email protected] <javascript:>>: >>>> >>>> Have you tried: >>>>> >>>>> Field('url', 'json') >>>>> >>>>> Anthony >>>>> >>>>> On Saturday, March 29, 2014 7:11:34 PM UTC-4, mweissen wrote: >>>>> >>>>>> Let's say I have >>>>>> URL(f="myfunction", vars=dict(x=1)) >>>>>> or >>>>>> v = {"f":"myfunction", "vars":{"x":1}} >>>>>> URL(**v) >>>>>> >>>>>> That works fine. >>>>>> >>>>>> Now I do >>>>>> j = json.dumps(v) >>>>>> >>>>>> I store j in >>>>>> Field("url", requires=IS_JSON()) >>>>>> >>>>>> Now I read this field and call URL(**fieldvalue). I get an error: >>>>>> >>>>>> <type 'exceptions.SyntaxError'> when calling URL, function or >>>>>> function name required >>>>>> >>>>>> The fieldvalue I get is >>>>>> {'u'f : u'festfunction' ... } >>>>>> >>>>>> The key is a unicode string and not utf-8 coded. >>>>>> >>>>>> What is wrong? How can I get an utf-8 string? >>>>>> >>>>>> Regards, Martin >>>>>> >>>>>> >>>> >>>> > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

