anerjan / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

[OnDeserialized] Detect the deserializer used? #235

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm having trouble detecting if the protobuf-net deserializer is at work in my 
'OnDeserialized' method or another (in my case : json deserializer).

Any ways to detect this? I'm using V1.

Original issue reported on code.google.com by tim@mylemans.com on 26 Sep 2011 at 6:21

GoogleCodeExporter commented 9 years ago
If you really want to know that it is protobuf-net, use the specific 
protobuf-net attribute - JSON won't call that.

In v2, there is additional support for passing in a serialization context which 
could be detected, but in *general*, it will be hard to tell between 
serializers. Is there a specific scenario you need this for?

Original comment by marc.gravell on 26 Sep 2011 at 7:46

GoogleCodeExporter commented 9 years ago
Yes, I'm using a custom serialization process making use of a PostSharp aspect, 
the serialization 'event handlers' and 'Extensible' to manually append/get 
fields by 'id'

Usage is something like:

[ProtoTypeMember(1, Scalar = typeof(long), Converter = 
typeof(DateTimeConverter))]
public DateTime UploadedOn { get; set; }

Thing is that the code that uses the 'Extensible' class should only be used by 
the protobuf serializer.

But I found an ugly work around:

if (Environment.StackTrace.Contains(".DataContractJsonSerializer.")) { /* json 
serializer in action */ }

Works, but ugly so a better alternative would be welcome.

Original comment by tim@mylemans.com on 27 Sep 2011 at 10:41