Implementation:Alibaba MNN Protobuf Text Format H
Metadata
| Attribute | Value |
|---|---|
| Implementation File | 3rd_party/protobuf/src/google/protobuf/text_format.h
|
| Line Count | 687 |
| Domains | Serialization, Debugging |
| Key Classes | TextFormat, TextFormat::Printer
|
| Created | 2026-02-10 |
| Knowledge Sources | Repo, Doc |
Overview
Human-readable text format serialization for protobuf messages. Provides both printing (message to text) and parsing (text to message) of protobuf data in a readable, editable format.
Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.
Description
The TextFormat class provides static utility methods and nested helper classes for converting protobuf messages to and from a human-readable text representation (sometimes called "text proto" or "pbtxt" format). Key components:
TextFormat::Print-- Converts a message to its text representation, writing to a string or output stream.TextFormat::Parse-- Parses a text-format string into a message object.TextFormat::Printer-- Configurable printer with options for single-line output, custom field value printers, truncation of long strings, and hiding unknown fields.TextFormat::Parser-- Configurable parser with support for custom error collectors, allowing/disallowing unknown fields, and partial message parsing.TextFormat::FieldValuePrinter-- Base class for custom per-field formatting (e.g., printing enums as integers, formatting byte fields as hex).
The text format is widely used for debugging, logging, configuration files (.pbtxt), and test assertions.
Usage
#include "google/protobuf/text_format.h"
Used for debugging output, configuration file parsing, and human-readable serialization.
Code Reference
class PROTOBUF_EXPORT TextFormat {
public:
// Simple static methods.
static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
static bool PrintToString(const Message& message, std::string* output);
static bool Parse(io::ZeroCopyInputStream* input, Message* output);
static bool ParseFromString(const std::string& input, Message* output);
// Configurable printer.
class PROTOBUF_EXPORT Printer {
public:
Printer();
bool Print(const Message& message, io::ZeroCopyOutputStream* output) const;
bool PrintToString(const Message& message, std::string* output) const;
void SetSingleLineMode(bool single_line_mode);
void SetExpandAny(bool expand);
void SetTruncateStringFieldLongerThan(int64_t max_length);
void SetUseShortRepeatedPrimitives(bool use_short);
// Register custom field value printers.
bool RegisterFieldValuePrinter(
const FieldDescriptor* field,
const FieldValuePrinter* printer);
};
// Configurable parser.
class PROTOBUF_EXPORT Parser {
public:
Parser();
bool Parse(io::ZeroCopyInputStream* input, Message* output);
bool ParseFromString(const std::string& input, Message* output);
void AllowUnknownField(bool allow);
void AllowPartialMessage(bool allow);
void RecordErrorsTo(io::ErrorCollector* error_collector);
};
};
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input (Print) | Message |
Populated protobuf message to render as text |
| Output (Print) | std::string / stream |
Human-readable text proto representation |
| Input (Parse) | std::string / stream |
Text proto format string to deserialize |
| Output (Parse) | Message |
Populated message fields from parsed text |
Usage Examples
// Print a message as human-readable text
std::string text;
google::protobuf::TextFormat::PrintToString(my_message, &text);
std::cout << text;
// Parse text format into a message
MyMessage msg;
google::protobuf::TextFormat::ParseFromString(
"name: \"example\"\nid: 42\n", &msg);
// Single-line output for logging
google::protobuf::TextFormat::Printer printer;
printer.SetSingleLineMode(true);
std::string single_line;
printer.PrintToString(msg, &single_line);