Protobuf: Ruby JSON serialization is incompatible when using well known types

Created on 25 Apr 2018  路  2Comments  路  Source: protocolbuffers/protobuf

Ruby JSON serialization seems to be incompatible when using Well-Known types.

For example, I'm using google.protobuf.Timestamp.
It expects to be mapped RFC3339 string. However, ruby binding encodes it to be object.

Google::Protobuf::Timestamp.new(seconds: Time.current.to_i, nanos: Time.current.nsec).to_json
=> "{\"seconds\":1524627489,\"nanos\":820635000}"

failure case code

sample.proto
syntax = "proto3";
package proto.foo;
import "google/protobuf/timestamp.proto";

@option java_outer_classname = "Protobuf";

message Record {
  google.protobuf.Timestamp created_at = 1;
  google.protobuf.Timestamp updated_at = 2;
}
serialize.rb
Proto::Foo::Record.new(
  created_at: Google::Protobuf::Timestamp(seconds: created_at.to_i, nanos: created_at.nsec),
  updated_at: Google::Protobuf::Timestamp(seconds: updated_at.to_i, nanos: updated_at.nsec)
).to_json
parse.java
Protobuf.Record.Builder builder = Protobuf.Record.newBuilder();
JsonFormat.parser().merge(json, builder);
// => com.google.protobuf.InvalidProtocolBufferException
P2 bug ruby to-be-fixed

Most helpful comment

ruby hasn't passed conformance test yet. This will be fixed.

All 2 comments

ruby hasn't passed conformance test yet. This will be fixed.

Most of well known type issue in json have been fixed.
There are still some edge cases broken. See conformance/failure_list_ruby.txt for details.

Was this page helpful?
0 / 5 - 0 ratings