Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'package_show-performance-pt3'
Conflicts: ckan/logic/action/get.py
- Loading branch information
Showing
5 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import simplejson as json | ||
import simplejson.encoder as json_encoder | ||
|
||
|
||
class LazyJSONObject(dict): | ||
'''An object that behaves like a dict returned from json.loads''' | ||
def __init__(self, json_string): | ||
self._json_string = json_string | ||
self._json_dict = None | ||
|
||
def _loads(self): | ||
if not self._json_dict: | ||
self._json_dict = json.loads(self._json_string) | ||
self._json_string = None | ||
return self._json_dict | ||
|
||
def __nonzero__(self): | ||
return True | ||
|
||
def for_json(self): | ||
if self._json_string: | ||
return JSONString(self._json_string) | ||
return self._json_dict | ||
|
||
|
||
def _loads_method(name): | ||
def method(self, *args, **kwargs): | ||
return getattr(self._loads(), name)(*args, **kwargs) | ||
return method | ||
|
||
for fn in ['__cmp__', '__contains__', '__delitem__', '__eq__', '__ge__', | ||
'__getitem__', '__gt__', '__iter__', '__le__', '__len__', '__lt__', | ||
'__ne__', '__setitem__', 'clear', 'copy', 'fromkeys', 'get', | ||
'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', | ||
'pop', 'popitem', 'setdefault', 'update', 'values']: | ||
setattr(LazyJSONObject, fn, _loads_method(fn)) | ||
|
||
|
||
class JSONString(int): | ||
''' | ||
A type for already-encoded JSON | ||
Fake-out simplejson by subclassing int so that simplejson calls | ||
our __str__ method to produce JSON. | ||
This trick is unpleasant, but significantly less fragile than | ||
subclassing JSONEncoder and modifying its internal workings, or | ||
monkeypatching the simplejson library. | ||
''' | ||
def __init__(self, s): | ||
self.s = s | ||
super(JSONString, self).__init__(-1) | ||
|
||
def __str__(self): | ||
return s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters