def send(self, evt, data={}): if self.live == False: return message = microjson.to_json({"evt": evt, "data": data}) Log.info("Sending message: "+message) self._socket.send(message+"\n")
def loadSettings(): with open('../public/api/v1/data/system/sysinfo.json') as config_file: content = config_file.read() config = microjson.from_json(content) msg_data = {} msg_data['name'] = config['name'] msg_data['location'] = config['location'] return microjson.to_json(msg_data)
def loadSettings(): with open('config.json') as config_file: content = config_file.read() config = microjson.from_json(content) msg_data = {} msg_data['name'] = config['name'] msg_data['location'] = config['location'] return microjson.to_json(msg_data)
def test_string_object(self): class Bag: pass cases = [("hello", '"hello"'), (u"\u2018hi\u2019", r'"\u2018hi\u2019"')] for py, js in cases: for meth in ("__str__", "__unicode__"): obj = Bag() setattr(obj, meth, lambda: py) r = microjson.to_json(obj) self.assertEquals(r, js) class NObj: pass self.assertRaises(microjson.JSONError, microjson.encode, NObj())
def test_string_object(self): class Bag: pass cases = [("hello", '"hello"'), (u"\u2018hi\u2019", r'"\u2018hi\u2019"')] for py, js in cases: for meth in ('__str__', '__unicode__'): obj = Bag() setattr(obj, meth, lambda: py) r = microjson.to_json(obj) self.assertEquals(r, js) class NObj: pass self.assertRaises(microjson.JSONError, microjson.encode, NObj())
def _render(self, parent, obj): ''' Implementation of __str__. ''' def _dotted(k): if parent: return '%s.%s' % (parent, k) return k # output this instance's keys buf = '' subs = [] vals = 0 for key in obj._key_order: if isinstance(key, int): comment = obj._comments.get(key, '') buf += _format_comment('#', comment) continue val = obj[key] if isinstance(val, Wax): subs.append((key, val)) continue vals += 1 note = obj._annotations.get(key, '') buf += _format_comment(';', note) buf += key + ' = ' + microjson.to_json(val) + '\n' # output sub-instances for key, inst in subs: note = obj._annotations.get(key, '') buf += _format_comment(';', note) buf += self._render(_dotted(key), inst) # only output this group header if: # - we have at least 1 non sub-instance key # - we have no contents at all # we output a header when we are empty in order to completely # recreate the original hierarchy. if parent and (vals or not subs): return ('\n[%s]\n' % parent) + buf return buf
def _render(self, parent, obj): ''' Implementation of __str__. ''' def _dotted(k): if parent: return '%s.%s' % (parent, k) return k # output this instance's keys buf = '' subs = [] vals = 0 for key in obj._key_order: if isinstance(key, int): comment = obj._comments.get(key, '') buf += _format_comment('#', comment) continue val = obj[key] if isinstance(val, Wax): subs.append( (key, val) ) continue vals += 1 note = obj._annotations.get(key, '') buf += _format_comment(';', note) buf += key + ' = ' + microjson.to_json(val) + '\n' # output sub-instances for key, inst in subs: note = obj._annotations.get(key, '') buf += _format_comment(';', note) buf += self._render(_dotted(key), inst) # only output this group header if: # - we have at least 1 non sub-instance key # - we have no contents at all # we output a header when we are empty in order to completely # recreate the original hierarchy. if parent and (vals or not subs): return ('\n[%s]\n' % parent) + buf return buf
def _run_cases(self, cases): for py, js in cases: r = microjson.to_json(py) self.assertEquals(r, js)
vm = CleVM() vm.Cpu.Registers[0] = obj vm.Memory.write([Clops["SER"],Clops["DON"]]) j = time.time() vm.Cpu.run() bytes = vm.Cpu.Registers[0] clvmEncodeTime = time.time()-j if tryUjson: j = time.time() asuson = ujson.dumps(obj) usonEncodeTime = time.time()-j j = time.time() asjson = microjson.to_json(obj) jsonEncodeTime = time.time()-j print "Compiled to:", bytes print "Executing" vm.Memory.write(bytes) j = time.time() vm.Cpu.run() clvmDecodeTime = time.time() - j if tryUjson: j = time.time() ujson.loads(asuson) usonDecodeTime = time.time() - j