Remove support for Serializable.

This could potentially be added back in later, but its use is limited and it's a pain in terms of support in PCL environments.
One use that has been highlighted is passing objects between AppDomains; we'd recommend passing a byte array explicitly and reparsing on the other side.
pull/317/head
Jon Skeet 10 years ago
parent cc058e1118
commit f651f73a3c
  1. 2
      csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj
  2. 12
      csharp/src/ProtocolBuffers.Test/SerializableAttribute.cs
  3. 184
      csharp/src/ProtocolBuffers.Test/SerializableTest.cs
  4. 201
      csharp/src/ProtocolBuffers/CustomSerialization.cs
  5. 1
      csharp/src/ProtocolBuffers/ProtocolBuffers.csproj
  6. 1
      csharp/src/ProtocolBuffers/ProtocolBuffersLite.csproj
  7. 4
      csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLite.Test.csproj
  8. 3
      csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLiteMixed.Test.csproj
  9. 57
      csharp/src/ProtocolBuffersLite.Test/SerializableLiteTest.cs

@ -81,7 +81,6 @@
<Compile Include="Compatibility\TestResources.cs" /> <Compile Include="Compatibility\TestResources.cs" />
<Compile Include="Compatibility\TextCompatibilityTests.cs" /> <Compile Include="Compatibility\TextCompatibilityTests.cs" />
<Compile Include="Compatibility\XmlCompatibilityTests.cs" /> <Compile Include="Compatibility\XmlCompatibilityTests.cs" />
<Compile Include="SerializableAttribute.cs" />
<Compile Include="TestProtos\GoogleSize.cs" /> <Compile Include="TestProtos\GoogleSize.cs" />
<Compile Include="TestProtos\GoogleSpeed.cs" /> <Compile Include="TestProtos\GoogleSpeed.cs" />
<Compile Include="TestProtos\Unittest.cs" /> <Compile Include="TestProtos\Unittest.cs" />
@ -112,7 +111,6 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionTester.cs" /> <Compile Include="ReflectionTester.cs" />
<Compile Include="ReusableBuilderTest.cs" /> <Compile Include="ReusableBuilderTest.cs" />
<Compile Include="SerializableTest.cs" />
<Compile Include="TestCornerCases.cs" /> <Compile Include="TestCornerCases.cs" />
<Compile Include="TestMimeMessageFormats.cs" /> <Compile Include="TestMimeMessageFormats.cs" />
<Compile Include="TestUtil.cs" /> <Compile Include="TestUtil.cs" />

@ -1,12 +0,0 @@
#if NOSERIALIZABLE && !COMPACT_FRAMEWORK
namespace System
{
[AttributeUsage(AttributeTargets.Class)]
public class SerializableAttribute : Attribute
{
public SerializableAttribute () : base() { }
}
}
#endif

@ -1,184 +0,0 @@
#if !NOSERIALIZABLE
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Hosting;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Google.ProtocolBuffers.TestProtos;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Google.ProtocolBuffers
{
[TestClass]
public class SerializableTest
{
/// <summary>
/// Just keep it from even compiling if we these objects don't implement the expected interface.
/// </summary>
public static readonly ISerializable CompileTimeCheckSerializableMessage = TestXmlMessage.DefaultInstance;
public static readonly ISerializable CompileTimeCheckSerializableBuilder = new TestXmlMessage.Builder();
[TestMethod]
[Ignore] // Serialization hasn't been reimplemented yet
public void TestPlainMessage()
{
TestXmlMessage message = TestXmlMessage.CreateBuilder()
.SetValid(true)
.SetText("text")
.AddTextlines("a")
.AddTextlines("b")
.AddTextlines("c")
.SetNumber(0x1010101010)
.AddNumbers(1)
.AddNumbers(2)
.AddNumbers(3)
.SetChild(TestXmlChild.CreateBuilder()
.AddOptions(EnumOptions.ONE)
.SetBinary(ByteString.CopyFrom(new byte[1])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.TWO)
.SetBinary(ByteString.CopyFrom(new byte[2])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.THREE)
.SetBinary(ByteString.CopyFrom(new byte[3])))
.Build();
MemoryStream ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, message);
ms.Position = 0;
TestXmlMessage copy = (TestXmlMessage)new BinaryFormatter().Deserialize(ms);
Assert.AreEqual(message, copy);
}
[TestMethod]
[Ignore] // Serialization hasn't been reimplemented yet
public void TestMessageWithExtensions()
{
TestXmlMessage message = TestXmlMessage.CreateBuilder()
.SetValid(true)
.SetText("text")
.AddTextlines("a")
.AddTextlines("b")
.AddTextlines("c")
.SetNumber(0x1010101010)
.AddNumbers(1)
.AddNumbers(2)
.AddNumbers(3)
.SetChild(TestXmlChild.CreateBuilder()
.AddOptions(EnumOptions.ONE)
.SetBinary(ByteString.CopyFrom(new byte[1])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.TWO)
.SetBinary(ByteString.CopyFrom(new byte[2])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.THREE)
.SetBinary(ByteString.CopyFrom(new byte[3])))
.SetExtension(UnittestExtrasXmltest.ExtensionText, " extension text value ! ")
.SetExtension(UnittestExtrasXmltest.ExtensionMessage, new TestXmlExtension.Builder().SetNumber(42).Build())
.AddExtension(UnittestExtrasXmltest.ExtensionNumber, 100)
.AddExtension(UnittestExtrasXmltest.ExtensionNumber, 101)
.AddExtension(UnittestExtrasXmltest.ExtensionNumber, 102)
.SetExtension(UnittestExtrasXmltest.ExtensionEnum, EnumOptions.ONE)
.Build();
ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
UnittestExtrasXmltest.RegisterAllExtensions(registry);
MemoryStream ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, message);
ms.Position = 0;
//you need to provide the extension registry as context to the serializer
BinaryFormatter bff = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All, registry));
TestXmlMessage copy = (TestXmlMessage)bff.Deserialize(ms);
// And all extensions will be defined.
Assert.AreEqual(message, copy);
}
[TestMethod]
[Ignore] // Serialization hasn't been reimplemented yet
public void TestPlainBuilder()
{
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder()
.SetValid(true)
.SetText("text")
.AddTextlines("a")
.AddTextlines("b")
.AddTextlines("c")
.SetNumber(0x1010101010)
.AddNumbers(1)
.AddNumbers(2)
.AddNumbers(3)
.SetChild(TestXmlChild.CreateBuilder()
.AddOptions(EnumOptions.ONE)
.SetBinary(ByteString.CopyFrom(new byte[1])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.TWO)
.SetBinary(ByteString.CopyFrom(new byte[2])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.THREE)
.SetBinary(ByteString.CopyFrom(new byte[3])))
;
MemoryStream ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, builder);
ms.Position = 0;
TestXmlMessage.Builder copy = (TestXmlMessage.Builder)new BinaryFormatter().Deserialize(ms);
Assert.AreEqual(builder.Build(), copy.Build());
}
[TestMethod]
[Ignore] // Serialization hasn't been reimplemented yet
public void TestBuilderWithExtensions()
{
TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder()
.SetValid(true)
.SetText("text")
.AddTextlines("a")
.AddTextlines("b")
.AddTextlines("c")
.SetNumber(0x1010101010)
.AddNumbers(1)
.AddNumbers(2)
.AddNumbers(3)
.SetChild(TestXmlChild.CreateBuilder()
.AddOptions(EnumOptions.ONE)
.SetBinary(ByteString.CopyFrom(new byte[1])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.TWO)
.SetBinary(ByteString.CopyFrom(new byte[2])))
.AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
.AddOptions(EnumOptions.THREE)
.SetBinary(ByteString.CopyFrom(new byte[3])))
.SetExtension(UnittestExtrasXmltest.ExtensionText, " extension text value ! ")
.SetExtension(UnittestExtrasXmltest.ExtensionMessage, new TestXmlExtension.Builder().SetNumber(42).Build())
.AddExtension(UnittestExtrasXmltest.ExtensionNumber, 100)
.AddExtension(UnittestExtrasXmltest.ExtensionNumber, 101)
.AddExtension(UnittestExtrasXmltest.ExtensionNumber, 102)
.SetExtension(UnittestExtrasXmltest.ExtensionEnum, EnumOptions.ONE)
;
ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
UnittestExtrasXmltest.RegisterAllExtensions(registry);
MemoryStream ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, builder);
ms.Position = 0;
//you need to provide the extension registry as context to the serializer
BinaryFormatter bff = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All, registry));
TestXmlMessage.Builder copy = (TestXmlMessage.Builder)bff.Deserialize(ms);
// And all extensions will be defined.
Assert.AreEqual(builder.Build(), copy.Build());
}
}
}
#endif

@ -1,201 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
/*
* This entire source file is not supported on some platform
*/
#if !NOSERIALIZABLE
using System;
using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Security;
namespace Google.ProtocolBuffers
{
/*
* Specialized handing of *all* message types. Messages are serialized into a byte[] and stored
* into the SerializationInfo, and are then reconstituted by an IObjectReference class after
* deserialization. IDeserializationCallback is supported on both the Builder and Message.
*/
[Serializable]
partial class AbstractMessageLite<TMessage, TBuilder> : ISerializable
{
[SecurityCritical]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.SetType(typeof(SerializationSurrogate));
info.AddValue("message", ToByteArray());
info.AddValue("initialized", IsInitialized);
}
[Serializable]
private sealed class SerializationSurrogate : IObjectReference, ISerializable
{
static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
private readonly byte[] _message;
private readonly bool _initialized;
private SerializationSurrogate(SerializationInfo info, StreamingContext context)
{
_message = (byte[])info.GetValue("message", typeof(byte[]));
_initialized = info.GetBoolean("initialized");
}
[SecurityCritical]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
object IObjectReference.GetRealObject(StreamingContext context)
{
ExtensionRegistry registry = context.Context as ExtensionRegistry;
TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
IDeserializationCallback callback = builder as IDeserializationCallback;
if(callback != null)
{
callback.OnDeserialization(context);
}
TMessage message = _initialized ? builder.Build() : builder.BuildPartial();
callback = message as IDeserializationCallback;
if (callback != null)
{
callback.OnDeserialization(context);
}
return message;
}
[SecurityCritical]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("message", _message);
}
}
}
[Serializable]
partial class AbstractBuilderLite<TMessage, TBuilder> : ISerializable
{
[SecurityCritical]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.SetType(typeof(SerializationSurrogate));
info.AddValue("message", Clone().BuildPartial().ToByteArray());
}
[Serializable]
private sealed class SerializationSurrogate : IObjectReference, ISerializable
{
static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
private readonly byte[] _message;
private SerializationSurrogate(SerializationInfo info, StreamingContext context)
{
_message = (byte[])info.GetValue("message", typeof(byte[]));
}
[SecurityCritical]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
object IObjectReference.GetRealObject(StreamingContext context)
{
ExtensionRegistry registry = context.Context as ExtensionRegistry;
TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
IDeserializationCallback callback = builder as IDeserializationCallback;
if(callback != null)
{
callback.OnDeserialization(context);
}
return builder;
}
[SecurityCritical]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("message", _message);
}
}
}
/*
* Spread some attribute love around, keeping this all here so we don't use conditional compliation
* in every one of these classes. If we introduce a new platform that also does not support this
* we can control it all from this source file.
*/
[Serializable]
partial class GeneratedMessageLite<TMessage, TBuilder> { }
[Serializable]
partial class ExtendableMessageLite<TMessage, TBuilder> { }
[Serializable]
partial class AbstractMessage<TMessage, TBuilder> { }
[Serializable]
partial class GeneratedMessage<TMessage, TBuilder> { }
[Serializable]
partial class ExtendableMessage<TMessage, TBuilder> { }
[Serializable]
partial class GeneratedBuilderLite<TMessage, TBuilder> { }
[Serializable]
partial class ExtendableBuilderLite<TMessage, TBuilder> { }
[Serializable]
partial class AbstractBuilder<TMessage, TBuilder> { }
[Serializable]
partial class GeneratedBuilder<TMessage, TBuilder> { }
[Serializable]
partial class ExtendableBuilder<TMessage, TBuilder> { }
[Serializable]
partial class DynamicMessage
{
[Serializable]
partial class Builder { }
}
}
#endif

@ -61,7 +61,6 @@
<Compile Include="Collections\IPopsicleList.cs" /> <Compile Include="Collections\IPopsicleList.cs" />
<Compile Include="Collections\PopsicleList.cs" /> <Compile Include="Collections\PopsicleList.cs" />
<Compile Include="CodedOutputStream.ComputeSize.cs" /> <Compile Include="CodedOutputStream.ComputeSize.cs" />
<Compile Include="CustomSerialization.cs" />
<Compile Include="Delegates.cs" /> <Compile Include="Delegates.cs" />
<Compile Include="CodedInputStream.cs" /> <Compile Include="CodedInputStream.cs" />
<Compile Include="CodedOutputStream.cs" /> <Compile Include="CodedOutputStream.cs" />

@ -61,7 +61,6 @@
<Compile Include="Collections\Lists.cs" /> <Compile Include="Collections\Lists.cs" />
<Compile Include="Collections\PopsicleList.cs" /> <Compile Include="Collections\PopsicleList.cs" />
<Compile Include="Collections\ReadOnlyDictionary.cs" /> <Compile Include="Collections\ReadOnlyDictionary.cs" />
<Compile Include="CustomSerialization.cs" />
<Compile Include="Descriptors\FieldMappingAttribute.cs" /> <Compile Include="Descriptors\FieldMappingAttribute.cs" />
<Compile Include="Descriptors\FieldType.cs" /> <Compile Include="Descriptors\FieldType.cs" />
<Compile Include="Descriptors\MappedType.cs" /> <Compile Include="Descriptors\MappedType.cs" />

@ -72,15 +72,11 @@
<Compile Include="..\ProtocolBuffers.Test\Properties\AssemblyInfo.cs"> <Compile Include="..\ProtocolBuffers.Test\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link> <Link>Properties\AssemblyInfo.cs</Link>
</Compile> </Compile>
<Compile Include="..\ProtocolBuffers.Test\SerializableAttribute.cs">
<Link>SerializableAttribute.cs</Link>
</Compile>
<Compile Include="AbstractBuilderLiteTest.cs" /> <Compile Include="AbstractBuilderLiteTest.cs" />
<Compile Include="AbstractMessageLiteTest.cs" /> <Compile Include="AbstractMessageLiteTest.cs" />
<Compile Include="ExtendableBuilderLiteTest.cs" /> <Compile Include="ExtendableBuilderLiteTest.cs" />
<Compile Include="ExtendableMessageLiteTest.cs" /> <Compile Include="ExtendableMessageLiteTest.cs" />
<Compile Include="LiteTest.cs" /> <Compile Include="LiteTest.cs" />
<Compile Include="SerializableLiteTest.cs" />
<Compile Include="TestLiteByApi.cs" /> <Compile Include="TestLiteByApi.cs" />
<Compile Include="TestProtos\UnittestExtrasLite.cs" /> <Compile Include="TestProtos\UnittestExtrasLite.cs" />
<Compile Include="TestProtos\UnittestImportLite.cs" /> <Compile Include="TestProtos\UnittestImportLite.cs" />

@ -72,9 +72,6 @@
<Compile Include="..\ProtocolBuffers.Test\Properties\AssemblyInfo.cs"> <Compile Include="..\ProtocolBuffers.Test\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link> <Link>Properties\AssemblyInfo.cs</Link>
</Compile> </Compile>
<Compile Include="..\ProtocolBuffers.Test\SerializableAttribute.cs">
<Link>SerializableAttribute.cs</Link>
</Compile>
<Compile Include="AbstractBuilderLiteTest.cs" /> <Compile Include="AbstractBuilderLiteTest.cs" />
<Compile Include="AbstractMessageLiteTest.cs" /> <Compile Include="AbstractMessageLiteTest.cs" />
<Compile Include="ExtendableBuilderLiteTest.cs" /> <Compile Include="ExtendableBuilderLiteTest.cs" />

@ -1,57 +0,0 @@
using System;
#if !NOSERIALIZABLE
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Google.ProtocolBuffers.TestProtos;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Google.ProtocolBuffers
{
[TestClass]
public class SerializableLiteTest
{
/// <summary>
/// Just keep it from even compiling if we these objects don't implement the expected interface.
/// </summary>
public static readonly ISerializable CompileTimeCheckSerializableMessage = TestRequiredLite.DefaultInstance;
public static readonly ISerializable CompileTimeCheckSerializableBuilder = new TestRequiredLite.Builder();
[TestMethod]
[Ignore] // Serialization hasn't been reimplemented yet
public void TestPlainMessage()
{
TestRequiredLite message = TestRequiredLite.CreateBuilder()
.SetD(42)
.BuildPartial();
MemoryStream ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, message);
ms.Position = 0;
TestRequiredLite copy = (TestRequiredLite)new BinaryFormatter().Deserialize(ms);
Assert.AreEqual(message, copy);
}
[TestMethod]
[Ignore] // Serialization hasn't been reimplemented yet
public void TestPlainBuilder()
{
TestRequiredLite.Builder builder = TestRequiredLite.CreateBuilder()
.SetD(42)
;
MemoryStream ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, builder);
ms.Position = 0;
TestRequiredLite.Builder copy = (TestRequiredLite.Builder)new BinaryFormatter().Deserialize(ms);
Assert.AreEqual(builder.BuildPartial(), copy.BuildPartial());
}
}
}
#endif
Loading…
Cancel
Save