ericvana / protobuf-net

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

Serialization Error #295

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
While serializing, getting this error message "Object reference not set to an 
instance". Following is the Stack Trace Log : 
   at ProtoBuf.Meta.RuntimeTypeModel.GetKey(Type type, Boolean demand, Boolean getBaseKey) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 388
   at ProtoBuf.Meta.ValueMember.TryGetCoreSerializer(RuntimeTypeModel model, DataFormat dataFormat, Type type, WireType& defaultWireType, Boolean asReference, Boolean dynamicType) in C:\Dev\protobuf-net\protobuf-net\Meta\ValueMember.cs:line 440
   at ProtoBuf.Meta.ValueMember.BuildSerializer() in C:\Dev\protobuf-net\protobuf-net\Meta\ValueMember.cs:line 261
   at ProtoBuf.Meta.ValueMember.get_Serializer() in C:\Dev\protobuf-net\protobuf-net\Meta\ValueMember.cs:line 137
   at ProtoBuf.Meta.MetaType.BuildSerializer() in C:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 359
   at ProtoBuf.Meta.MetaType.get_Serializer() in C:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 278
   at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 400
   at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 141
   at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 168
   at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 155
   at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance) in C:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 79
   at Model.Manager.SaveTest(FileStream fileStream) in D:\Test Protobuf\Test\Model\Manager.cs:line 211

Original issue reported on code.google.com by farooqmu...@gmail.com on 5 Jun 2012 at 8:15

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
when i do this by using binary formatter it works fine.

Original comment by farooqmu...@gmail.com on 5 Jun 2012 at 11:17

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
how can I disable a giver property for the serializer?

Original comment by farooqmu...@gmail.com on 6 Jun 2012 at 6:10

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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