Convert a repeated field to a JSON array and add it to 'obj'.
| 38 | |
| 39 | // Convert a repeated field to a JSON array and add it to 'obj'. |
| 40 | static void RepeatedFieldToJson(const google::protobuf::Message& pb, |
| 41 | const google::protobuf::Reflection* reflection, |
| 42 | const google::protobuf::FieldDescriptor* field, Document* document, Value* obj) { |
| 43 | Value arr(rapidjson::kArrayType); |
| 44 | int size = reflection->FieldSize(pb, field); |
| 45 | for (int i = 0; i < size; i++) { |
| 46 | switch (field->cpp_type()) { |
| 47 | case google::protobuf::FieldDescriptor::CPPTYPE_INT32: |
| 48 | arr.PushBack( |
| 49 | reflection->GetRepeatedInt32(pb, field, i), document->GetAllocator()); |
| 50 | break; |
| 51 | case google::protobuf::FieldDescriptor::CPPTYPE_INT64: |
| 52 | arr.PushBack( |
| 53 | reflection->GetRepeatedInt64(pb, field, i), document->GetAllocator()); |
| 54 | break; |
| 55 | case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: |
| 56 | arr.PushBack( |
| 57 | reflection->GetRepeatedUInt32(pb, field, i), document->GetAllocator()); |
| 58 | break; |
| 59 | case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: |
| 60 | arr.PushBack( |
| 61 | reflection->GetRepeatedUInt64(pb, field, i), document->GetAllocator()); |
| 62 | break; |
| 63 | case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: |
| 64 | arr.PushBack( |
| 65 | reflection->GetRepeatedFloat(pb, field, i), document->GetAllocator()); |
| 66 | break; |
| 67 | case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: |
| 68 | arr.PushBack( |
| 69 | reflection->GetRepeatedDouble(pb, field, i), document->GetAllocator()); |
| 70 | break; |
| 71 | case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: |
| 72 | arr.PushBack(reflection->GetRepeatedBool(pb, field, i), document->GetAllocator()); |
| 73 | break; |
| 74 | case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: { |
| 75 | Value enum_str(reflection->GetRepeatedEnum(pb, field, i)->name(), |
| 76 | document->GetAllocator()); |
| 77 | arr.PushBack(enum_str, document->GetAllocator()); |
| 78 | break; |
| 79 | } |
| 80 | case google::protobuf::FieldDescriptor::CPPTYPE_STRING: { |
| 81 | string str = reflection->GetRepeatedString(pb, field, i); |
| 82 | Redact(&str, nullptr); |
| 83 | Value val_str(str, document->GetAllocator()); |
| 84 | arr.PushBack(val_str, document->GetAllocator()); |
| 85 | break; |
| 86 | } |
| 87 | case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { |
| 88 | Value child_obj(rapidjson::kObjectType); |
| 89 | ProtobufToJson( |
| 90 | reflection->GetRepeatedMessage(pb, field, i), document, &child_obj); |
| 91 | arr.PushBack(child_obj, document->GetAllocator()); |
| 92 | break; |
| 93 | } |
| 94 | default: |
| 95 | DCHECK(false) << "Type NYI: " << field->cpp_type() << " " << field->name(); |
| 96 | } |
| 97 | } |
no test coverage detected