Skip to content

Commit ad655da

Browse files
author
Mikael Setterberg
committed
Adding possibility to use custom JSONEncoder in order to be able to serialize arbitrary objects (such as date time).
1 parent 3b3b30b commit ad655da

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

pyArango/database.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,12 @@ def hasGraph(self, name):
170170
"""returns true if the databse has a graph by the name of 'name'"""
171171
return name in self.graphs
172172

173-
def AQLQuery(self, query, batchSize = 100, rawResults = False, bindVars = {}, options = {}, count = False, fullCount = False, **moreArgs) :
173+
def AQLQuery(self, query, batchSize = 100, rawResults = False, bindVars = {}, options = {}, count = False, fullCount = False,
174+
json_encoder = None, **moreArgs) :
174175
"""Set rawResults = True if you want the query to return dictionnaries instead of Document objects.
175176
You can use **moreArgs to pass more arguments supported by the api, such as ttl=60 (time to live)"""
176-
return AQLQuery(self, query, rawResults = rawResults, batchSize = batchSize, bindVars = bindVars, options = options, count = count, fullCount = fullCount, **moreArgs)
177+
return AQLQuery(self, query, rawResults = rawResults, batchSize = batchSize, bindVars = bindVars, options = options, count = count, fullCount = fullCount,
178+
json_encoder = json_encoder, **moreArgs)
177179

178180
def explainAQLQuery(self, query, allPlans = False) :
179181
"""Returns an explanation of the query. Setting allPlans to True will result in ArangoDB returning all possible plans. False returns only the optimal plan"""

pyArango/query.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ def __str__(self) :
131131

132132
class AQLQuery(Query) :
133133
"AQL queries are attached to and instanciated by a database"
134-
def __init__(self, database, query, batchSize, bindVars, options, count, fullCount, rawResults = True, **moreArgs) :
134+
def __init__(self, database, query, batchSize, bindVars, options, count, fullCount, rawResults = True,
135+
json_encoder = None, **moreArgs) :
135136
payload = {'query' : query, 'batchSize' : batchSize, 'bindVars' : bindVars, 'options' : options, 'count' : count, 'fullCount' : fullCount}
136137
payload.update(moreArgs)
137138

138139
self.query = query
139140
self.database = database
140141
self.connection = self.database.connection
141-
request = self.connection.session.post(database.cursorsURL, data = json.dumps(payload))
142+
request = self.connection.session.post(database.cursorsURL, data = json.dumps(payload, cls=json_encoder))
142143
Query.__init__(self, request, database, rawResults)
143144

144145
def explain(self, allPlans = False) :
@@ -165,14 +166,15 @@ def _raiseInitFailed(self, request) :
165166

166167
class SimpleQuery(Query) :
167168
"Simple queries are attached to and instanciated by a collection"
168-
def __init__(self, collection, queryType, rawResults, **queryArgs) :
169+
def __init__(self, collection, queryType, rawResults, json_encoder = None,
170+
**queryArgs) :
169171

170172
self.collection = collection
171173
self.connection = self.collection.database.connection
172174

173175
payload = {'collection' : collection.name}
174176
payload.update(queryArgs)
175-
payload = json.dumps(payload)
177+
payload = json.dumps(payload, cls=json_encoder)
176178
URL = "%s/simple/%s" % (collection.database.URL, queryType)
177179
request = self.connection.session.put(URL, data = payload)
178180

0 commit comments

Comments
 (0)