def decode(self, istring): """Decode JSON method""" if MODULE == 'cjson': return cjson.decode(istring) elif MODULE == 'yajl': return yajl.Decoder().decode(istring) return self.decoder.decode(istring)
def main_many_times(): contenders = [] try: import yajl contenders.append( ('yajl', (yajl.Encoder().encode, yajl.Decoder().decode))) except ImportError: pass try: import ujson contenders.append(('ujson', (ujson.dumps, ujson.loads))) except ImportError: pass try: import cjson contenders.append(('cjson', (cjson.encode, cjson.decode))) except ImportError: pass try: import simplejson contenders.append(('simplejson', (simplejson.dumps, simplejson.loads))) except ImportError: pass try: import json contenders.append(('json', (json.dumps, json.loads))) except ImportError: pass for name, args in contenders: test(*args) x, y = profile(*args) print("%-11s serialize: %0.3f deserialize: %0.3f total: %0.3f" % (name, x, y, x + y))
string_contenders = [ Contender('rapidjson utf8', partial(rj.dumps, ensure_ascii=False), rj.loads), Contender('rapidjson ascii', partial(rj.dumps, ensure_ascii=True), rj.loads), ] try: import yajl except ImportError: pass else: contenders.append( Contender('yajl', yajl.Encoder().encode, yajl.Decoder().decode)) try: import simplejson except ImportError: pass else: contenders.append( Contender('simplejson', simplejson.dumps, simplejson.loads)) string_contenders.extend([ Contender('simplejson utf8', partial(simplejson.dumps, ensure_ascii=False), simplejson.loads), Contender('simplejson ascii', partial(simplejson.dumps, ensure_ascii=True), simplejson.loads),
def run_client_test(name, serialize, deserialize, iterations=100 * 1000, data=default_data): squashed_data = serialize(data) serialize_profile = time_func(serialize, data, iterations) deserialize_profile = time_func(deserialize, squashed_data, iterations) return serialize_profile, deserialize_profile contenders = [] if yajl: contenders.append(('yajl', yajl.Encoder().encode, yajl.Decoder().decode)) if simplejson: contenders.append(('simplejson', simplejson.dumps, simplejson.loads)) if json: contenders.append(('stdlib json', json.dumps, json.loads)) if rapidjson: contenders.append(('rapidjson', rapidjson.dumps, partial(rapidjson.loads, precise_float=True))) if ujson: contenders.append( ('ujson', ujson.dumps, partial(ujson.loads, precise_float=True)))
def profile(serial, deserial, data=None, x=100 * 1000): if not data: data = default_data squashed = serial(data) return (ttt(serial, data, x), ttt(deserial, squashed, x)) def test(serial, deserial, data=None): if not data: data = default_data assert deserial(serial(data)) == data contenders = [ ('yajl', (yajl.Encoder().encode, yajl.Decoder().decode)), ] if cjson: contenders.append(('cjson', (cjson.encode, cjson.decode))) if simplejson: contenders.append(('simplejson', (simplejson.dumps, simplejson.loads))) if json: contenders.append(('stdlib json', (json.dumps, json.loads))) for name, args in contenders: test(*args) x, y = profile(*args) print("%-11s serialize: %0.3f deserialize: %0.3f total: %0.3f" % (name, x, y, x + y))
def decode(self, json): return yajl.Decoder().decode(json)
def setUp(self): self.d = yajl.Decoder()