Open GoogleCodeExporter opened 9 years ago
Is there any repro that goes along with that? the stacktrace might help, but
something I can use to put a test together to validate the error and the fix
would be better
Original comment by marc.gravell
on 5 Jun 2012 at 8:34
Following is the sample code of Asset and Plant:
//Asset Class
[ProtoContract(SkipConstructor = true)]
[ProtoInclude(500, typeof(Plant))]
public class Asset : ISerializable, TreeList.IVirtualTreeListData, IDisposable,
ICloneable
{
//Constructor
[ProtoMember(1, DynamicType = true)]
public List<Asset> AllAssets
{
get
{
//Code
}
}
[ProtoMember(2, DynamicType = true)]
public List<Asset> AssetHierarcy
{
get
{
//Code
}
}
[ProtoMember(3, DynamicType = true)]
public List<Asset> ChildAssets { get; private set; }
}
//Plant Class
[ProtoContract(SkipConstructor = true)]
public class Plant : Asset
{
//Costructor
[ProtoMember (101, DynamicType = true)]
public Parameter Altitude { get; set; }
[ProtoMember(105, DynamicType = true)]
public Asset Blowers
{
get
{
//Code
return null;
}
}
}
public static void SaveTest(FileStream fileStream)
{
fileStream.Position = 0;
var bf = new BinaryFormatter();
try
{
var outputStream = new ZipOutputStream(fileStream);
outputStream.SetLevel(5);
outputStream.Password = null;
ZipEntry oZipEntry;
oZipEntry = new ZipEntry(fileStream.Name);
outputStream.PutNextEntry(oZipEntry);
//bf.Serialize(outputStream, instance);
Serializer.Serialize(outputStream, instance); //Error Here
outputStream.Finish();
outputStream.Close();
}
catch (Exception e)
{
MessageBox.Show("Comprassion Error " + e.Message);
fileStream.Close();
}
}
Original comment by farooqmu...@gmail.com
on 5 Jun 2012 at 11:16
when i do this by using binary formatter it works fine.
Original comment by farooqmu...@gmail.com
on 5 Jun 2012 at 11:17
That code is not sufficient to reproduce anything - not least, it doesn't
compile (many things missing). Have you got something that *actually shows* the
problem, so that I can investigate? For example, the following works fine:
[ProtoContract(SkipConstructor = true)]
[ProtoInclude(500, typeof(Plant))]
public class Asset
{
public Asset()
{
AllAssets = new List<Asset>();
ChildAssets = new List<Asset>();
}
[ProtoMember(1)]
public List<Asset> AllAssets { get; private set; }
[ProtoMember(2)]
public List<Asset> AssetHierarcy { get; private set; }
[ProtoMember(3)]
public List<Asset> ChildAssets { get; private set; }
}
[ProtoContract(SkipConstructor = true)]
public class Plant : Asset
{
[ProtoMember(105)]
public Asset Blowers { get; set; }
}
[Test]
public void Execute()
{
Asset asset = new Plant {Blowers = new Asset(), ChildAssets = {new Plant()}};
var clone = Serializer.DeepClone(asset);
}
Original comment by marc.gravell
on 5 Jun 2012 at 11:34
At my side, there is one property which is creating null exception but the same
code is serialized by using binary formatter without any error. binary
formatter automatically handling null exception but protobuf is generating
error message for null exception.
Original comment by farooqmu...@gmail.com
on 5 Jun 2012 at 12:10
BinaryFormatter doesn't work on properties; at no point does it invoke
properties - it works at a different level. So: if it is your property that is
raising an exception: *don't do that*. protobuf-net does support various
mechanisms for conditional serialization, so it is entirely possible to disable
(for want of a better term) a given property for the serializer. But again: to
properly advise, I need to see the actual problem.
Original comment by marc.gravell
on 5 Jun 2012 at 12:14
how can I disable a giver property for the serializer?
Original comment by farooqmu...@gmail.com
on 6 Jun 2012 at 6:10
Also, how can we enable null value support in proto buf? like I have a property
[ProtoMember (101)]
public Parameter Al { get; set; }
How can I enable null value support for this property?
Original comment by farooqmu...@gmail.com
on 6 Jun 2012 at 7:27
I assume you mean *conditionally* ? if you don't want the property at all, just
don't mark it for serialization. But conditionally: there are existing BCL
approaches like the ShouldSerialize*() pattern:
[ProtoMember(1)]
public int Foo {get;set;}
public bool ShouldSerializeFoo() { ... }
this existing Microsoft pattern has you return true if you want it to
serialize, false otherwise.
For null support, note that protobuf (the underlying Google spec) *has no
concept of null*. There are ways of getting around this, but it depends a lot
on context.
Original comment by marc.gravell
on 6 Jun 2012 at 9:21
there are few properties which have null values at the time of serialization. I
think these null values are producing null value exception error. Because
Protobuf doesn't handle null value automatically. Am I right? If yes then
kindly suggest how can I cope with this null value exception? What are ways of
getting rid of this? Is there any option which I can use at the time of
declaring ProtoMember or there is some other method to deal with it?
Thanks,
Original comment by farooqmu...@gmail.com
on 6 Jun 2012 at 10:19
If your property returns null, then that isn't a problem at all - protobuf-net
will simply assume that refers to a default state, and therefore it doesn't
need to be serialized. It will skip it and move on. It will not raise an
exception due to properties returning null.
However - if your property **raises an exception**, then that is a problem.
Again, running theme in all of this: if you can produce a runnable sample that
*actually shows* the problem you are having, it would really help.
Original comment by marc.gravell
on 6 Jun 2012 at 10:24
Original issue reported on code.google.com by
farooqmu...@gmail.com
on 5 Jun 2012 at 8:15