function Message::serializeToJsonStream
@ignore
1 call to Message::serializeToJsonStream()
- Message::serializeToJsonString in vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ Message.php - Serialize the message to json string.
File
-
vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ Message.php, line 1448
Class
- Message
- Parent class of all proto messages. Users should not instantiate this class or extend this class or its child classes by their own. See the comment of specific functions for more details.
Namespace
Google\Protobuf\InternalCode
public function serializeToJsonStream(&$output) {
if (is_a($this, 'Google\\Protobuf\\Any')) {
$output->writeRaw("{", 1);
$type_field = $this->desc
->getFieldByNumber(1);
$value_msg = $this->unpack();
// Serialize type url.
$output->writeRaw("\"@type\":", 8);
$output->writeRaw("\"", 1);
$output->writeRaw($this->getTypeUrl(), strlen($this->getTypeUrl()));
$output->writeRaw("\"", 1);
// Serialize value
if (GPBUtil::hasSpecialJsonMapping($value_msg)) {
$output->writeRaw(",\"value\":", 9);
$value_msg->serializeToJsonStream($output);
}
else {
$value_fields = $value_msg->desc
->getField();
foreach ($value_fields as $field) {
if ($value_msg->existField($field)) {
$output->writeRaw(",", 1);
if (!$value_msg->serializeFieldToJsonStream($output, $field)) {
return false;
}
}
}
}
$output->writeRaw("}", 1);
}
elseif (is_a($this, 'Google\\Protobuf\\FieldMask')) {
$field_mask = GPBUtil::formatFieldMask($this);
$output->writeRaw("\"", 1);
$output->writeRaw($field_mask, strlen($field_mask));
$output->writeRaw("\"", 1);
}
elseif (is_a($this, 'Google\\Protobuf\\Duration')) {
$duration = GPBUtil::formatDuration($this) . "s";
$output->writeRaw("\"", 1);
$output->writeRaw($duration, strlen($duration));
$output->writeRaw("\"", 1);
}
elseif (get_class($this) === 'Google\\Protobuf\\Timestamp') {
$timestamp = GPBUtil::formatTimestamp($this);
$timestamp = json_encode($timestamp);
$output->writeRaw($timestamp, strlen($timestamp));
}
elseif (get_class($this) === 'Google\\Protobuf\\ListValue') {
$field = $this->desc
->getField()[1];
if (!$this->existField($field)) {
$output->writeRaw("[]", 2);
}
else {
if (!$this->serializeFieldToJsonStream($output, $field)) {
return false;
}
}
}
elseif (get_class($this) === 'Google\\Protobuf\\Struct') {
$field = $this->desc
->getField()[1];
if (!$this->existField($field)) {
$output->writeRaw("{}", 2);
}
else {
if (!$this->serializeFieldToJsonStream($output, $field)) {
return false;
}
}
}
else {
if (!GPBUtil::hasSpecialJsonMapping($this)) {
$output->writeRaw("{", 1);
}
$fields = $this->desc
->getField();
$first = true;
foreach ($fields as $field) {
if ($this->existField($field) || GPBUtil::hasJsonValue($this)) {
if ($first) {
$first = false;
}
else {
$output->writeRaw(",", 1);
}
if (!$this->serializeFieldToJsonStream($output, $field)) {
return false;
}
}
}
if (!GPBUtil::hasSpecialJsonMapping($this)) {
$output->writeRaw("}", 1);
}
}
return true;
}