def test_generate_models_json_array(self): models_json = self.hwp5file.bodytext.section(0).models_json() gen = models_json.generate() simplejson = importjson() json_array = simplejson.loads(''.join(gen)) self.assertEquals(128, len(json_array))
def test_models_json_open(self): simplejson = importjson() f = self.docinfo.models_json().open() try: self.assertEquals(67, len(simplejson.load(f))) finally: f.close()
def test_generate_simplejson_dumps(self): simplejson = importjson() records_json = self.hwp5file.docinfo.records_json() json = ''.join(records_json.generate()) jsonobject = simplejson.loads(json) self.assertEquals(67, len(jsonobject))
def test_model_to_json(self): from hwp5.binmodel import model_to_json model = self.hwp5file.docinfo.model(0) json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals('DocumentProperties', jsonobject['type'])
def test_head_record_stream(self): from hwp5.tagids import HWPTAG_DISTRIBUTE_DOC_DATA from hwp5.importhelper import importjson simplejson = importjson() stream = self.jscriptversion.head_record_stream() record = simplejson.load(stream) self.assertEquals(HWPTAG_DISTRIBUTE_DOC_DATA, record['tagid']) # stream should have been exausted self.assertEquals('', stream.read(1))
def test_model_to_json_with_unparsed(self): from hwp5.binmodel import model_to_json model = dict(type=RecordModel, content=[], payload='\x00\x01\x02\x03', unparsed='\xff\xfe\xfd\xfc') json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals(['ff fe fd fc'], jsonobject['unparsed'])
def test_model_to_json_with_controlchar(self): from hwp5.binmodel import model_to_json model = self.hwp5file.bodytext.section(0).model(1) json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals('ParaText', jsonobject['type']) self.assertEquals( [[0, 8], dict(code=2, param='\x00' * 8, chid='secd')], jsonobject['content']['chunks'][0])
def test_model_to_json_with_controlchar(self): from hwp5.binmodel import model_to_json model = self.hwp5file.bodytext.section(0).model(1) json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals('ParaText', jsonobject['type']) self.assertEquals([[0, 8], dict(code=2, param='\x00' * 8, chid='secd')], jsonobject['content']['chunks'][0])
def model_to_json(model, *args, **kwargs): ''' convert a model to json ''' json = importjson() model = dict(model) model['type'] = model['type'].__name__ record = model record['payload'] = list(dumpbytes(record['payload'])) if 'unparsed' in model: model['unparsed'] = list(dumpbytes(model['unparsed'])) if 'binevents' in model: del model['binevents'] return json.dumps(model, *args, **kwargs)
def test_record_to_json(self): from hwp5.recordstream import record_to_json simplejson = importjson() record = self.hwp5file.docinfo.records().next() json = record_to_json(record) jsonobject = simplejson.loads(json) self.assertEquals(16, jsonobject['tagid']) self.assertEquals(0, jsonobject['level']) self.assertEquals(26, jsonobject['size']) self.assertEquals(['01 00 01 00 01 00 01 00 01 00 01 00 01 00 00 00', '00 00 07 00 00 00 05 00 00 00'], jsonobject['payload']) self.assertEquals(0, jsonobject['seqno']) self.assertEquals('HWPTAG_DOCUMENT_PROPERTIES', jsonobject['tagname'])
def test_record_to_json(self): from hwp5.recordstream import record_to_json simplejson = importjson() record = self.hwp5file.docinfo.records().next() json = record_to_json(record) jsonobject = simplejson.loads(json) self.assertEquals(16, jsonobject['tagid']) self.assertEquals(0, jsonobject['level']) self.assertEquals(26, jsonobject['size']) self.assertEquals([ '01 00 01 00 01 00 01 00 01 00 01 00 01 00 00 00', '00 00 07 00 00 00 05 00 00 00' ], jsonobject['payload']) self.assertEquals(0, jsonobject['seqno']) self.assertEquals('HWPTAG_DOCUMENT_PROPERTIES', jsonobject['tagname'])
def record_to_json(record, *args, **kwargs): ''' convert a record to json ''' from .dataio import dumpbytes json = importjson() record['payload'] = list(dumpbytes(record['payload'])) return json.dumps(record, *args, **kwargs)