[protobuf] Re: Override ToString behavior in ProtoBuf 3 C# implementation
Firstly, I'd encourage you not to do that: a) this opaque binary data goes against the general aims of protos being cross-platform. That may not be a requirement for you at the moment, but I'd urge you to at least bear it in mind b) unless you really need DateTime's somewhat odd semantics (http://blog.nodatime.org/2011/08/what-wrong-with-datetime-anyway.html) I'd encourage you to use the well-known Timestamp type. There are conversions available within that already, and it has a custom format which is basically ISO-8601. As an aside, at some point after Noda Time 2.0 has landed, I expect to create a NodaTime.Protobuf nuget package which adds conversions between Instant and Timestamp, and NodaTime.Duration and Google.Protobuf.WellKnownTypes.Duration too. The nanosecond precision in protobuf was a contributing factor to the decision to use nanosecond precision in Noda Time 2.0. Just something to bear in mind if you were already considering Noda Time. Now, all that aside, I can see cases where it might make sense. I've filed https://github.com/google/protobuf/issues/933 to capture this, partly so that others could add their use cases to it. The proposed way of implementing it would be a somewhat grotty hack though, using a partial method with a ref parameter. Other ideas would be welcome... Jon On Tuesday, 3 November 2015 02:18:27 UTC, Teddy Zhang wrote: > > I need to override the ToString behavior in C# to make it human readable. > > E.g. I defined a message type to represent DateTime in C#, and then write > a partial class to make it be able to convert from/to DateTime. > >> message ProtoDateTime >> >> { >> >> sfixed64 BinaryData = 1; >> >> } >> > > However, the default ToString() doesn't generate human readable contents. > > Currently the default implantation is (codegen code): > > public override string ToString() { > > return pb::JsonFormatter.Default.Format(this); > > } > Which will generate something like this, which is not readable. > { "dateTime": { "binaryData": "5247507155853679530" } } > > Is there a way to override this behavior? > If not, is there a plan to support this? Thanks. > > -- 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
[protobuf] Re: Override ToString behavior in ProtoBuf 3 C# implementation
The C# codegen only works for proto3 anyway. Out of your examples, I'd use Duration and Timestamp - and probably just a string for Guid. That's the one I'd be most convinced by though, as the difference in storage between the text and binary representation is so high. Note that this would only be for diagnostic purposes though - the Json formatter and parser will not use it. (I don't know whether any other platforms support this, mind you...) Jon On 4 Nov 2015 8:19 p.m., "Teddy"wrote: > Thanks for the info on Noda Time. I'll consider using Noda Time when we > move to proto 3. > > The issue here is actually creating custom message for common types like > the DateTime above (which seems to be a poor example). Other common types > we need are guid, timespan, unix time etc. We want to have a user friendly > representation in debugger/logs for easier debugging/troubleshooting. > > On Wed, Nov 4, 2015 at 12:00 PM, Jon Skeet wrote: > >> Hmm... I posted a reply to this yesterday, but apparently it didn't make >> it through. Let's see if this one gets through to the group. >> >> I've raised an issue for it on Github, but don't have any plans to >> support it imminently. >> >> I would strongly urge you away from your current platform-specific >> DateTime representation. If you can, use the Timestamp well-known type, >> which has a custom Json representation. If not, consider another >> platform-neutral representation which is likely to be more readable in a >> string form anyway. (DateTime is somewhat broken as a type anyway. When >> Noda Time 2.0 is out, I intend to create another Nuget package to bridge >> that and Protobuf. The choice of nanosecond precision in Noda Time 2.0 was >> influenced by proto3.) >> >> Jon >> On 4 Nov 2015 7:51 p.m., "Teddy Zhang" wrote: >> >>> +Jon who seems to be the developer on this. >>> >>> On Monday, November 2, 2015 at 6:18:27 PM UTC-8, Teddy Zhang wrote: I need to override the ToString behavior in C# to make it human readable. E.g. I defined a message type to represent DateTime in C#, and then write a partial class to make it be able to convert from/to DateTime. > message ProtoDateTime > > { > > sfixed64 BinaryData = 1; > > } > However, the default ToString() doesn't generate human readable contents. Currently the default implantation is (codegen code): public override string ToString() { return pb::JsonFormatter.Default.Format(this); } Which will generate something like this, which is not readable. { "dateTime": { "binaryData": "5247507155853679530" } } Is there a way to override this behavior? If not, is there a plan to support this? Thanks. > -- 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
[protobuf] Re: Override ToString behavior in ProtoBuf 3 C# implementation
The Github issue is this one, by the way: https://github.com/google/protobuf/issues/933 Note that an alternative for debugging would be to use a DebuggerDisplayAttribute applied on the partial class. Haven't tried this myself, but will look into it. Jon On 4 November 2015 at 20:25, Jon Skeetwrote: > The C# codegen only works for proto3 anyway. > > Out of your examples, I'd use Duration and Timestamp - and probably just a > string for Guid. That's the one I'd be most convinced by though, as the > difference in storage between the text and binary representation is so > high. Note that this would only be for diagnostic purposes though - the > Json formatter and parser will not use it. > > (I don't know whether any other platforms support this, mind you...) > > Jon > On 4 Nov 2015 8:19 p.m., "Teddy" wrote: > >> Thanks for the info on Noda Time. I'll consider using Noda Time when we >> move to proto 3. >> >> The issue here is actually creating custom message for common types like >> the DateTime above (which seems to be a poor example). Other common types >> we need are guid, timespan, unix time etc. We want to have a user friendly >> representation in debugger/logs for easier debugging/troubleshooting. >> >> On Wed, Nov 4, 2015 at 12:00 PM, Jon Skeet wrote: >> >>> Hmm... I posted a reply to this yesterday, but apparently it didn't make >>> it through. Let's see if this one gets through to the group. >>> >>> I've raised an issue for it on Github, but don't have any plans to >>> support it imminently. >>> >>> I would strongly urge you away from your current platform-specific >>> DateTime representation. If you can, use the Timestamp well-known type, >>> which has a custom Json representation. If not, consider another >>> platform-neutral representation which is likely to be more readable in a >>> string form anyway. (DateTime is somewhat broken as a type anyway. When >>> Noda Time 2.0 is out, I intend to create another Nuget package to bridge >>> that and Protobuf. The choice of nanosecond precision in Noda Time 2.0 was >>> influenced by proto3.) >>> >>> Jon >>> On 4 Nov 2015 7:51 p.m., "Teddy Zhang" wrote: >>> +Jon who seems to be the developer on this. On Monday, November 2, 2015 at 6:18:27 PM UTC-8, Teddy Zhang wrote: > > I need to override the ToString behavior in C# to make it human > readable. > > E.g. I defined a message type to represent DateTime in C#, and then > write a partial class to make it be able to convert from/to DateTime. > >> message ProtoDateTime >> >> { >> >> sfixed64 BinaryData = 1; >> >> } >> > > However, the default ToString() doesn't generate human readable > contents. > > Currently the default implantation is (codegen code): > > public override string ToString() { > > return pb::JsonFormatter.Default.Format(this); > > } > Which will generate something like this, which is not readable. > { "dateTime": { "binaryData": "5247507155853679530" } } > > Is there a way to override this behavior? > If not, is there a plan to support this? Thanks. > > >> -- 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
[protobuf] Re: Override ToString behavior in ProtoBuf 3 C# implementation
+Jon who seems to be the developer on this. On Monday, November 2, 2015 at 6:18:27 PM UTC-8, Teddy Zhang wrote: > > I need to override the ToString behavior in C# to make it human readable. > > E.g. I defined a message type to represent DateTime in C#, and then write > a partial class to make it be able to convert from/to DateTime. > >> message ProtoDateTime >> >> { >> >> sfixed64 BinaryData = 1; >> >> } >> > > However, the default ToString() doesn't generate human readable contents. > > Currently the default implantation is (codegen code): > > public override string ToString() { > > return pb::JsonFormatter.Default.Format(this); > > } > Which will generate something like this, which is not readable. > { "dateTime": { "binaryData": "5247507155853679530" } } > > Is there a way to override this behavior? > If not, is there a plan to support this? Thanks. > > -- 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
[protobuf] Re: Override ToString behavior in ProtoBuf 3 C# implementation
Hmm... I posted a reply to this yesterday, but apparently it didn't make it through. Let's see if this one gets through to the group. I've raised an issue for it on Github, but don't have any plans to support it imminently. I would strongly urge you away from your current platform-specific DateTime representation. If you can, use the Timestamp well-known type, which has a custom Json representation. If not, consider another platform-neutral representation which is likely to be more readable in a string form anyway. (DateTime is somewhat broken as a type anyway. When Noda Time 2.0 is out, I intend to create another Nuget package to bridge that and Protobuf. The choice of nanosecond precision in Noda Time 2.0 was influenced by proto3.) Jon On 4 Nov 2015 7:51 p.m., "Teddy Zhang"wrote: > +Jon who seems to be the developer on this. > > On Monday, November 2, 2015 at 6:18:27 PM UTC-8, Teddy Zhang wrote: >> >> I need to override the ToString behavior in C# to make it human readable. >> >> E.g. I defined a message type to represent DateTime in C#, and then write >> a partial class to make it be able to convert from/to DateTime. >> >>> message ProtoDateTime >>> >>> { >>> >>> sfixed64 BinaryData = 1; >>> >>> } >>> >> >> However, the default ToString() doesn't generate human readable contents. >> >> Currently the default implantation is (codegen code): >> >> public override string ToString() { >> >> return pb::JsonFormatter.Default.Format(this); >> >> } >> Which will generate something like this, which is not readable. >> { "dateTime": { "binaryData": "5247507155853679530" } } >> >> Is there a way to override this behavior? >> If not, is there a plan to support this? Thanks. >> >> -- 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
[protobuf] Re: Override ToString behavior in ProtoBuf 3 C# implementation
Thanks for the info on Noda Time. I'll consider using Noda Time when we move to proto 3. The issue here is actually creating custom message for common types like the DateTime above (which seems to be a poor example). Other common types we need are guid, timespan, unix time etc. We want to have a user friendly representation in debugger/logs for easier debugging/troubleshooting. On Wed, Nov 4, 2015 at 12:00 PM, Jon Skeetwrote: > Hmm... I posted a reply to this yesterday, but apparently it didn't make > it through. Let's see if this one gets through to the group. > > I've raised an issue for it on Github, but don't have any plans to support > it imminently. > > I would strongly urge you away from your current platform-specific > DateTime representation. If you can, use the Timestamp well-known type, > which has a custom Json representation. If not, consider another > platform-neutral representation which is likely to be more readable in a > string form anyway. (DateTime is somewhat broken as a type anyway. When > Noda Time 2.0 is out, I intend to create another Nuget package to bridge > that and Protobuf. The choice of nanosecond precision in Noda Time 2.0 was > influenced by proto3.) > > Jon > On 4 Nov 2015 7:51 p.m., "Teddy Zhang" wrote: > >> +Jon who seems to be the developer on this. >> >> On Monday, November 2, 2015 at 6:18:27 PM UTC-8, Teddy Zhang wrote: >>> >>> I need to override the ToString behavior in C# to make it human readable. >>> >>> E.g. I defined a message type to represent DateTime in C#, and then >>> write a partial class to make it be able to convert from/to DateTime. >>> message ProtoDateTime { sfixed64 BinaryData = 1; } >>> >>> However, the default ToString() doesn't generate human readable contents. >>> >>> Currently the default implantation is (codegen code): >>> >>> public override string ToString() { >>> >>> return pb::JsonFormatter.Default.Format(this); >>> >>> } >>> Which will generate something like this, which is not readable. >>> { "dateTime": { "binaryData": "5247507155853679530" } } >>> >>> Is there a way to override this behavior? >>> If not, is there a plan to support this? Thanks. >>> >>> -- 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.