Protobuf: [PHP] mergeFromJsonString can not unserialize maps with numeric keys

Created on 14 Jun 2018  Â·  4Comments  Â·  Source: protocolbuffers/protobuf

With the following proto:

syntax = "proto3";

package test;

message Hello {
    map<string, string> m = 1;
};

The PHP implementation of protobuf has troubles decoding the following JSON message:

{"m":{"foo":"bar","123":"42"}}

The error is:

PHP Fatal error:  Uncaught Google\Protobuf\Internal\GPBDecodeException: Error occurred during parsing: String field only accepts string value in php/src/Google/Protobuf/Internal/Message.php:836
Stack trace:
#0 php/src/Google/Protobuf/Internal/Message.php(1116): Google\Protobuf\Internal\Message->convertJsonValueToProtoValue(123, Object(Google\Protobuf\Internal\FieldDescriptor), true)
#1 php/src/Google/Protobuf/Internal/Message.php(1086): Google\Protobuf\Internal\Message->mergeFromArrayJsonImpl(Array)
#2 php/src/Google/Protobuf/Internal/Message.php(1168): Google\Protobuf\Internal\Message->mergeFromJsonArray(Array)
#3 php/src/Google/Protobuf/Internal/Message.php(742): Google\Protobuf\Internal\Message->parseFromJsonStream(Object(Google\Protobuf\Internal\RawInputStream))
#4 bug/test.php(17): Google\Protobuf\Internal\Message->mergeFromJsonString('{"m":{"foo":"ba...')
#5 {main}
  thrown in php/src/Google/Protobuf/Internal/Message.php on line 836

The cause is that we decode the JSON as array before parsing it, and that PHP arrays always store numeric keys as integers, and everything else as strings. So, json decoding {"123":"42"} results in [123 => "42"] rather than ["123" => "42"] (which is impossible in PHP).

P2 bug php to-be-fixed

Most helpful comment

I'll fix it.

All 4 comments

Is serialized data created by protobuf? AFAIK, all map keys are converted
to string in json
On Wed, Jun 20, 2018 at 12:23 Feng Xiao notifications@github.com wrote:

Assigned #4765 https://github.com/google/protobuf/issues/4765 to
@TeBoring https://github.com/TeBoring.

—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
https://github.com/google/protobuf/issues/4765#event-1692019400, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AE9H5S30bCcgpQ72PyypMVYGbmkXktPnks5t-qFBgaJpZM4UoLbZ
.

Exactly, however json_decode() casts numeric keys back to int

I'll fix it.

Thanks ! I confirm that the issue is fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xenji picture xenji  Â·  27Comments

blowmage picture blowmage  Â·  26Comments

joshuarubin picture joshuarubin  Â·  69Comments

jtattermusch picture jtattermusch  Â·  83Comments

tang3w picture tang3w  Â·  39Comments