public void BinSerial_TwoIdenticalChildsShouldBeSameInstance() { var parent = new ParentChildTestClass() { Name = "parent", }; var child = new ParentChildTestClass() { Name = "child", Father = parent, Mother = parent, }; Assert.AreSame(child.Father, child.Mother, "Precondition: Saved Father and Mother are same instance"); var stream = new MemoryStream(); var settings = new SharpSerializerBinarySettings(BinarySerializationMode.SizeOptimized); var serializer = new SharpSerializer(settings); serializer.Serialize(child, stream); serializer = new SharpSerializer(settings); stream.Position = 0; ParentChildTestClass loaded = serializer.Deserialize(stream) as ParentChildTestClass; Assert.AreSame(loaded.Father, loaded.Mother, "Loaded Father and Mother are same instance"); }
public void serializeBurstBinary_Click(object sender, EventArgs e) { // create fake obj var obj = RootContainer.CreateFakeRoot(); // create instance of sharpSerializer var settings = new SharpSerializerBinarySettings(BinarySerializationMode.Burst); var serializer = new SharpSerializer(settings); // ************************************************************************************* // For advanced serialization you create SharpSerializer with an overloaded constructor // // SharpSerializerBinarySettings settings = createBinarySettings(); // serializer = new SharpSerializer(settings); // // Scroll the page to the createBinarySettings() method for more details // ************************************************************************************* // set the filename var filename = "sharpSerializerExample.burst"; // serialize SerializationMessage = serialize(obj, serializer, filename); //IKI: iOS UIAlertView ShowMessageAlert(this, null); return; }
private static void TextEncoding(SharpSerializerBinarySettings settings) { // Encoding // Default Encoding is UTF8. // Changing of Encoding has impact on format in which are all strings stored (type names, property names and string values) settings.Encoding = System.Text.Encoding.ASCII; }
public SharpSerializer() // TODO: Is it possible to assigh Type to serializer, so it could speed up? { var settings = new SharpSerializerBinarySettings { Mode = BinarySerializationMode.Burst }; _serializer = new Polenter.Serialization.SharpSerializer(settings); }
/// <summary> /// Returns the fully rehydrated object based upon the given byte array. /// </summary> /// <param name="data">The byte array that represents the object.</param> /// <returns>Fully rehydrated object.</returns> public static object DeserializeFromBinary(byte[] data) { using (var memStream = new MemoryStream(data)) { var settings = new SharpSerializerBinarySettings { IncludeAssemblyVersionInTypeName = true, IncludeCultureInTypeName = true, IncludePublicKeyTokenInTypeName = true }; SharpSerializer ser = new SharpSerializer(settings); return ser.Deserialize(memStream); } }
public void BinSerial_ShouldSerializeGuid() { var parent = new ClassWithGuid() { Guid = Guid.NewGuid(), }; var stream = new MemoryStream(); var settings = new SharpSerializerBinarySettings(BinarySerializationMode.SizeOptimized); var serializer = new SharpSerializer(settings); serializer.Serialize(parent, stream); serializer = new SharpSerializer(settings); stream.Position = 0; ClassWithGuid loaded = serializer.Deserialize(stream) as ClassWithGuid; Assert.AreEqual(parent.Guid, loaded.Guid, "same guid"); }
internal void Load() { try { var collectionName = typeof(Settings).FullName; // _writableSettingsStore.DeleteCollection(collectionName); if (!_writableSettingsStore.CollectionExists(collectionName)) { Save(); } var settings = new SharpSerializerBinarySettings(BinarySerializationMode.SizeOptimized) { IncludeAssemblyVersionInTypeName = false, IncludeCultureInTypeName = false, IncludePublicKeyTokenInTypeName = false }; var serializer = new SharpSerializer(settings); var xml = _writableSettingsStore.GetMemoryStream(collectionName, nameof(Items)); FromSerializable((serializer.Deserialize(xml) as SerializableSettings)); } catch (Exception) { //Debug.Fail(ex.Message); } }
internal void Save() { try { var collectionName = typeof(Settings).FullName; if (!_writableSettingsStore.CollectionExists(collectionName)) { _writableSettingsStore.CreateCollection(collectionName); } var settings = new SharpSerializerBinarySettings(BinarySerializationMode.SizeOptimized) { IncludeAssemblyVersionInTypeName = false, IncludeCultureInTypeName = false, IncludePublicKeyTokenInTypeName = false }; var serializer = new SharpSerializer(settings); using var m = new MemoryStream(); serializer.Serialize(ToSerializable(), m); _writableSettingsStore.SetMemoryStream(collectionName, nameof(Items), m); } catch (Exception) { // } }
/// <summary> /// Returns the byte array representation of the object. /// </summary> /// <param name="entity">The object to serialize.</param> /// <returns>Byte array representation of the object.</returns> public static byte[] SerializeToBinary(object entity) { byte[] result; var settings = new SharpSerializerBinarySettings { IncludeAssemblyVersionInTypeName = true, IncludeCultureInTypeName = true, IncludePublicKeyTokenInTypeName = true }; SharpSerializer ser = new SharpSerializer(settings); using (MemoryStream memStream = new MemoryStream()) { var sw = new BinaryWriter(memStream); ser.Serialize(entity, memStream); sw.Flush(); memStream.Position = 0; var sr = new BinaryReader(memStream); result = sr.ReadBytes((int)memStream.Length); } return result; }
/// <summary> /// Binary serialization with custom settings /// </summary> /// <param name = "settings"></param> public SharpSerializer(SharpSerializerBinarySettings settings) { if (settings == null) { throw new ArgumentNullException("settings"); } initialize(settings); }
private void initialize(SharpSerializerBinarySettings settings) { // PropertiesToIgnore PropertyProvider.PropertiesToIgnore = settings.AdvancedSettings.PropertiesToIgnore; PropertyProvider.AttributesToIgnore = settings.AdvancedSettings.AttributesToIgnore; //RootName RootName = settings.AdvancedSettings.RootName; // TypeNameConverter) var typeNameConverter = settings.AdvancedSettings.TypeNameConverter ?? DefaultInitializer.GetTypeNameConverter(settings.IncludeAssemblyVersionInTypeName, settings.IncludeCultureInTypeName, settings.IncludePublicKeyTokenInTypeName, settings.FindPluginAssembly); // Create Serializer and Deserializer IBinaryReader reader = null; IBinaryWriter writer = null; if (settings.Mode == BinarySerializationMode.Burst) { // Burst mode writer = new BurstBinaryWriter(typeNameConverter, settings.Encoding); reader = new BurstBinaryReader(typeNameConverter, settings.Encoding); } else { // Size optimized mode writer = new SizeOptimizedBinaryWriter(typeNameConverter, settings.Encoding); reader = new SizeOptimizedBinaryReader(typeNameConverter, settings.Encoding); } _deserializer = new BinaryPropertyDeserializer(reader); _serializer = new BinaryPropertySerializer(writer); }
private SharpSerializerBinarySettings createBinarySettings() { // create the settings instance var settings = new SharpSerializerBinarySettings(); // bare instance of SharpSerializerBinarySettings tells SharpSerializer to serialize data into binary format in the SizeOptimized mode // However there is more possibility to influence the serialization TextEncoding(settings); // AssemblyQualifiedName // During serialization all types must be converted to strings. // Since v.2.12 the type is stored as an AssemblyQualifiedName per default. // You can force the SharpSerializer to shorten the type descriptions // by setting the following properties to false // Example of AssemblyQualifiedName: // "System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" // Example of the short type name: // "System.String, mscorlib" settings.IncludeAssemblyVersionInTypeName = true; settings.IncludeCultureInTypeName = true; settings.IncludePublicKeyTokenInTypeName = true; // Mode // The default mode, without altering the settings, is BinarySerializationMode.SizeOptimized // Actually you can choose another mode - BinarySerializationMode.Burst // // What is the difference? // To successfully restore the object tree from the serialized stream, all objects have to be serialized including their type information. // Both modes differ in the art the type information is stored. // // BinarySerializationMode.Burst // In the burst mode, type of every object is serialized as a string as part of this object. // It doesn't matter if all serialized objects are of the same type, their types are serialized as text as many times as many objects. // It increases the file size especially when serializing collections. Type information is duplicated. // It's ok for single, simple objects, as it has small overhead. BurstBinaryWriter supports this mode. // // BinarySerializationMode.SizeOptimized // In the SizeOptimized mode all types are grouped into a list. All type duplicates are removed. // Serialized objects refer only to this list using index of their type. It's recommended approach for serializing of complex // objects with many properties, or many items of the same type (collections). The drawback is - all objects are cached, // then their types are analysed, type list is created, objects are injected with indexes and finally the data is written. // Apart from types, all property names are handled the same way in the SizeOptimized mode. // SizeOptimizedBinaryWriter supports this mode. settings.Mode = BinarySerializationMode.SizeOptimized; // ADVANCED SETTINGS // Most of the classes needed to alter these settings are in the namespace Polenter.Serialization.Advanced // PropertiesToIgnore // Sometimes you want to ignore some properties during the serialization. // If they are parts of your own business objects, you can mark these properties with ExcludeFromSerializationAttribute. // However it is not possible to mark them in the built in .NET classes // In such a case you add these properties to the list PropertiesToIgnore. // I.e. System.Collections.Generic.List<string> has the "Capacity" property which is irrelevant for // the whole Serialization and should be ignored // serializer.PropertyProvider.PropertiesToIgnore.Add(typeof(List<string>), "Capacity") settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(List <string>), "Capacity"); // RootName // There is always a root element during the serialization. Default name of this element is "Root", // but you can change it to any other text. settings.AdvancedSettings.RootName = "MyFunnyClass"; // TypeNameConverter // Since the v.2.12 all types are serialized as AssemblyQualifiedName. // To change this you can alter the settings above (Include...) or create your own instance of ITypeNameConverter. // Important! This property overrides the three properties below/above: // IncludeAssemblyVersionInTypeName, IncludeCultureInTypeName, IncludePublicKeyTokenInTypeName settings.AdvancedSettings.TypeNameConverter = new MyTypeNameConverterWithCompressedTypeNames(); return(settings); }
private SharpSerializerBinarySettings createBinarySettings() { // create the settings instance var settings = new SharpSerializerBinarySettings(); // bare instance of SharpSerializerBinarySettings tells SharpSerializer to serialize data into binary format in the SizeOptimized mode // However there is more possibility to influence the serialization // Encoding // Default Encoding is UTF8. // Changing of Encoding has impact on format in which are all strings stored (type names, property names and string values) settings.Encoding = System.Text.Encoding.ASCII; // AssemblyQualifiedName // During serialization all types must be converted to strings. // Since v.2.12 the type is stored as an AssemblyQualifiedName per default. // You can force the SharpSerializer to shorten the type descriptions // by setting the following properties to false // Example of AssemblyQualifiedName: // "System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" // Example of the short type name: // "System.String, mscorlib" settings.IncludeAssemblyVersionInTypeName = true; settings.IncludeCultureInTypeName = true; settings.IncludePublicKeyTokenInTypeName = true; // Mode // The default mode, without altering the settings, is BinarySerializationMode.SizeOptimized // Actually you can choose another mode - BinarySerializationMode.Burst // // What is the difference? // To successfully restore the object tree from the serialized stream, all objects have to be serialized including their type information. // Both modes differ in the art the type information is stored. // // BinarySerializationMode.Burst // In the burst mode, type of every object is serialized as a string as part of this object. // It doesn't matter if all serialized objects are of the same type, their types are serialized as text as many times as many objects. // It increases the file size especially when serializing collections. Type information is duplicated. // It's ok for single, simple objects, as it has small overhead. BurstBinaryWriter supports this mode. // // BinarySerializationMode.SizeOptimized // In the SizeOptimized mode all types are grouped into a list. All type duplicates are removed. // Serialized objects refer only to this list using index of their type. It's recommended approach for serializing of complex // objects with many properties, or many items of the same type (collections). The drawback is - all objects are cached, // then their types are analysed, type list is created, objects are injected with indexes and finally the data is written. // Apart from types, all property names are handled the same way in the SizeOptimized mode. // SizeOptimizedBinaryWriter supports this mode. settings.Mode = BinarySerializationMode.SizeOptimized; // ADVANCED SETTINGS // Most of the classes needed to alter these settings are in the namespace Polenter.Serialization.Advanced // PropertiesToIgnore // Sometimes you want to ignore some properties during the serialization. // If they are parts of your own business objects, you can mark these properties with ExcludeFromSerializationAttribute. // However it is not possible to mark them in the built in .NET classes // In such a case you add these properties to the list PropertiesToIgnore. // I.e. System.Collections.Generic.List<string> has the "Capacity" property which is irrelevant for // the whole Serialization and should be ignored // serializer.PropertyProvider.PropertiesToIgnore.Add(typeof(List<string>), "Capacity") settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(List<string>), "Capacity"); // RootName // There is always a root element during the serialization. Default name of this element is "Root", // but you can change it to any other text. settings.AdvancedSettings.RootName = "MyFunnyClass"; // TypeNameConverter // Since the v.2.12 all types are serialized as AssemblyQualifiedName. // To change this you can alter the settings above (Include...) or create your own instance of ITypeNameConverter. // Important! This property overrides the three properties below/above: // IncludeAssemblyVersionInTypeName, IncludeCultureInTypeName, IncludePublicKeyTokenInTypeName settings.AdvancedSettings.TypeNameConverter = new MyTypeNameConverterWithCompressedTypeNames(); return settings; }
public void TooShortSizeOptimizedBinaryStreamTest() { var myArray = new[] { "ala", "ma", null, "kota" }; var settings = new SharpSerializerBinarySettings(BinarySerializationMode.Burst); serialize(myArray, new SharpSerializer(settings), shortenData); }
public void CorruptedBurstBinaryStreamTest() { var myArray = new[] { "ala", "ma", null, "kota" }; var settings = new SharpSerializerBinarySettings(BinarySerializationMode.Burst); serialize(myArray, new SharpSerializer(settings), replaceSomeBytesInData); }