|
@@ -773,10 +773,10 @@ class Message
|
|
|
* @return null.
|
|
|
* @throws \Exception Invalid data.
|
|
|
*/
|
|
|
- public function mergeFromJsonString($data)
|
|
|
+ public function mergeFromJsonString($data, $ignore_unknown = false)
|
|
|
{
|
|
|
$input = new RawInputStream($data);
|
|
|
- $this->parseFromJsonStream($input);
|
|
|
+ $this->parseFromJsonStream($input, $ignore_unknown);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -801,6 +801,7 @@ class Message
|
|
|
private function convertJsonValueToProtoValue(
|
|
|
$value,
|
|
|
$field,
|
|
|
+ $ignore_unknown,
|
|
|
$is_map_key = false)
|
|
|
{
|
|
|
switch ($field->getType()) {
|
|
@@ -849,7 +850,7 @@ class Message
|
|
|
} elseif (!is_object($value) && !is_array($value)) {
|
|
|
throw new GPBDecodeException("Expect message.");
|
|
|
}
|
|
|
- $submsg->mergeFromJsonArray($value);
|
|
|
+ $submsg->mergeFromJsonArray($value, $ignore_unknown);
|
|
|
}
|
|
|
return $submsg;
|
|
|
case GPBType::ENUM:
|
|
@@ -862,9 +863,12 @@ class Message
|
|
|
$enum_value = $field->getEnumType()->getValueByName($value);
|
|
|
if (!is_null($enum_value)) {
|
|
|
return $enum_value->getNumber();
|
|
|
+ } else if ($ignore_unknown) {
|
|
|
+ return $this->defaultValue($field);
|
|
|
+ } else {
|
|
|
+ throw new GPBDecodeException(
|
|
|
+ "Enum field only accepts integer or enum value name");
|
|
|
}
|
|
|
- throw new GPBDecodeException(
|
|
|
- "Enum field only accepts integer or enum value name");
|
|
|
case GPBType::STRING:
|
|
|
if (is_null($value)) {
|
|
|
return $this->defaultValue($field);
|
|
@@ -1125,17 +1129,17 @@ class Message
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected function mergeFromJsonArray($array)
|
|
|
+ protected function mergeFromJsonArray($array, $ignore_unknown)
|
|
|
{
|
|
|
if (is_a($this, "Google\Protobuf\Any")) {
|
|
|
$this->clear();
|
|
|
$this->setTypeUrl($array["@type"]);
|
|
|
$msg = $this->unpack();
|
|
|
if (GPBUtil::hasSpecialJsonMapping($msg)) {
|
|
|
- $msg->mergeFromJsonArray($array["value"]);
|
|
|
+ $msg->mergeFromJsonArray($array["value"], $ignore_unknown);
|
|
|
} else {
|
|
|
unset($array["@type"]);
|
|
|
- $msg->mergeFromJsonArray($array);
|
|
|
+ $msg->mergeFromJsonArray($array, $ignore_unknown);
|
|
|
}
|
|
|
$this->setValue($msg->serializeToString());
|
|
|
return;
|
|
@@ -1171,7 +1175,7 @@ class Message
|
|
|
$fields = $this->getFields();
|
|
|
foreach($array as $key => $value) {
|
|
|
$v = new Value();
|
|
|
- $v->mergeFromJsonArray($value);
|
|
|
+ $v->mergeFromJsonArray($value, $ignore_unknown);
|
|
|
$fields[$key] = $v;
|
|
|
}
|
|
|
}
|
|
@@ -1194,7 +1198,7 @@ class Message
|
|
|
}
|
|
|
foreach ($array as $key => $v) {
|
|
|
$value = new Value();
|
|
|
- $value->mergeFromJsonArray($v);
|
|
|
+ $value->mergeFromJsonArray($v, $ignore_unknown);
|
|
|
$values = $struct_value->getFields();
|
|
|
$values[$key]= $value;
|
|
|
}
|
|
@@ -1207,7 +1211,7 @@ class Message
|
|
|
}
|
|
|
foreach ($array as $v) {
|
|
|
$value = new Value();
|
|
|
- $value->mergeFromJsonArray($v);
|
|
|
+ $value->mergeFromJsonArray($v, $ignore_unknown);
|
|
|
$values = $list_value->getValues();
|
|
|
$values[]= $value;
|
|
|
}
|
|
@@ -1217,10 +1221,10 @@ class Message
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
- $this->mergeFromArrayJsonImpl($array);
|
|
|
+ $this->mergeFromArrayJsonImpl($array, $ignore_unknown);
|
|
|
}
|
|
|
|
|
|
- private function mergeFromArrayJsonImpl($array)
|
|
|
+ private function mergeFromArrayJsonImpl($array, $ignore_unknown)
|
|
|
{
|
|
|
foreach ($array as $key => $value) {
|
|
|
$field = $this->desc->getFieldByJsonName($key);
|
|
@@ -1244,10 +1248,12 @@ class Message
|
|
|
$proto_key = $this->convertJsonValueToProtoValue(
|
|
|
$tmp_key,
|
|
|
$key_field,
|
|
|
+ $ignore_unknown,
|
|
|
true);
|
|
|
$proto_value = $this->convertJsonValueToProtoValue(
|
|
|
$tmp_value,
|
|
|
- $value_field);
|
|
|
+ $value_field,
|
|
|
+ $ignore_unknown);
|
|
|
self::kvUpdateHelper($field, $proto_key, $proto_value);
|
|
|
}
|
|
|
} else if ($field->isRepeated()) {
|
|
@@ -1261,14 +1267,16 @@ class Message
|
|
|
}
|
|
|
$proto_value = $this->convertJsonValueToProtoValue(
|
|
|
$tmp,
|
|
|
- $field);
|
|
|
+ $field,
|
|
|
+ $ignore_unknown);
|
|
|
self::appendHelper($field, $proto_value);
|
|
|
}
|
|
|
} else {
|
|
|
$setter = $field->getSetter();
|
|
|
$proto_value = $this->convertJsonValueToProtoValue(
|
|
|
$value,
|
|
|
- $field);
|
|
|
+ $field,
|
|
|
+ $ignore_unknown);
|
|
|
if ($field->getType() === GPBType::MESSAGE) {
|
|
|
if (is_null($proto_value)) {
|
|
|
continue;
|
|
@@ -1288,7 +1296,7 @@ class Message
|
|
|
/**
|
|
|
* @ignore
|
|
|
*/
|
|
|
- public function parseFromJsonStream($input)
|
|
|
+ public function parseFromJsonStream($input, $ignore_unknown)
|
|
|
{
|
|
|
$array = json_decode($input->getData(), true, 512, JSON_BIGINT_AS_STRING);
|
|
|
if ($this instanceof \Google\Protobuf\ListValue) {
|
|
@@ -1304,7 +1312,7 @@ class Message
|
|
|
}
|
|
|
}
|
|
|
try {
|
|
|
- $this->mergeFromJsonArray($array);
|
|
|
+ $this->mergeFromJsonArray($array, $ignore_unknown);
|
|
|
} catch (\Exception $e) {
|
|
|
throw new GPBDecodeException($e->getMessage());
|
|
|
}
|