Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Message.php

function Message::jsonByteSize

@ignore

1 call to Message::jsonByteSize()
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 1944

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\Internal

Code

public function jsonByteSize() {
    $size = 0;
    if (is_a($this, 'Google\\Protobuf\\Any')) {
        // Size for "{}".
        $size += 2;
        // Size for "\"@type\":".
        $size += 8;
        // Size for url. +2 for "" /.
        $size += strlen($this->getTypeUrl()) + 2;
        $value_msg = $this->unpack();
        if (GPBUtil::hasSpecialJsonMapping($value_msg)) {
            // Size for "\",value\":".
            $size += 9;
            $size += $value_msg->jsonByteSize();
        }
        else {
            $value_size = $value_msg->jsonByteSize();
            // size === 2 it's empty message {} which is not serialized inside any
            if ($value_size !== 2) {
                // Size for value. +1 for comma, -2 for "{}".
                $size += $value_size - 1;
            }
        }
    }
    elseif (get_class($this) === 'Google\\Protobuf\\FieldMask') {
        $field_mask = GPBUtil::formatFieldMask($this);
        $size += strlen($field_mask) + 2;
        // 2 for ""
    }
    elseif (get_class($this) === 'Google\\Protobuf\\Duration') {
        $duration = GPBUtil::formatDuration($this) . "s";
        $size += strlen($duration) + 2;
        // 2 for ""
    }
    elseif (get_class($this) === 'Google\\Protobuf\\Timestamp') {
        $timestamp = GPBUtil::formatTimestamp($this);
        $timestamp = json_encode($timestamp);
        $size += strlen($timestamp);
    }
    elseif (get_class($this) === 'Google\\Protobuf\\ListValue') {
        $field = $this->desc
            ->getField()[1];
        if ($this->existField($field)) {
            $field_size = $this->fieldJsonByteSize($field);
            $size += $field_size;
        }
        else {
            // Size for "[]".
            $size += 2;
        }
    }
    elseif (get_class($this) === 'Google\\Protobuf\\Struct') {
        $field = $this->desc
            ->getField()[1];
        if ($this->existField($field)) {
            $field_size = $this->fieldJsonByteSize($field);
            $size += $field_size;
        }
        else {
            // Size for "{}".
            $size += 2;
        }
    }
    else {
        if (!GPBUtil::hasSpecialJsonMapping($this)) {
            // Size for "{}".
            $size += 2;
        }
        $fields = $this->desc
            ->getField();
        $count = 0;
        foreach ($fields as $field) {
            $field_size = $this->fieldJsonByteSize($field);
            $size += $field_size;
            if ($field_size != 0) {
                $count++;
            }
        }
        // size for comma
        $size += $count > 0 ? $count - 1 : 0;
    }
    return $size;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal