Proto3 C# JSON parser throws error for wellknown types

1,364 views
Skip to first unread message

Ajay Bhandari

unread,
Jan 10, 2017, 4:59:51 AM1/10/17
to Protocol Buffers
Hi,
I have simple proto definition like below and through the protoc compiler I am able to use the generated C# message class to covert object to byte array, de-serialize the object from byte array using the message parser etc.

When I try to use the JSON Formatter & parser, I am able to serialize the same to JSON string, but during deserialization the JSON parser throws an error about not able to convert System.UInt32 (.NET) to Google.

Will appreciate any help in resolving this. I am using Google.Protobuf.3.1.0 and Google.Protobuf.Tools.3.1.0 nuget packages

Thanks.

PROTO
=====

syntax = "proto3";
package testpack;
option csharp_namespace = "Test.v1";

// import proto definitions from google protobuf
import "wrappers.proto";
import "timestamp.proto";

message TestMessage {
 google.protobuf.UInt32Value testUintProperty = 1;
 google.protobuf.Int32Value testIntProperty = 2;
 google.protobuf.BoolValue testBoolProperty = 3;
 google.protobuf.Timestamp timestamp = 4;
}

CODE SNIPPTES (C#)
==================

 // this works

     var testMessage = <some method to create TestMessage object using the generated TestMessage class>
     var tmBytes = testMessage.ToByteArray();
     var deserializedTestMessage = TestMessage.Parser.ParseFrom(tmBytes);

 // this works - json below
            JsonFormatter jsf = new JsonFormatter(new JsonFormatter.Settings(true));
            string jsonString = jsf.Format(testMessage);
  
 // this throws error - see exception details below
 var deserializedTestMessageFromJson = JsonParser.Default.Parse<TestMessage>(jsonString);

JSON
====

{ "testUintproperty": 1024, "testIntproperty": 300, "testBoolproperty": true, "timestamp": "2017-01-10T09:46:53.218325200Z" }


SYSTEMEXCEPTION MESSAGE
==========================

Unable to cast object of type 'System.UInt32' to type 'Google.Protobuf.WellKnownTypes.UInt32Value'.

STACK TRACE
===========

 at lambda_method(Closure , IMessage , Object )
   at Google.Protobuf.Reflection.SingleFieldAccessor.SetValue(IMessage message, Object value)
   at Google.Protobuf.JsonParser.MergeField(IMessage message, FieldDescriptor field, JsonTokenizer tokenizer)
   at Google.Protobuf.JsonParser.Merge(IMessage message, JsonTokenizer tokenizer)
   at Google.Protobuf.JsonParser.Merge(IMessage message, TextReader jsonReader)
   at Google.Protobuf.JsonParser.Parse[T](TextReader jsonReader)
   at Google.Protobuf.JsonParser.Parse[T](String json)
   at DeviceSimulator.Program.<SendDeviceToCloudTestMessagesAsync>d__3.MoveNext() ...

Jon Skeet

unread,
Jan 10, 2017, 8:03:48 AM1/10/17
to Protocol Buffers
Thanks for reporting this - I've copied it into a github issue: https://github.com/google/protobuf/issues/2574

I'll look into it as soon as I can. (Although I'm at a conference this week, making it slightly tricky.)
I thought I had tests for all of the well-known types, so it's certainly surprising...

Jon

Jon Skeet

unread,
Jan 10, 2017, 8:51:46 AM1/10/17
to Protocol Buffers
Okay, I've tried it with your message - and it deserializes that JSON just fine for me. I've updated the issue - could you look there and reply to the questions?
(It would be better to keep it on github than on the mailing list.)

Jon

Ajay Bhandari

unread,
Jan 10, 2017, 9:29:20 AM1/10/17
to Protocol Buffers
Thanks Jon. I have replied on the issue.
Reply all
Reply to author
Forward
0 new messages