def findreq(protocol, f): #libtxt_recall = CDLL("./libtxt_recall.so") #libtxt_recall.Init() if protocol == "stream": return pbjson.dict2pb( websearch2.proto.news_recommendation.guanxin_news_rcm_pb2. NewsRcmRequest, s) #return pbjson.dict2pb(so.NewsRcmRequest, s) elif protocol == "recall": return pbjson.dict2pb( websearch2.proto.news_recommendation.newsrcm_txt_recall_pb2. Request, s) #return pbjson.dict2pb(libtxt_recall._ZNK2pb3rcm5queue3txt17RelatedTxtRequest3hotEv, s) else: return pbjson.dict2pb( websearch2.proto.news_recommendation.newsrcm_score_pb2.Request, s)
def convertToPB(): pbDict = {} import item_data as ID return pbDict for k, v in ID.data.iteritems(): pb = pbjson.dict2pb(Item_Data, v) pbDict[k] = pb return pbDict
def genConvertedBytes(self, key, value): byteList = [] keyBytes = self.genKeyBytes(key) fieldDict = self.convertToNormalDict(value, self.eFields) if type(value) == dict: dataType = DataProtoBufConverter.VALUE_TYPE_PB pbObj = pbjson.dict2pb(self.pbClass, fieldDict) vbytes = pbObj.SerializeToString() else: dataType = DataProtoBufConverter.VALUE_TYPE_E vbytes = p.dumps(fieldDict) lenBytes = struct.pack('2h1i', dataType, len(keyBytes), len(vbytes)) byteList.append(lenBytes) byteList.append(keyBytes) byteList.append(vbytes) return byteList
def read_from_jsonfile(filename): with open(filename, "r") as file_obj: # json_data = file_obj.read() #这句并不是json数据 json_data = simplejson.load(file_obj) # 或者 # file_obj = open(filename) # json_data = simplejson.load(file_obj) # file_obj.close print(" 1).json_data from file ->\n", json_data, type(json_data)) address_book = pbjson.dict2pb(addressbook_pb2.AddressBook, json_data) print(" 2).parse address_book from json_data ->\n", address_book, type(address_book)) print(" 3).use address_book ->") for person in address_book.persons: print("name: {}, age: {}".format(person.name, person.age))
def handle_body(body, isProtobuf=True): """处理body""" if not body: body = None return body print type(body) if isProtobuf: #转换json为protobuf body_json = simplejson.loads(body) print type(body_json) logger.info("body is %s", str(body_json)) a1 = pbjson.dict2pb(SkmrMain_pb2.SkmrMsg, body_json) logger.info("SkmrMsg is %s", str(a1)) body = a1.SerializeToString() return body
{ "number": "+1234567890", "type": 0 }, { "number": "+2345678901", "type": 1 } ] }, { "name": "Ben Bun", "id": 2, "email": "*****@*****.**", "phone": [ { "number": "+1234567890", "type": 0 } ] } ] } #Convert JSoN to Protobuf adr_book = pbjson.dict2pb(AddressBook, adr_book_json) #Convert Protobuf to JSoN new_json = pbjson.pb2json(adr_book)
import pbjson import map_pb2 d = {'d': {1: {'id': 1, 'name': 'hello'}, 2: {'id': 2, 'name': 'world'}}} m = pbjson.dict2pb(map_pb2.M, d) print m
adr_book_json = { "person": [{ "name": "Mohamed Lee", "id": 1, "phone": [{ "number": "+1234567890", "type": 0 }, { "number": "+2345678901", "type": 1 }] }, { "name": "Ben Bun", "id": 2, "email": "*****@*****.**", "phone": [{ "number": "+1234567890", "type": 0 }] }] } #Convert JSoN to Protobuf adr_book = pbjson.dict2pb(AddressBook, adr_book_json) #Convert Protobuf to JSoN new_json = pbjson.pb2json(adr_book)
"name": "peking university", "city": "Beijing" }, { "name": "BHSF", "city": "Beijing" }], "used_name": ["Tom", "Jerry"], "ht": { "city": "Shanghai" } }, { "name": "Ben Bun", "id": 2, "email": "*****@*****.**", "phone": [{ "number": "+1234567890", "type": 0 }] }] } # Convert JSoN to Protobuf adr_book = pbjson.dict2pb(AddressBook, adr_book_json, extend=extend_pb2) # print adr_book import simplejson as json print json.dumps(adr_book_json, indent=4) # Convert Protobuf to JSoN new_json = pbjson.pb2json(adr_book) print new_json