internal CompositeData(PortableObjectDocument document, object data, int metaTypeIndex = -1) { if (data == null) { throw new InvalidOperationException("No need to allocate CompositeData from NULL data. Write null directly"); //todo Refactor execption type,text etc... } m_Document = document; var tp = data.GetType(); if (tp.IsPrimitive) { throw new InvalidOperationException("Can not allocate CompositeData from primitive type: " + tp.FullName); //todo Refactor execption type,text etc... } var dict = document.m_CompositeDataDict; if (dict == null) { dict = new Dictionary <object, int>(ReferenceEqualityComparer <object> .Instance); document.m_CompositeDataDict = dict; } int existingIndex; if (dict.TryGetValue(data, out existingIndex)) { m_ExistingReferenceIndex = existingIndex; } else { document.m_CompositeData.Add(this); dict.Add(data, document.m_CompositeData.Count - 1); m_MetaTypeIndex = metaTypeIndex >= 0 ? metaTypeIndex : MetaType.GetExistingOrNewMetaTypeIndex(document, data.GetType()); } }
internal CompositeData(PortableObjectDocument document, object data, int metaTypeIndex = -1) { if (data==null) throw new InvalidOperationException("No need to allocate CompositeData from NULL data. Write null directly");//todo Refactor execption type,text etc... m_Document = document; var tp = data.GetType(); if (tp.IsPrimitive) throw new InvalidOperationException("Can not allocate CompositeData from primitive type: " + tp.FullName);//todo Refactor execption type,text etc... var dict = document.m_CompositeDataDict; if (dict==null) { dict = new Dictionary<object, int>(ReferenceEqualityComparer<object>.Instance); document.m_CompositeDataDict = dict; } int existingIndex; if (dict.TryGetValue(data, out existingIndex)) { m_ExistingReferenceIndex = existingIndex; } else { document.m_CompositeData.Add(this); dict.Add(data, document.m_CompositeData.Count-1); m_MetaTypeIndex = metaTypeIndex>=0 ? metaTypeIndex : MetaType.GetExistingOrNewMetaTypeIndex(document, data.GetType()); } }
internal CompositeReflectedData(PortableObjectDocument document, object data, int metaTypeIndex = -1) : base(document, data, metaTypeIndex) { if (!ExistingReference) { serializeFields(data); } }
internal CompositeCustomData(PortableObjectDocument document, object data, int metaTypeIndex = -1) : base(document, data, metaTypeIndex) { if (!ExistingReference) { serializeFromTransform(data); } }
internal MetaType(PortableObjectDocument document, Type type) { __CLRType = type; m_Document = document; m_Name = type.Name; m_Namespace = type.Namespace; m_AssemblyQualifiedName = type.AssemblyQualifiedName; }
internal CompositeArrayData(PortableObjectDocument document, Array data, int metaTypeIndex = -1) : base(document, data, metaTypeIndex) { if (!ExistingReference) { serializeArray(data); } }
public void RootPrimitive3_string() { var data = "test string"; var doc = new PortableObjectDocument(data); Assert.AreEqual(typeof(MetaPrimitiveType), doc.RootMetaType.GetType()); Assert.AreEqual("test string", doc.Root); }
public void RootPrimitive2_bool() { var data = true; var doc = new PortableObjectDocument(data); Assert.AreEqual(typeof(MetaPrimitiveType), doc.RootMetaType.GetType()); Assert.AreEqual(true, doc.Root); }
public void RootComposite1() { var data = new Tuple<int, string>(5,"yes"); var doc = new PortableObjectDocument(data); Assert.AreEqual(typeof(MetaComplexType), doc.RootMetaType.GetType()); Assert.AreEqual(typeof(CompositeReflectedData), doc.Root.GetType()); var crd = (CompositeReflectedData)doc.Root; Assert.AreEqual(2, crd.FieldData.Length); Assert.AreEqual(5, crd.FieldData[0]); Assert.AreEqual("yes", crd.FieldData[1]); }
/// <summary> /// Obtains new or existing instance of MetaType that represents a Type in the document instance /// </summary> internal static int GetExistingOrNewMetaTypeIndex(PortableObjectDocument document, Type type) { var dict = document.m_TypesDict; if (dict == null) { dict = new Dictionary <Type, int>(); document.m_TypesDict = dict; } int result; if (dict.TryGetValue(type, out result)) { return(result); } MetaType mtp; if (type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(TimeSpan)) { mtp = new MetaPrimitiveType(document, type); } else { mtp = new MetaComplexType(document, type); } //1. add to dict so no infinite loop happens during recursive call to this func result = document.m_Types.Count; document.m_Types.Add(mtp); dict.Add(type, result); //2. Build the type inner structures mtp.Build(); return(result); }
public void RootDictionary() { var originalData = new Dictionary<string, int> { {"x",10}, {"y",-20} }; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject() as Dictionary<string, int>; Assert.IsFalse( object.ReferenceEquals(originalData, convertedData) ); Assert.AreEqual(originalData.Count, convertedData.Count); Assert.AreEqual(10, convertedData["x"]); Assert.AreEqual(-20, convertedData["y"]); }
public void RootCompositeWriteRead_PersonList() { var originalData = new List<TestPerson> { new TestPerson{ Name = "Kolyan", DOB = DateTime.Now, Assets=2000000, IsRegistered=true, Luck=150.89}, new TestPerson{ Name = "Zmeyan", DOB = DateTime.Now.AddYears(-25), Assets=50000000, IsRegistered=false, Luck=283.4}, }; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject() as List<TestPerson>; Assert.IsFalse( object.ReferenceEquals(originalData, convertedData) ); Assert.AreEqual(originalData.Count, convertedData.Count); Assert.IsTrue (originalData[0].Equals( convertedData[0] ) ); Assert.IsTrue (originalData[1].Equals( convertedData[1] ) ); }
public void RootCompositeWriteRead_BusinessFamily() { var originalData = new TestBusinessFamily{ Husband = new TestPerson{ Name = "Kolyan Zver'", DOB = DateTime.Now, Assets=2000000, IsRegistered=true, Luck=150.5489}, Wife = new TestPerson{ Name = "Feiga Pozman", DOB = DateTime.Now, Assets=578, IsRegistered=false, Luck=250.489}, Kid = new TestPerson{ Name = "Mykola Zver'", DOB = DateTime.Now, Assets=12, IsRegistered=true, Luck=350.189}, Assets = 9000000000, IsCertified = true }; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject() as TestFamily; Assert.IsFalse( object.ReferenceEquals(originalData, convertedData) ); Assert.IsTrue (originalData.Equals( convertedData ) ); }
public void RootCompositeWriteRead_Person() { var originalData = new TestPerson{ Name = "Kolyan", DOB = DateTime.Now, Assets=2000000, IsRegistered=true, Luck=150.89}; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject() as TestPerson; Assert.IsFalse( object.ReferenceEquals(originalData, convertedData) ); Assert.IsTrue (originalData.Equals( convertedData ) ); }
public void RootCompositeWriteRead_tuple() { var originalData = new Tuple<int, string>(5,"yes"); var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject() as Tuple<int, string>; Assert.IsFalse( object.ReferenceEquals(originalData, convertedData) ); Assert.AreEqual(5, convertedData.Item1); Assert.AreEqual("yes", convertedData.Item2); }
/// <summary> /// Serializes a graph of arbitrary CLR objects into stream using PortableObjectDocument container, /// optionally taking document creation attributes /// </summary> /// <param name="stream">Target stream</param> /// <param name="root">CLR object graph</param> /// <param name="documentCreationDate">Optional document creation attribute</param> /// <param name="documentNotes">Optional document creation attribute</param> public void Serialize(Stream stream, object root, DateTime? documentCreationDate = null, string documentNotes = null) { var document = new PortableObjectDocument(root, documentCreationDate, documentNotes); m_Serializer.Serialize(stream, document); }
internal CompositeCustomData(PortableObjectDocument document, object data, int metaTypeIndex = -1) : base(document, data, metaTypeIndex) { if (!ExistingReference) serializeFromTransform(data); }
/// <summary> /// Override to provide custome seriallization of type marked with this attribute into custom name/value bag /// </summary> /// <param name="document">Document context that this operation executes under</param> /// <param name="source">Source native data to serialize / populate data from</param> /// <param name="data">Name/value bag to write data into</param> public abstract void SerializeCustomObjectData(PortableObjectDocument document, object source, Dictionary <string, CustomTypedEntry> data);
public void RootConcurrentDictionary() { var originalData = new ConcurrentDictionary<string, int>(); originalData["x"] = 10; originalData["y"] = -20; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject() as ConcurrentDictionary<string, int>; Assert.IsFalse( object.ReferenceEquals(originalData, convertedData) ); Assert.AreEqual(originalData.Count, convertedData.Count); Assert.AreEqual(10, convertedData["x"]); Assert.AreEqual(-20, convertedData["y"]); }
internal MetaPrimitiveType(PortableObjectDocument document, Type type) : base(document, type) { }
internal MetaComplexType(PortableObjectDocument document, Type type) : base(document, type) { }
public void DeserializationTransform1() { var originalData = new PODTest_Ver1 { Name = "Xerson Person", Description = "Some description", Age = 25 }; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject(new PODTestVersionUpgradeStrategy()); Assert.IsTrue( convertedData is PODTest_Ver2); var ver2 = convertedData as PODTest_Ver2; Assert.AreEqual( originalData.Name, ver2.Name); Assert.AreEqual( originalData.Description, ver2.Description); Assert.AreEqual( originalData.Age, ver2.AgeAsOfToday); Assert.AreEqual( DateTime.Now.AddYears(-originalData.Age).Year, ver2.DOB.Year); }
public void RootPrimitiveWriteRead_string() { var originalData = "hello testing"; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject(); Assert.AreEqual(originalData, convertedData); }
public void RootPrimitiveWriteRead_string_null() { string originalData = null; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject(); Assert.AreEqual(originalData, convertedData); }
public void RootPrimitiveWriteRead_double() { var originalData = 10 / 3.01d; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject(); Assert.AreEqual(originalData, convertedData); }
public void RootPrimitiveWriteRead_int_nullable1() { int? originalData = 5; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject(); Assert.AreEqual(originalData, convertedData); }
public void RootPrimitiveWriteRead_datetime() { var originalData = DateTime.Now; var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject(); Assert.AreEqual(originalData, convertedData); }
/// <summary> /// Override to provide custome seriallization of type marked with this attribute into custom name/value bag /// </summary> /// <param name="document">Document context that this operation executes under</param> /// <param name="source">Source native data to serialize / populate data from</param> /// <param name="data">Name/value bag to write data into</param> public abstract void SerializeCustomObjectData(PortableObjectDocument document, object source, Dictionary<string, CustomTypedEntry> data);
public void RootPrimitiveWriteRead_timespan() { var originalData = TimeSpan.FromDays(12.4321); var doc = new PortableObjectDocument(originalData); var convertedData = doc.ToOriginalObject(); Assert.AreEqual(originalData, convertedData); }
internal CompositeArrayData(PortableObjectDocument document, Array data, int metaTypeIndex = -1) : base(document, data, metaTypeIndex) { if (!ExistingReference) serializeArray(data); }
internal CompositeReflectedData(PortableObjectDocument document, object data, int metaTypeIndex = -1) : base(document, data, metaTypeIndex) { if (!ExistingReference) serializeFields(data); }