本文整理汇总了C#中Serializer.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# Serializer.Initialize方法的具体用法?C# Serializer.Initialize怎么用?C# Serializer.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Serializer
的用法示例。
在下文中一共展示了Serializer.Initialize方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestClone
public void TestClone()
{
foreach (bool skipDefaults in new bool[] { true, false }) {
Serializer serializer = new Serializer();
serializer.Initialize();
serializer.SkipDefaultsDuringSerialization = skipDefaults;
Test source = new Test();
source.tstr = "Test String";
source.ti = 1;
source.td = 3.141;
source.te = TestEnum.ValueOfOne;
source.tHashtable = new Hashtable { { 1, 2 }, { "foo", new TestData { name = "bar" } } };
source.tTypedDictionary = new Dictionary<string, double> { { "foo", 1 }, { "bar", 2 } };
source.tTypedDictOfData = new Dictionary<string, TestData> { { "foo", new TestData { name = "wheat" } } };
source.tArrayList = new ArrayList { 1, 2, "foo", true, new TestData { name = "wheat" } };
source.tTypedList = new List<TestData> { new TestData { name = "wheat" }, new TestData { name = "bread" } };
source.tTestRecursive = new Test();
source.tTestRecursive.tstr = "Hello World!";
Test target = serializer.Clone(source);
Assert.IsTrue(target != source);
Assert.IsTrue(source.tstr == target.tstr);
Assert.IsTrue(source.ti == target.ti);
Assert.IsTrue(source.td == target.td);
Assert.IsTrue(source.te == target.te);
Assert.IsTrue(source.tHashtable != target.tHashtable);
Assert.IsTrue(source.tHashtable.Count == target.tHashtable.Count);
Assert.IsTrue((int)(source.tHashtable[1]) == (int)(target.tHashtable[1]));
Assert.IsTrue(((TestData)(source.tHashtable["foo"])).name == ((TestData)(target.tHashtable["foo"])).name);
Assert.IsTrue(source.tArrayList.Count == target.tArrayList.Count);
Assert.IsTrue(source.tTypedList.Count == target.tTypedList.Count);
Assert.IsTrue(source.tTypedList[0].name == target.tTypedList[0].name);
serializer.Release();
}
}
示例2: Main
static void Main(string[] args)
{
//TODO: protobuf records
//TODO: parameters filtering records by
// - intersection
// - metaintersection
// - time (from, to, <=, <)
// - channels (also for infering from metaintersection, intersection)
var folderName = "folder";
var recordsFileName = "records.mig";
var testPath = @"E:\Politechnika\TWO\MGR\Natężenia";
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(folderName);
}
var sw = new Stopwatch();
sw.Start();
IEnumerable<Record> records;
Record[] enumerable;
Loader loader;
var serializer = new Serializer();
serializer.Initialize(typeof(Loader));
if (!File.Exists(recordsFileName))
{
Console.WriteLine("SERIALIZIN'");
loader = new Loader(testPath);
Console.WriteLine("LOADER CREATED AT {0:0.00}s", sw.Elapsed.TotalSeconds);
var fStream = new FileStream(recordsFileName, FileMode.Create);
serializer.Serialize(loader, fStream);
Console.WriteLine("LOADER SERIALIZED AT {0:0.00}s", sw.Elapsed.TotalSeconds);
}
else
{
Console.WriteLine("DESERIALIZIN'");
var fStream = new FileStream(recordsFileName, FileMode.Open);
loader = serializer.Deserialize<Loader>(fStream);
Console.WriteLine("LOADER DESERIALIZED AT {0:0.00}s", sw.Elapsed.TotalSeconds);
}
Console.WriteLine("LOADED AT {0:0.00}s", sw.Elapsed.TotalSeconds);
records = loader.Records;
File.WriteAllLines("bad_files.txt", ParseDiagnostics.BadFiles);
enumerable = records as Record[] ?? records.ToArray();
Console.WriteLine(enumerable.Count());
sw.Stop();
Console.WriteLine("RECORDS EXTRACTED AT {0:0.00}s", sw.Elapsed.TotalSeconds);
Console.ReadKey();
sw.Start();
var f = 0;
var fileSw = new Stopwatch();
fileSw.Start();
foreach (var dataFile in loader.DataFiles)
{
enumerable = dataFile.Records as Record[] ?? dataFile.Records.ToArray();
var sb = new StringBuilder();
int i = 0;
sb.Append(String.Format("\t\t\t\t"));
foreach (var ch in dataFile.DataChannels)
{
sb.Append(String.Format("{0}\t", ch.UId));
}
sb.Append(Environment.NewLine);
foreach (var time in enumerable.Select(r => r.Start).Distinct().OrderBy(dt => dt))
{
sb.Append(String.Format("{0}:\t", time.ToString("MM-dd hh:mm")));
foreach (var ch in dataFile.DataChannels)
{
DateTime time1 = time;
Channel ch1 = ch;
sb.Append(String.Format("{0}\t\t", enumerable
.Where(r => r.Start == time1 && r.Channel == ch1)
.ToLine()));
//if (i++ % 1000 == 0) Console.WriteLine(i);
}
sb.Append(Environment.NewLine);
}
File.WriteAllText(String.Format("{0}\\{1}.txt", "folder", dataFile), sb.ToString());
f++;
Console.WriteLine("{0}/{1} files, {2:0.00}s (+{3:0.00}s) elapsed...", f, loader.DataFiles.Count, sw.Elapsed.TotalSeconds, fileSw.Elapsed.TotalSeconds);
fileSw.Restart();
//.........这里部分代码省略.........
示例3: TestSerializationDeserializationPrimitives
public void TestSerializationDeserializationPrimitives()
{
Serializer serializer = new Serializer();
serializer.Initialize();
TestPrimitives source = new TestPrimitives() {
b1 = byte.MinValue, b2 = byte.MaxValue, sb1 = sbyte.MinValue, sb2 = sbyte.MaxValue,
s1 = short.MinValue, s2 = short.MaxValue, us1 = ushort.MinValue, us2 = ushort.MaxValue,
i1 = int.MinValue, i2 = int.MaxValue, u1 = uint.MinValue, u2 = uint.MaxValue,
f = (float)System.Math.PI, d = System.Math.PI, c1 = char.MinValue, c2 = char.MaxValue, s = "foo" };
object intermediate = serializer.Serialize(source, true);
TestPrimitives result = (TestPrimitives) serializer.Deserialize(intermediate);
foreach (var member in TypeUtils.GetMembers(source)) {
object sobj = TypeUtils.GetValue(member, source);
object robj = TypeUtils.GetValue(member, result);
Assert.IsTrue(sobj.Equals(robj));
}
serializer.Release();
}
示例4: TestSerializationDeltaAndJSON
public void TestSerializationDeltaAndJSON()
{
Serializer serializer = new Serializer();
serializer.Initialize();
serializer.SkipDefaultsDuringSerialization = true;
Test source = new Test();
source.tstr = "Test String";
source.ti = 1;
source.td = 3.141;
source.tTypedList = new List<TestData> { new TestData { name = "wheat" }, new TestData { name = "bread" } };
source.tTestRecursive = new Test();
source.tTestRecursive.tstr = "Hello World!";
source.tTypedDictOfData = new Dictionary<string, TestData> { { "foo", new TestData { name = "wheat" } } };
Hashtable serialized = serializer.Serialize(source) as Hashtable;
Assert.IsNotNull(serialized);
string json = JSON.JsonEncode(serialized);
Assert.IsNotNull(json);
object unjson = JSON.JsonDecode(json);
Assert.IsNotNull(unjson);
Test result = serializer.Deserialize<Test>(unjson);
Assert.IsNotNull(result);
Assert.IsTrue(source.tstr == result.tstr);
Assert.IsTrue(source.td == result.td);
Assert.IsTrue(source.ti == result.ti);
Assert.IsTrue(source.tTestRecursive.tstr == result.tTestRecursive.tstr);
Assert.IsTrue(source.tTypedDictOfData["foo"].name == result.tTypedDictOfData["foo"].name);
Assert.IsTrue(source.tTypedDictOfData["foo"].buy == result.tTypedDictOfData["foo"].buy);
serializer.Release();
}
示例5: TestSerializationDelta
public void TestSerializationDelta()
{
Serializer serializer = new Serializer();
serializer.Initialize();
serializer.SkipDefaultsDuringSerialization = true;
Test source = new Test();
source.tstr = "Test String";
source.tTypedList = new List<TestData> { new TestData { name = "wheat" }, new TestData { name = "bread" } };
source.tTestRecursive = new Test();
source.tTestRecursive.tstr = "Hello World!";
source.tTypedDictOfData = new Dictionary<string, TestData> { { "foo", new TestData { name = "wheat" } } };
Hashtable serialized = serializer.Serialize(source) as Hashtable;
Assert.IsTrue(serialized["tstr"] as String == "Test String");
Assert.IsTrue(serialized["td"] == null);
Assert.IsTrue(serialized["ti"] == null);
Assert.IsTrue(serialized["te"] == null);
Assert.IsTrue((serialized["tTestRecursive"] as Hashtable)["tstr"] as String == "Hello World!");
Assert.IsTrue(((serialized["tTypedDictOfData"] as Hashtable)["foo"] as Hashtable)["name"] as string == "wheat");
Test result = serializer.Deserialize<Test>(serialized);
Assert.IsTrue(source.tstr == result.tstr);
Assert.IsTrue(source.td == result.td);
Assert.IsTrue(source.tTestRecursive.tstr == result.tTestRecursive.tstr);
Assert.IsTrue(source.tTypedDictOfData["foo"].name == result.tTypedDictOfData["foo"].name);
serializer.Release();
}
示例6: TestSerialization
public void TestSerialization()
{
foreach (bool skipDefaults in new bool[] { true, false }) {
Serializer serializer = new Serializer();
serializer.Initialize();
serializer.SkipDefaultsDuringSerialization = skipDefaults;
Test source = new Test();
source.tstr = "Test String";
source.ti = 1;
source.td = 3.141;
source.te = TestEnum.ValueOfOne;
source.tHashtable = new Hashtable { { 1, 2 }, { "foo", new TestData { name = "bar" } } };
source.tTypedDictionary = new Dictionary<string, double> { { "foo", 1 }, { "bar", 2 } };
source.tTypedDictOfData = new Dictionary<string, TestData> { { "foo", new TestData { name = "wheat" } } };
source.tArrayList = new ArrayList { 1, 2, "foo", true, new TestData { name = "wheat" } };
source.tTypedList = new List<TestData> { new TestData { name = "wheat" }, new TestSubData { name = "bread", subname = "wheat" } };
source.tTestRecursive = new Test();
source.tTestRecursive.tstr = "Hello World!";
source.tStruct.i = 42;
object result = serializer.Serialize(source);
Assert.IsTrue(result is Hashtable);
Assert.IsTrue(((Hashtable)result).Keys.Count == (skipDefaults ? 11 : 14));
Assert.IsTrue((string)((Hashtable)result)["tstr"] == "Test String");
Assert.IsTrue((int)((Hashtable)result)["ti"] == 1);
Assert.IsTrue((double)((Hashtable)result)["td"] == 3.141);
Assert.IsTrue((int)((Hashtable)result)["te"] == 1); // enum gets serialized as its numeric value
string json = JSON.JsonEncode(result);
Assert.IsNotNull(json);
serializer.Release();
}
}
示例7: TestDeserializingImplicitNamespaces
public void TestDeserializingImplicitNamespaces()
{
string test = @"
{
""tHashtable"": {
""foo"": ""bar"",
""x"": 1,
""instance"": {
""#type"": ""TestData"",
""name"": ""foo""
}
}
}";
Serializer serializer = new Serializer();
serializer.Initialize();
serializer.AddImplicitNamespace("SomaSim.Serializer.SerializerTest", false); // false because it's an enclosing class, not a namespace!
Hashtable jsonobj = JSON.JsonDecode(test) as Hashtable;
Test target = serializer.Deserialize<Test>(jsonobj);
Assert.IsTrue(target.tHashtable != null);
Assert.IsTrue(target.tHashtable["instance"] != null);
Assert.IsTrue(target.tHashtable["instance"] is TestData);
Assert.IsTrue((target.tHashtable["instance"] as TestData).name == "foo");
}
示例8: TestDeserialization
public void TestDeserialization()
{
Serializer serializer = new Serializer();
serializer.Initialize();
Hashtable jsonobj = JSON.JsonDecode(json) as Hashtable;
Test target = serializer.Deserialize<Test>(jsonobj);
Assert.IsTrue(target.ti == 1);
Assert.IsTrue(target.td == 3.141);
Assert.IsTrue(target.tstr == "This is a test string");
Assert.IsTrue(target.te == TestEnum.ValueOfOne);
Assert.IsTrue(target.tHashtable.Keys.Count == 3);
Assert.IsTrue(target.tHashtable["foo"] as String == "bar");
Assert.IsTrue(target.tHashtable["instance"] is TestData);
Assert.IsTrue((target.tHashtable["instance"] as TestData).name == "foo");
Assert.IsTrue(target.tTypedDictionary.Keys.Count == 3);
Assert.IsTrue(target.tTypedDictionary["a"] == 1);
Assert.IsTrue(target.tTypedDictionary["b"] == 2);
Assert.IsTrue(target.tTypedDictionary["c"] == 3.1);
Assert.IsTrue(target.tTypedDictOfData.Keys.Count == 1);
Assert.IsTrue(target.tTypedDictOfData["foo"].name == "wheat");
Assert.IsTrue(target.tArrayList.Count == 4);
Assert.IsTrue(target.tArrayList[2] as String == "foo");
Assert.IsTrue(target.tTypedList.Count == 3);
Assert.IsTrue(target.tTypedList[0].name == "wheat");
Assert.IsTrue(target.tTypedList[0].buy == 10);
Assert.IsTrue(target.tTypedList[0].sell);
Assert.IsTrue(target.tStruct.i == 123);
Assert.IsTrue(target.tStruct.j == 0);
Assert.IsTrue(target.tTestRecursive.ti == 4);
Assert.IsTrue(target.tTestRecursiveExplicit is Test);
Assert.IsTrue((target.tTestRecursiveExplicit as Test).ti == 42);
serializer.Release();
}