From f651f73a3c3356a93c4e3c131833ed8b5d93e2bc Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 29 Apr 2015 09:18:19 +0100 Subject: [PATCH] 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. --- .../ProtocolBuffers.Test.csproj | 2 - .../SerializableAttribute.cs | 12 -- .../ProtocolBuffers.Test/SerializableTest.cs | 184 ---------------- .../ProtocolBuffers/CustomSerialization.cs | 201 ------------------ .../ProtocolBuffers/ProtocolBuffers.csproj | 1 - .../ProtocolBuffersLite.csproj | 1 - .../ProtocolBuffersLite.Test.csproj | 4 - .../ProtocolBuffersLiteMixed.Test.csproj | 3 - .../SerializableLiteTest.cs | 57 ----- 9 files changed, 465 deletions(-) delete mode 100644 csharp/src/ProtocolBuffers.Test/SerializableAttribute.cs delete mode 100644 csharp/src/ProtocolBuffers.Test/SerializableTest.cs delete mode 100644 csharp/src/ProtocolBuffers/CustomSerialization.cs delete mode 100644 csharp/src/ProtocolBuffersLite.Test/SerializableLiteTest.cs diff --git a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj index b11b1ad8e1..5702c01137 100644 --- a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj +++ b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj @@ -81,7 +81,6 @@ - @@ -112,7 +111,6 @@ - diff --git a/csharp/src/ProtocolBuffers.Test/SerializableAttribute.cs b/csharp/src/ProtocolBuffers.Test/SerializableAttribute.cs deleted file mode 100644 index 0553762bcf..0000000000 --- a/csharp/src/ProtocolBuffers.Test/SerializableAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -#if NOSERIALIZABLE && !COMPACT_FRAMEWORK - -namespace System -{ - [AttributeUsage(AttributeTargets.Class)] - public class SerializableAttribute : Attribute - { - public SerializableAttribute () : base() { } - } -} - -#endif diff --git a/csharp/src/ProtocolBuffers.Test/SerializableTest.cs b/csharp/src/ProtocolBuffers.Test/SerializableTest.cs deleted file mode 100644 index da5b0b5120..0000000000 --- a/csharp/src/ProtocolBuffers.Test/SerializableTest.cs +++ /dev/null @@ -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 - { - /// - /// Just keep it from even compiling if we these objects don't implement the expected interface. - /// - 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 \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/CustomSerialization.cs b/csharp/src/ProtocolBuffers/CustomSerialization.cs deleted file mode 100644 index ae9fca2299..0000000000 --- a/csharp/src/ProtocolBuffers/CustomSerialization.cs +++ /dev/null @@ -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 : 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 : 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 { } - - [Serializable] - partial class ExtendableMessageLite { } - - [Serializable] - partial class AbstractMessage { } - - [Serializable] - partial class GeneratedMessage { } - - [Serializable] - partial class ExtendableMessage { } - - [Serializable] - partial class GeneratedBuilderLite { } - - [Serializable] - partial class ExtendableBuilderLite { } - - [Serializable] - partial class AbstractBuilder { } - - [Serializable] - partial class GeneratedBuilder { } - - [Serializable] - partial class ExtendableBuilder { } - - [Serializable] - partial class DynamicMessage - { - [Serializable] - partial class Builder { } - } -} -#endif \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj index d9e50ec6ff..5f6404a265 100644 --- a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj +++ b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj @@ -61,7 +61,6 @@ - diff --git a/csharp/src/ProtocolBuffers/ProtocolBuffersLite.csproj b/csharp/src/ProtocolBuffers/ProtocolBuffersLite.csproj index fa76514735..fc6e163fd7 100644 --- a/csharp/src/ProtocolBuffers/ProtocolBuffersLite.csproj +++ b/csharp/src/ProtocolBuffers/ProtocolBuffersLite.csproj @@ -61,7 +61,6 @@ - diff --git a/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLite.Test.csproj b/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLite.Test.csproj index f4d5e83e72..7701543cff 100644 --- a/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLite.Test.csproj +++ b/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLite.Test.csproj @@ -72,15 +72,11 @@ Properties\AssemblyInfo.cs - - SerializableAttribute.cs - - diff --git a/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLiteMixed.Test.csproj b/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLiteMixed.Test.csproj index a721d6535b..44b9a290cb 100644 --- a/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLiteMixed.Test.csproj +++ b/csharp/src/ProtocolBuffersLite.Test/ProtocolBuffersLiteMixed.Test.csproj @@ -72,9 +72,6 @@ Properties\AssemblyInfo.cs - - SerializableAttribute.cs - diff --git a/csharp/src/ProtocolBuffersLite.Test/SerializableLiteTest.cs b/csharp/src/ProtocolBuffersLite.Test/SerializableLiteTest.cs deleted file mode 100644 index 992ec7e31f..0000000000 --- a/csharp/src/ProtocolBuffersLite.Test/SerializableLiteTest.cs +++ /dev/null @@ -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 - { - /// - /// Just keep it from even compiling if we these objects don't implement the expected interface. - /// - 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 \ No newline at end of file