JSON

protobuf转json,protobufjson(3)

字号+ 作者:H5之家 来源:H5之家 2017-11-29 16:25 我要评论( )

然后就是需要对repeated的字段进行相应的转换, 1 void GetRepeatedJson(std:: string pb2jsonstring, const ::google::protobuf::Message msg, const google::protobuf::FieldDescriptor *goal_field, const ::goo

    然后就是需要对repeated的字段进行相应的转换,

1 void GetRepeatedJson(std::string& pb2jsonstring, const ::google::protobuf::Message& msg, const google::protobuf::FieldDescriptor *goal_field, const ::google::protobuf::Reflection *reflection, bool Enum_2_Str,bool Showzero) 2 { 3 std::string tmp_string; 4 int judge=0; 5 std::int32_t v32=0; 6 std::uint32_t vu32=0; 7 std::int64_t v64=0; 8 std::uint64_t vu64=0; 9 double vd=0; 10 std::string str; 11 for (int i = 0; i < reflection->FieldSize(msg, goal_field); ++i) 12 { 13 switch (goal_field->type()) 14 { 15 case FieldDescriptor::TYPE_MESSAGE: 16 { 17 const Message& tmp_msg = reflection->GetRepeatedMessage(msg, goal_field, i); 18 if (0 != tmp_msg.ByteSize()) 19 { 20 tmp_string=""; NeedEmptyToJson(tmp_string, tmp_msg, Enum_2_Str,Showzero); 23 judge = AppendTmpString2(pb2jsonstring,tmp_string,judge); 24 } 25 } 26 break; 27 case FieldDescriptor::TYPE_INT32: 28 { 29 v32=reflection->GetRepeatedInt32(msg, goal_field,i); 30 if(v32==0) 31 { 32 if(Showzero) 33 { 34 tmp_string=""; 35 tmp_string.append(std::to_string(v32)); 36 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 37 } 38 } { 41 tmp_string=""; 42 tmp_string.append(std::to_string(v32)); 43 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 44 } 45 } ; 48 case FieldDescriptor::TYPE_UINT32: 49 { 50 vu32=reflection->GetRepeatedUInt32(msg, goal_field,i); 51 if(vu32==0) 52 { 53 if(Showzero) 54 { 55 tmp_string=""; 56 tmp_string.append(std::to_string(vu32)); 57 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 58 } 59 } { 62 tmp_string=""; 63 tmp_string.append(std::to_string(vu32)); 64 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 65 } 66 } 67 break; 68 case FieldDescriptor::TYPE_INT64: 69 { 70 v64=reflection->GetRepeatedInt64(msg, goal_field,i); 71 if(v64==0) 72 { 73 if(Showzero) 74 { 75 tmp_string=""; 76 tmp_string.append(std::to_string(v64)); 77 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 78 } 79 } { 82 tmp_string=""; 83 tmp_string.append(std::to_string(v64)); 84 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 85 } 86 } 87 break; 88 case FieldDescriptor::TYPE_UINT64: 89 { 90 vu64=reflection->GetRepeatedUInt64(msg, goal_field,i); 91 if(vu64==0) 92 { 93 if(Showzero) 94 { 95 tmp_string=""; 96 tmp_string.append(std::to_string(vu64)); 97 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 98 } 99 } { 102 tmp_string=""; 103 tmp_string.append(std::to_string(vu64)); 104 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 105 } 106 107 } 108 break; 109 case FieldDescriptor::TYPE_STRING: 110 case FieldDescriptor::TYPE_BYTES: 111 { 112 str=""; 113 str=reflection->GetRepeatedString(msg, goal_field,i); 114 if(str.empty()) 115 { 116 if(Showzero) 117 { 118 tmp_string=""; ).append(str).append(); 120 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 121 } 122 } { 125 tmp_string=""; ).append(str).append(); 127 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 128 129 } 130 } 131 break; 132 case FieldDescriptor::TYPE_DOUBLE: 133 { 134 vd=reflection->GetRepeatedDouble(msg, goal_field,i); 135 if(vd==0) 136 { 137 if(Showzero) 138 { 139 tmp_string=""; 140 tmp_string.append(std::to_string(vd)); 141 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 142 } 143 } { 146 tmp_string=""; 147 tmp_string.append(std::to_string(vd)); 148 judge = AppendTmpString1(pb2jsonstring,tmp_string,judge); 149 } 150 151 } 152 break; 153 case FieldDescriptor::TYPE_BOOL: 154 { 155 tmp_string=""; 156 if(reflection->GetRepeatedBool(msg, goal_field,i)) ); tmp_string.append(); 160 judge = AppendTmpString2(pb2jsonstring,tmp_string,judge); 161 162 } 163 break; 164 case FieldDescriptor::TYPE_ENUM: 165 { 166 tmp_string=""; 167 if (Enum_2_Str) 168 { ).append(reflection->GetRepeatedEnum(msg,goal_field,i)->name()).append(); 170 } { enumstr[8]; 174 memset(enumstr, 0, sizeof(enumstr)); , reflection->GetRepeatedEnum(msg,goal_field,i)->number()); 176 tmp_string.append(enumstr); 177 } 178 judge = AppendTmpString2(pb2jsonstring,tmp_string,judge); 179 } 180 break; 181 default: 182 break; 183 } 184 } 185 } 186 int AppendTmpString2(std::string &pb2jsonstring,std::string &tmp_string, int judge) 187 { 188 if( judge!=0 && tmp_string.length()!=0) 189 { ).append(tmp_string); 191 return judge; 192 } ( judge==0 && tmp_string.length()!=0) 194 { 195 pb2jsonstring.append(tmp_string); ; 197 } 198 return judge; 199 } 总结说明

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Java Bean和JSON相互转换的思路

    Java Bean和JSON相互转换的思路

    2017-11-30 10:17

  • Ajax+Jsp+servlet+json技术的使用

    Ajax+Jsp+servlet+json技术的使用

    2017-11-29 12:22

  • 教程|JSON处理1.1:JSON指针挑战和应对

    教程|JSON处理1.1:JSON指针挑战和应对

    2017-11-29 10:13

  • Python的数据序列化「Json Pickle」

    Python的数据序列化「Json Pickle」

    2017-11-28 12:09

网友点评
o