Implementation:Alibaba MNN Protobuf Wire Format H
Metadata
| Attribute | Value |
|---|---|
| Implementation File | 3rd_party/protobuf/src/google/protobuf/wire_format.h
|
| Line Count | 414 |
| Domains | Serialization, Wire_Protocol |
| Key Class | WireFormat
|
| Created | 2026-02-10 |
| Knowledge Sources | Repo, Doc |
Overview
Wire format encoding/decoding declarations for the full protobuf runtime. Provides reflection-based serialization and deserialization that operates on Message objects via FieldDescriptors.
Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.
Description
The WireFormat class provides static methods for serializing and deserializing protobuf messages at the wire-format level using the reflection API. Unlike WireFormatLite (which works with known types at compile time), WireFormat operates dynamically via FieldDescriptor and Reflection, making it suitable for:
- Generic serialization -- Serializing any
Messagewithout generated code, by iterating over field descriptors. - Generic parsing -- Parsing wire data into a message by looking up field descriptors from tag numbers.
- Unknown field handling -- Routing unrecognized tags to the message's
UnknownFieldSet. - Size computation -- Computing the serialized byte size of individual fields and complete messages.
- Extension support -- Handling extension fields that are registered in the
ExtensionSet.
This is the "full" wire format layer; the "lite" layer (WireFormatLite) is used by generated code for direct, type-specific operations.
Usage
#include "google/protobuf/wire_format.h"
Used by the protobuf runtime internally for reflection-based serialization. Not typically called directly by application code.
Code Reference
class PROTOBUF_EXPORT WireFormat {
public:
// Serialize a single field via reflection.
static void SerializeFieldWithCachedSizes(
const FieldDescriptor* field,
const Message& message,
io::CodedOutputStream* output);
// Compute serialized size of a single field.
static size_t FieldByteSize(const FieldDescriptor* field,
const Message& message);
// Parse a single field from wire data.
static bool ParseAndMergeField(
uint32_t tag,
const FieldDescriptor* field,
Message* message,
io::CodedInputStream* input);
// Skip an unknown field and optionally store it.
static bool SkipField(io::CodedInputStream* input,
uint32_t tag,
UnknownFieldSet* unknown_fields);
// Compute total serialized size of a message.
static size_t ByteSize(const Message& message);
// Wire type for a given field type.
static WireFormatLite::WireType WireTypeForFieldType(
FieldDescriptor::Type type);
};
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input | CodedInputStream |
Wire-format byte stream for parsing |
| Output | CodedOutputStream |
Wire-format byte stream for serialization |
| Metadata | FieldDescriptor |
Runtime field schema used for dynamic dispatch |
| Fallback | UnknownFieldSet |
Unrecognized fields routed here for preservation |
Usage Examples
// Reflection-based serialization (internal runtime usage)
const Descriptor* desc = msg.GetDescriptor();
const Reflection* refl = msg.GetReflection();
std::vector<const FieldDescriptor*> fields;
refl->ListFields(msg, &fields);
for (const auto* field : fields) {
WireFormat::SerializeFieldWithCachedSizes(field, msg, output);
}