Okay, there are two pieces of good news:
- I've managed to reproduce the issue
- It's easy for you to fix by changing your proto and how you build
Basically, you're not importing the wrapper types as in the way protoc
expects.
Instead of importing "wrappers.proto" and using -I on the command line to
point to google/protobuf, you should import
"google/protobuf/wrappers.proto" and use -I to point to the directory above
google (in the protobuf tools package, which is where I assuming you're
finding wrappers.proto) You can then refer to just "Int32Value" instead of
".google.protobuf.Int32Value", although the latter still works too.
At that point, your NullableInt property should be generated as a .NET
Nullable<int> rather than as an Int32Value, so you need to change your code
to:
var foo = new Foo { NullableInt = 5 }
It should be much easier to work with at that point, aside from anything
else.
It turns out you're not the first person to run into this:
- https://github.com/google/protobuf/issues/2574
- https://github.com/google/protobuf/issues/2575
... but it's still not clear to me whether we *should* try to fully support
the way you were generating the code before; it's entirely possible that
other parts of protobuf infrastructure will be confused by this too. I can
look at fixing this particular part of the C# codegen, but I wouldn't like
to say it'll be plain sailing for everything else.
Jon
On 24 October 2017 at 21:56, Adam Cozzette <[email protected]> wrote:
> +Jon Skeet, who is the expert on C# protos
>
> On Mon, Oct 16, 2017 at 7:41 AM, <[email protected]> wrote:
>
>> Using Google.Protobuf Nuget package version 3.4.1
>>
>> In its simplest form, I have a Google protocol-buffers message defined
>> like this:
>>
>> syntax = "proto3";package tests;
>> option csharp_namespace = "MyTests";
>> import "wrappers.proto";
>>
>> message Foo {
>> .google.protobuf.Int32Value NullableInt = 1;}
>>
>> I compile it to C# code using Google's protoc tool from
>> Google.Protobuf.Tools version 3.4.0.
>>
>> When I instantiate an instance of Foo and serialize it to a string using
>> the Google.Protobuf.JsonFormatter I get a sensible result, which is "{
>> "NullableInt": 5 }". When I de-serialize this back into a Foo instance
>> using Google.Protobuf.JsonParser, the parser throws a
>> System.InvalidCastException stating "Unable to cast object of type
>> 'System.Int32' to type 'Google.Protobuf.WellKnownTypes.Int32Value'."
>>
>> Why is this exception being thrown? Am I doing something stupid?
>>
>> Here is the code that I am running:
>>
>> [Test]public void TestRoundTripInt32Value(){
>> var foramtter = new JsonFormatter(new JsonFormatter.Settings(false));
>> var foo = new Foo { NullableInt = new Int32Value {Value = 5} };
>> var str = foramtter.Format(foo);
>> Console.WriteLine(str);
>> var parser = new JsonParser(new JsonParser.Settings(100));
>> var foo2 = parser.Parse<Foo>(str); // <= Throws!
>> Assert.That(foo2, Is.EqualTo(foo));}
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Protocol Buffers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at https://groups.google.com/group/protobuf.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.