Skip to content

Commit

Permalink
3. Data Streams : Serialization #117
Browse files Browse the repository at this point in the history
- 3. Data Streams : Serialization #117

Run code analysis to improve the code #124

- fixed all problems. Some have been added to suppression files.
  • Loading branch information
mpostol committed Jul 31, 2018
1 parent f7740d2 commit 19e0b55
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 327 deletions.
5 changes: 1 addition & 4 deletions Lecture/P01.Introduction/Introduction/LanguageCSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ class LanguageCSharp : Language, IInterface //inheritance
{

#region constructor
public LanguageCSharp()
{
LanguageMethod();
}
public LanguageCSharp() { }
#endregion

#region IInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="Generics\Node.cs" />
<Compile Include="Generics\NodeEnumerable.cs" />
<Compile Include="Generics\SelfDictionary.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TypeConcept\BasicTypesStruct.cs" />
<Compile Include="TypeConcept\InterfaceExample.cs" />
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Instrumentation\CustomData\Catalog.xsd.cs">
<DependentUpon>Catalog.xsd</DependentUpon>
</Compile>
<Compile Include="Instrumentation\SelfControlSerialization.cs" />
<Compile Include="SerializationUnitTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FileStreamUnitTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP
//____________________________________________________________________________

using TP.Lecture.Serialization;

using System.Diagnostics;
using TP.DataStreams.Serialization;

namespace Example.Xml.CustomData
{
/// <summary>
/// class catalog
/// </summary>
public partial class Catalog: IStylesheetNameProvider
public partial class Catalog : IStylesheetNameProvider
{
#region IStylesheetNameProvider Members
/// <summary>
Expand All @@ -23,5 +25,28 @@ public string StylesheetName
get { return "catalog.xslt"; }
}
#endregion
[Conditional("DEBUG")]
internal void AddTestingData()
{
CDDescription _cd1 = new CDDescription()
{
artist = "Bob Dylan",
title = "Empire Burlesque",
country = "USA",
company = "Columbia",
price = 10.90M,
year = 1985,
};
CDDescription _cd2 = new CDDescription
{
title = "Hide your heart",
artist = "Bonnie Tyler",
country = "UK",
company = "CBS Records",
price = 9.90M,
year = 1988
};
cd = new CDDescription[] { _cd1, _cd2 };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP
//____________________________________________________________________________

using System;
using System.Runtime.Serialization;

namespace TP.DataStreams.Instrumentation
Expand All @@ -14,6 +15,7 @@ namespace TP.DataStreams.Instrumentation
/// Class CustomSerialization - Demonstrates custom serialization approach
/// </summary>
/// <seealso cref="System.Runtime.Serialization.ISerializable" />
[Serializable]
public class SelfControlSerialization : ISerializable
{

Expand Down Expand Up @@ -47,13 +49,13 @@ public double AverageIncome
#endregion

#region ISerializable
public SelfControlSerialization(SerializationInfo info, StreamingContext context)
protected SelfControlSerialization(SerializationInfo info, StreamingContext context)
{
// Reset the property value using the GetValue method.
MaxIncome = info.GetDouble("MaxIncome");
MinIncome = info.GetDouble("MinIncome");
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
//// Use the AddValue method to specify serialized values (state of the object).
info.AddValue("MaxIncome", MaxIncome);
Expand Down
164 changes: 13 additions & 151 deletions Lecture/P03.DataStreams/DataStreams.UnitTest/SerializationUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@

using Example.Xml.CustomData;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using TP.DataStreams.Instrumentation;
using TP.DataStreams.Serialization;
using TP.Lecture.Serialization;

namespace TP.DataStreams
{
Expand All @@ -27,7 +22,7 @@ public class SerializationUnitTest
[TestMethod]
public void SerializeTestMethod()
{
ISerializable _objectToSerialize = new CustomSerialization(987654321.123, 123456789.987);
ISerializable _objectToSerialize = new SelfControlSerialization(987654321.123, 123456789.987);
CustomFormatter _formatter = new CustomFormatter();
const string _fileName = "test.xml";
File.Delete(_fileName);
Expand All @@ -39,155 +34,22 @@ public void SerializeTestMethod()
string _fileContent = File.ReadAllText(_fileName, Encoding.UTF8);
Debug.Write(_fileContent);
}
class CustomFormatter : Formatter
{
public override ISurrogateSelector SurrogateSelector { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override SerializationBinder Binder { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override StreamingContext Context { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public override object Deserialize(Stream serializationStream)
{
throw new NotImplementedException();
}

public override void Serialize(Stream serializationStream, object graph)
{

ISerializable _data = (ISerializable)graph;
SerializationInfo _info = new SerializationInfo(graph.GetType(), new FormatterConverter());
StreamingContext _context = new StreamingContext(StreamingContextStates.File);
_data.GetObjectData(_info, _context);
foreach (SerializationEntry _item in _info)
this.WriteMember(_item.Name, _item.Value);
XmlWriter _writer = XmlWriter.Create(serializationStream);
XDocument m_xmlDocument = new XDocument(new XElement( "SerializationTest", _values));
m_xmlDocument.Save(_writer);
_writer.Flush();
}
List<XElement> _values = new List<XElement>();
protected override void WriteArray(object obj, string name, Type memberType)
{
throw new NotImplementedException();
}

protected override void WriteBoolean(bool val, string name)
{
throw new NotImplementedException();
}

protected override void WriteByte(byte val, string name)
{
throw new NotImplementedException();
}

protected override void WriteChar(char val, string name)
{
throw new NotImplementedException();
}

protected override void WriteDateTime(DateTime val, string name)
{
throw new NotImplementedException();
}

protected override void WriteDecimal(decimal val, string name)
{
throw new NotImplementedException();
}

protected override void WriteDouble(double val, string name)
{
_values.Add( new XElement(name, val));
}

protected override void WriteInt16(short val, string name)
{
throw new NotImplementedException();
}

protected override void WriteInt32(int val, string name)
{
throw new NotImplementedException();
}

protected override void WriteInt64(long val, string name)
{
throw new NotImplementedException();
}

protected override void WriteObjectRef(object obj, string name, Type memberType)
{
throw new NotImplementedException();
}

protected override void WriteSByte(sbyte val, string name)
{
throw new NotImplementedException();
}

protected override void WriteSingle(float val, string name)
{
throw new NotImplementedException();
}

protected override void WriteTimeSpan(TimeSpan val, string name)
{
throw new NotImplementedException();
}

protected override void WriteUInt16(ushort val, string name)
{
throw new NotImplementedException();
}

protected override void WriteUInt32(uint val, string name)
{
throw new NotImplementedException();
}

protected override void WriteUInt64(ulong val, string name)
{
throw new NotImplementedException();
}

protected override void WriteValueType(object obj, string name, Type memberType)
{
throw new NotImplementedException();
}
}
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/e6d687d7-e0e8-4f46-ad88-fe764cd18c3b/implemant-iformatter-interface?forum=csharpgeneral
//https://stackoverflow.com/questions/12566946/serialize-a-generic-object-to-ini-text-file-implementing-a-custom-iformatter
[TestMethod]
public void ReadWRiteTest()
{
CDDescription _cd1 = new CDDescription()
{
artist = "Bob Dylan",
title = "Empire Burlesque",
country = "USA",
company = "Columbia",
price = 10.90M,
year = 1985,
};
CDDescription _cd2 = new CDDescription
{
title = "Hide your heart",
artist = "Bonnie Tyler",
country = "UK",
company = "CBS Records",
price = 9.90M,
year = 1988
};
Assert.IsFalse(_cd1.Equals(_cd2));
Catalog _catalog = new Catalog
{
cd = new CDDescription[] { _cd1, _cd2 }
};

#if !DEBUG
Assert.Inconclusive("The test can be executed only in debug configuration");
#endif
Catalog _catalog2Write = new Catalog();
_catalog2Write.AddTestingData();
Assert.IsNotNull(_catalog2Write.cd);
string _fileName = @"Instrumentation\CustomData\catalog.xml";
XmlFile.WriteXmlFile<Catalog>(_catalog, _fileName, System.IO.FileMode.Create);
Catalog _newPerson = XmlFile.ReadXmlFile<Catalog>(_fileName);
Assert.IsTrue(_catalog.cd[0].Equals(_newPerson.cd[0]));
Assert.IsTrue(_catalog.cd[1].Equals(_newPerson.cd[1]));
XmlFile.WriteXmlFile<Catalog>(_catalog2Write, _fileName, FileMode.Create);
Catalog _recoveredCatalog = XmlFile.ReadXmlFile<Catalog>(_fileName);
Assert.IsTrue(_catalog2Write.cd[0].Equals(_recoveredCatalog.cd[0]));
Assert.IsTrue(_catalog2Write.cd[1].Equals(_recoveredCatalog.cd[1]));

}

Expand Down
3 changes: 2 additions & 1 deletion Lecture/P03.DataStreams/DataStreams/DataStreams.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="FileStream\FileExample.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reflection\AttributedClass.cs" />
<Compile Include="Reflection\AttachedProperty.cs" />
<Compile Include="Serialization\CustomSerialization.cs" />
<Compile Include="Serialization\CustomFormatter.cs" />
<Compile Include="Serialization\IStylesheetNameProvider.cs" />
<Compile Include="Serialization\XmlFile.cs" />
</ItemGroup>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

namespace TP.Lecture.Serialization
//____________________________________________________________________________
//
// Copyright (C) 2018, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP
//____________________________________________________________________________


namespace TP.DataStreams.Serialization
{

/// <summary>
Expand Down
31 changes: 19 additions & 12 deletions Lecture/P03.DataStreams/DataStreams/Serialization/XmlFile.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using System;
//____________________________________________________________________________
//
// Copyright (C) 2018, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP
//____________________________________________________________________________

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace TP.Lecture.Serialization
namespace TP.DataStreams.Serialization
{
/// <summary>
/// Provides static methods for serialization objects into XML documents and writing the XML document to a file.
Expand All @@ -30,23 +37,23 @@ public static class XmlFile
public static void WriteXmlFile<type>( type dataObject, string path, FileMode mode, string stylesheetName )
{
if ( string.IsNullOrEmpty( path ) )
throw new ArgumentNullException("path");
throw new ArgumentNullException(nameof(path));
if ( string.IsNullOrEmpty( stylesheetName ) )
throw new ArgumentNullException( "stylesheetName" );
throw new ArgumentNullException( nameof(stylesheetName) );
if ( dataObject == null )
throw new ArgumentNullException( "content" );
XmlSerializer _srlzr = new XmlSerializer( typeof( type ) );
throw new ArgumentNullException( nameof(dataObject) );
XmlSerializer _xmlSerializer = new XmlSerializer( typeof( type ) );
XmlWriterSettings _setting = new XmlWriterSettings()
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n"
};
using ( FileStream _docStrm = new FileStream( path, mode, FileAccess.Write ) )
using ( XmlWriter _writer = XmlWriter.Create( _docStrm, _setting ) )
using ( FileStream _docStream = new FileStream( path, mode, FileAccess.Write ) )
{
XmlWriter _writer = XmlWriter.Create(_docStream, _setting);
_writer.WriteProcessingInstruction( "xml-stylesheet", "type=\"text/xsl\" " + String.Format( "href=\"{0}\"", stylesheetName ) );
_srlzr.Serialize( _writer, dataObject );
_xmlSerializer.Serialize( _writer, dataObject );
}
}
/// <summary>
Expand All @@ -71,12 +78,12 @@ public static void WriteXmlFile<type>( type dataObject, string path, FileMode mo
public static type ReadXmlFile<type>( string path )
{
if ( string.IsNullOrEmpty( path ) )
throw new ArgumentNullException("path");
throw new ArgumentNullException(nameof(path));
type _content = default( type );
XmlSerializer _srlzr = new XmlSerializer( typeof( type ) );
XmlSerializer _xmlSerializer = new XmlSerializer( typeof( type ) );
FileStream _docStream = new FileStream(path, FileMode.Open, FileAccess.Read);
using ( XmlReader _writer = XmlReader.Create( _docStream ) )
_content = (type)_srlzr.Deserialize( _writer );
_content = (type)_xmlSerializer.Deserialize( _writer );
return _content;
}
#endregion
Expand Down
Binary file not shown.
Loading

0 comments on commit 19e0b55

Please sign in to comment.