Comments (9)
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.
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.
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.
@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.
Weird - I'm using web2py trunk, and don't see this version there...
from pydal.
@nursix you're right. Let me check with the other developers.
from pydal.
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.
@nursix no problem. web2py trunk now is patched: web2py/web2py#930
from pydal.
Excellent, many thanks.
from pydal.
Related Issues (20)
- GHSL-2021-116 HOT 1
- Fix unable to parse csv file in web2py break CSV parsing when dynamic upload is used
- MSSQL adapter, tableX_view.insert (..) + trigger INSTEAD OF INSERT, .lastrowid (), "SELECT SCOPE_IDENTITY ()", return long (../ None / ..)
- NotNullViolation on PostgreSQL migration of populated table with a notnull field
- The user name or password contains @, unable to connect to MySQL HOT 4
- Deprecation imports google.appengine.api.rdbms in _gae and google adapter HOT 1
- Upgrade to python3 datastore adapter. Help
- problem with .contains() when passing list as argument HOT 1
- pool_size and leaking connections on PostgreSQL
- erro calling db.uuid method HOT 1
- count field in restapi HOT 1
- non-database upload field fails to build filename
- web2py_filesystem Updates/Corrections
- REGEXP_LIKE missing parameter "match_parameter" in oracle
- Incorrect sqlite placeholder for InDBMigrator
- Database is locked, attempts not working
- Error in the class FireBird
- import_from_csv_file fails if export_to_csv_file with ref id column with gaps
- PyDAL for Azure MySQL
- PyDAL for Redis
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydal.