Code Monkey home page Code Monkey logo

Comments (9)

nursix avatar nursix commented on August 17, 2024

Related to this commit, btw:

788df0e#diff-68220c912f02a03fd8bce58415479334L2742

The datetime_to_str logic should probably be retained here (plus the above fix) to make it work with default json.dumps again?

Or is there another way?

from pydal.

gi0baro avatar gi0baro commented on August 17, 2024

Hi nursix, this is my test on current master:

>>> from pydal import DAL, Field
>>> db = DAL()
>>> db.define_table('test', Field('a'), Field('date', 'datetime'))
<Table test (id,a,date)>
>>> from datetime import datetime
>>> db.test.insert(a='asd', date=datetime.utcnow())
1L
>>> rows = db(db.test.id > 0).select()
>>> rows.as_json()
u'[{"a": "asd", "date": "2015-04-20 14:10:07", "id": 1}]'
>>> rows[0].as_json()
u'{"a": "asd", "date": "2015-04-20 14:10:07", "id": 1}'

The current serialization in pyDAL actually converts date, time and datetime objects to strings in order to give valid JSON output.

Am I missing something?

from pydal.

nursix avatar nursix commented on August 17, 2024

I think the problem is that the latest pyDAL version (using serializers.json for Rows.as_json) isn't in web2py yet.

I repeated your test with the current web2py version and get this:

>>> from pydal import DAL, Field
>>> from datetime import datetime
>>> db.define_table('test', Field('a'), Field('date', 'datetime'))
<Table test (id,a,date)>
>>> from datetime import datetime
>>> db.test.insert(a='asd', date=datetime.utcnow())
2L
>>> rows = db(db.test.id > 0).select()
>>> rows.as_json()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/dominic/work/web2py/gluon/packages/dal/pydal/objects.py", line 2717, in as_json
    return json.dumps(items)
  File "/usr/lib64/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib64/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib64/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib64/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2015, 4, 20, 15, 2, 39) is not JSON serializable
>>> rows[0].as_json()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/dominic/work/web2py/gluon/packages/dal/pydal/objects.py", line 213, in as_json
    return json.dumps(item)
  File "/usr/lib64/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib64/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib64/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib64/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2015, 4, 20, 15, 2, 39) is not JSON serializable
>>> 

Would your test also work with the result of a join (i.e. datetime in a nested Row?).

In any case - if it works with the latest PyDAL, then it would be good to update web2py so that users benefit from the fix.

Many thanks.

from pydal.

gi0baro avatar gi0baro commented on August 17, 2024

@nursix yes, It's already merged on master as you can see from web2py/web2py#923

Will be released in 2.10.4 in the next days.

from pydal.

nursix avatar nursix commented on August 17, 2024

Weird - I'm using web2py trunk, and don't see this version there...

from pydal.

gi0baro avatar gi0baro commented on August 17, 2024

@nursix you're right. Let me check with the other developers.

from pydal.

nursix avatar nursix commented on August 17, 2024

No urgency - I've patched my local pyDAL, and blacklisted the current web2py trunk version for deployments with our system. That's btw the actual reason why I'm routinely testing our system (=Sahana Eden) against web2py trunk: detecting regression problems.

Just trying to make sure that it either gets fixed upstream, or we have a workaround for it in place.
Sorry for bothering you.

from pydal.

gi0baro avatar gi0baro commented on August 17, 2024

@nursix no problem. web2py trunk now is patched: web2py/web2py#930

from pydal.

nursix avatar nursix commented on August 17, 2024

Excellent, many thanks.

from pydal.

Related Issues (20)

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.