예제 #1
0
        public void TestSerializeModelObject()
        {
            var s = new ModelSerializer(StringSerializer.Create(), new LoadedTypeSerializer());

            object1.Object = null; // Remove unresolvable dependency

            var strm = new MemoryStream();

            streamWriter = new StreamWriter(strm);
            var writer = new SectionedStreamWriter(streamWriter);

            s.SerializeAttributes(object1, writer);

            streamWriter.Flush();

            strm.Position = 0;

            // For testing
            string serialized = getStringFromStream(strm);


            var deserialized = new TestObject();

            s.DeserializeAttributes(deserialized, new SectionedStreamReader(new StreamReader(strm)));

            Assert.AreEqual(object1.ToString(), deserialized.ToString());
        }
        private void serializeObjects(List <IModelObject> list, StreamWriter wr)
        {
            //       // TODO: Haxor for debug info!!!
            //strm.WriteLine((string)obj.GetType().GetField("DEBUG_CREATEDBY", BindingFlags.NonPublic| BindingFlags.Instance).GetValue(obj));
            //(string)obj.GetType().GetField("DEBUG_CREATEDBY", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(obj,reader.ReadLine());
            //// TODO: Haxor for debug info!!!
            //strm.WriteLine(.GetValue(obj));

            var strm = new SectionedStreamWriter(wr);

            myObjectDictionary.Clear();

            // Write all objects
            strm.EnterSection(ObjectsSection);
            foreach (var obj in list)
            {
                var id = myObjectDictionary.getObjectID(obj);
                strm.WriteLine(id.ToString());
                strm.WriteLine(typeSerializer.Serialize(obj.GetType()));
            }
            strm.ExitSection();

            // Write all attributes
            strm.EnterSection(AttributesSection);
            foreach (var obj in list)
            {
                var id = myObjectDictionary.getObjectID(obj);
                strm.WriteLine(id.ToString());
                SerializeAttributes(obj, strm);
            }
            strm.ExitSection();
        }
        /// <summary>
        /// Writes a string serialized attribute value to the stream
        /// </summary>
        /// <param name="strm"></param>
        /// <param name="serialized"></param>
        private static void writeAttributeData(SectionedStreamWriter strm, string serialized)
        {
            if (serialized.Contains('\n'))
            {
                var sublength = serialized.Length;
                if (serialized[serialized.Length - 1] == '\n')
                {
                    sublength--;
                }
                else
                {
                    throw new InvalidOperationException(
                              "Provided serialized data cannot be used since it doesnt end with \n");
                }
                if (serialized[serialized.Length - 2] == '\r')
                {
                    sublength--;
                }


                // Write section
                strm.EnterSection("AttributeData");
                strm.WriteLine(serialized.Substring(0, sublength));
                strm.ExitSection();
            }
            else
            {
                strm.WriteLine(serialized);
            }
        }
예제 #4
0
        public void TestEscaping()
        {
            var reader = new SectionedStreamReader(new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes("Ello"))));

            Assert.False(reader.isStartSection(SectionedStreamWriter.Escape("[ello]")));
            Assert.False(reader.isStartSection(SectionedStreamWriter.Escape("ello]")));
            Assert.False(reader.isStartSection(SectionedStreamWriter.Escape("[ello")));
            Assert.True(reader.isStartSection("[ello]"));
        }
        /// <summary>
        /// Writes an entire attribute
        /// </summary>
        private void writeAttribute(SectionedStreamWriter strm, IAttribute att, object value)
        {
            strm.WriteLine(att.Name);
            var serialized = serializeAttribute(att, value);

            if (serialized == StringSerializer.Unknown)
            {
                Console.WriteLine("Cannot serialize type: {0}", value.GetType().FullName);
            }
            writeAttributeData(strm, serialized);
        }
        public void SerializeAttributes(IModelObject obj, SectionedStreamWriter strm)
        {
            var allAttributes = ReflectionHelper.GetAllAttributes(obj.GetType());


            strm.EnterSection("EntityAttributes");

            foreach (var att in allAttributes)
            {
                var value = att.GetData(obj);
                if (value == null)
                {
                    continue;
                }

                writeAttribute(strm, att, value);
            }
            strm.ExitSection();
        }