public void SetUp()
        {
            _name = "PropertyName";

            _serializer = new PropertySerializer(
                new DataTypeSerializer());
        }
Exemple #2
0
 public override void SetUp()
 {
     base.SetUp();
     _columnSerializerStub = MockRepository.GenerateStub <IColumnSerializer>();
     _propertySerializer   = new PropertySerializer(_columnSerializerStub);
     _rdbmsPersistenceModelProviderStub = MockRepository.GenerateStub <IRdbmsPersistenceModelProvider>();
 }
        public object Deserialize(IFileStream stream)
        {
            var obj = new TType();

            PropertySerializer.Serialize(obj, stream);
            return(obj);
        }
Exemple #4
0
        static PropertySerializer[] GetPropertySerializers(Type type)
        {
            var props  = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var result = new PropertySerializer[props.Length];

            for (int i = 0, length = props.Length; i < length; ++i)
            {
                var prop   = props[i];
                var attr   = prop.GetCustomAttribute <JSONKeyAttribute>(true);
                var key    = attr != null ? attr.KeyName : prop.Name;
                var writer = GetSerializer(prop.PropertyType);
                if (writer != null)
                {
                    result[i] = new PropertySerializer
                    {
                        Key        = key,
                        WriteValue = (output, value, options) => writer(output, prop.GetValue(value), options)
                    };
                }
                else // happens when the type definition has recursion
                {
                    var propSer = result[i] = new PropertySerializer();
                    propSer.Key        = key;
                    propSer.WriteValue = (output, value, options) =>
                    {
                        var newWriter = GetSerializer(prop.PropertyType);
                        newWriter(output, prop.GetValue(value), options);
                        propSer.WriteValue = (output2, value2, options2) => newWriter(output2, prop.GetValue(value2), options2);
                    };
                }
            }
            return(result);
        }
        XElement SerializeProperty(PropertyDescriptor property)
        {
            var document   = new XDocument();
            var serializer = PropertySerializer.GetXmlSerializer(property.Name, property.PropertyType);

            using (var writer = document.CreateWriter())
            {
                serializer.Serialize(writer, property.GetValue(this), DefaultSerializerNamespaces);
            }
            return(document.Root);
        }
Exemple #6
0
 public virtual void Serialize(IFileStream stream)
 {
     if (stream.Mode == SerializeMode.Reading)
     {
         PropertySerializer.Serialize(this, stream);
         stream.SerializeValue(ref _objectUnknown);
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Exemple #7
0
        public Task LoadStateCommand(string[] args)
        {
            AppState state = PropertySerializer.Deserialize <AppState>(PropertyFiles.STATE_FILE);

            service.TickerSymbol = state.TickerSymbol;
            Cash   = state.Cash ?? 0;
            Shares = state.Shares ?? 0;

            IO.ShowMessage(Messages.LoadedStateFormat, PropertyFiles.STATE_FILE);

            return(Task.CompletedTask);
        }
Exemple #8
0
        public void TestSerializeReadOnlyWithContinue()
        {
            HaveReadOnly o = new HaveReadOnly();

            o.Value = "a";

            PropertySerializer <HaveReadOnly> ser = new PropertySerializer <HaveReadOnly>("Value", "WriteOnly");

            ser.ContinueOnError = true;
            Assert.AreEqual(true, ser.ContinueOnError);

            ser.Serialize(o, Dictionary);
        }
Exemple #9
0
        public Task SaveStateCommand(string[] args)
        {
            AppState state = new AppState();

            state.TickerSymbol = service.TickerSymbol;
            state.Shares       = Shares;
            state.Cash         = Cash;

            PropertySerializer.Serialize(state, PropertyFiles.STATE_FILE);

            IO.ShowMessage(Messages.SavedStateFormat, PropertyFiles.STATE_FILE);

            return(Task.CompletedTask);
        }
Exemple #10
0
        public object Deserialize(IFileStream stream)
        {
            string typeName = null;

            stream.SerializeName(ref typeName);
            uint valueSize = 0;

            stream.SerializeValue(ref valueSize);
            var serializer = PropertySerializer.GetSerializer(typeName);

            if (serializer != null)
            {
                var value = serializer.Deserialize(stream);
                return(value);
            }
            stream.Position += valueSize - 4;
            return(null);
        }
Exemple #11
0
        public void TestDeserializeReadOnly()
        {
            HaveReadOnly o = new HaveReadOnly();

            o.Value = "a";

            PropertySerializer <HaveReadOnly> ser = new PropertySerializer <HaveReadOnly>("Value", "ReadOnly");

            ser.ContinueOnError = false;
            Assert.AreEqual(false, ser.ContinueOnError);

            ser.Serialize(o, Dictionary);

            HaveReadOnly test = new HaveReadOnly();

            ser.Deserialize(test, Dictionary);            //should go boom

            Assert.Fail();
        }
Exemple #12
0
        public void TestObsolete()
        {
            PropertySerializer <HaveReadOnly> ser = new PropertySerializer <HaveReadOnly>();

            try
            {
                ser.GetType().InvokeMember("Serialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null,
                                           ser, new object[] { new object(), Dictionary });
            }
            catch (TargetInvocationException e)
            { Assert.AreEqual(typeof(NotSupportedException), e.InnerException.GetType()); }
            try
            {
                ser.GetType().InvokeMember("Deserialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null,
                                           ser, new object[] { new object(), Dictionary });
            }
            catch (TargetInvocationException e)
            { Assert.AreEqual(typeof(NotSupportedException), e.InnerException.GetType()); }
        }
        void DeserializeProperty(XElement element, PropertyDescriptor property)
        {
            if (property.PropertyType == typeof(XElement))
            {
                property.SetValue(this, element);
                return;
            }

            var serializer = PropertySerializer.GetXmlSerializer(property.Name, property.PropertyType);

            using (var reader = element.CreateReader())
            {
                var value = serializer.Deserialize(reader);
                if (property.IsReadOnly)
                {
                    var collection = (IList)property.GetValue(this);
                    if (collection == null)
                    {
                        throw new InvalidOperationException("Collection reference not set to an instance of an object.");
                    }

                    collection.Clear();
                    var collectionElements = value as IEnumerable;
                    if (collectionElements != null)
                    {
                        foreach (var collectionElement in collectionElements)
                        {
                            collection.Add(collectionElement);
                        }
                    }
                }
                else
                {
                    property.SetValue(this, value);
                }
            }
        }
Exemple #14
0
        public void TestSerialize()
        {
            PropertySerializer ser = new PropertySerializer(typeof(TestValues1), ValueNames);

            ser.ContinueOnError = false;
            Assert.AreEqual(false, ser.ContinueOnError);

            ser.Serialize(ValuesA, Dictionary);

            TestValues test = new TestValues();

            ser.Deserialize(test, Dictionary);

            Assert.AreEqual(ValuesA._bool, test._bool);
            Assert.AreEqual(ValuesA._byte, test._byte);
            Assert.AreEqual(ValuesA._char, test._char);
            Assert.AreEqual(ValuesA._DateTime, test._DateTime);
            Assert.AreEqual(ValuesA._decimal, test._decimal);
            Assert.AreEqual(ValuesA._double, test._double);
            Assert.AreEqual(ValuesA._float, test._float);
            Assert.AreEqual(ValuesA._Guid, test._Guid);
            Assert.AreEqual(ValuesA._int, test._int);
            Assert.AreEqual(ValuesA._long, test._long);
            Assert.AreEqual(ValuesA._sbyte, test._sbyte);
            Assert.AreEqual(ValuesA._short, test._short);
            Assert.AreEqual(ValuesA._string, test._string);
            Assert.AreEqual(ValuesA._TimeSpan, test._TimeSpan);
            Assert.AreEqual(ValuesA._uint, test._uint);
            Assert.AreEqual(ValuesA._ulong, test._ulong);
            Assert.AreEqual(ValuesA._Uri, test._Uri);
            Assert.AreEqual(ValuesA._ushort, test._ushort);
            Assert.AreEqual(ValuesA._Version, test._Version);

            //ROK - note, it can not deserialize this since it does not know the type:
            Assert.AreEqual(null, test._object);
        }
Exemple #15
0
 public virtual void Serialize(IFileStream stream)
 {
     PropertySerializer.Serialize(this, stream);
     stream.SerializeValue(ref this.CObjectUnknown0);
 }
		public void TestSerialize()
		{
			PropertySerializer ser = new PropertySerializer(typeof(TestValues1), ValueNames);
			ser.ContinueOnError = false;
			Assert.AreEqual(false, ser.ContinueOnError);

			ser.Serialize(ValuesA, Dictionary);

			TestValues test = new TestValues();
			ser.Deserialize(test, Dictionary);

			Assert.AreEqual(ValuesA._bool, test._bool);
			Assert.AreEqual(ValuesA._byte, test._byte);
			Assert.AreEqual(ValuesA._char, test._char);
			Assert.AreEqual(ValuesA._DateTime, test._DateTime);
			Assert.AreEqual(ValuesA._decimal, test._decimal);
			Assert.AreEqual(ValuesA._double, test._double);
			Assert.AreEqual(ValuesA._float, test._float);
			Assert.AreEqual(ValuesA._Guid, test._Guid);
			Assert.AreEqual(ValuesA._int, test._int);
			Assert.AreEqual(ValuesA._long, test._long);
			Assert.AreEqual(ValuesA._sbyte, test._sbyte);
			Assert.AreEqual(ValuesA._short, test._short);
			Assert.AreEqual(ValuesA._string, test._string);
			Assert.AreEqual(ValuesA._TimeSpan, test._TimeSpan);
			Assert.AreEqual(ValuesA._uint, test._uint);
			Assert.AreEqual(ValuesA._ulong, test._ulong);
			Assert.AreEqual(ValuesA._Uri, test._Uri);
			Assert.AreEqual(ValuesA._ushort, test._ushort);
			Assert.AreEqual(ValuesA._Version, test._Version);

			//ROK - note, it can not deserialize this since it does not know the type:
			Assert.AreEqual(null, test._object);
		}
        /// <summary>
        /// Returns a serializer that can be used to serialize and object
        /// of type <paramref name="objectType"/>.
        /// <note>
        ///     TODO: Add support for caching.
        /// </note>
        /// </summary>
        /// <param name="objectType">The type of object to be serialized.</param>
        /// <param name="ctx">The serialization context.</param>
        public virtual ISerializer Build(Type objectType, SerializationContext ctx)
        {
            if (objectType != null)
            {
                ISerializer s;

                if (typeof(Calendar).IsAssignableFrom(objectType))
                {
                    s = new CalendarSerializer(ctx);
                }
                else if (typeof(ICalendarComponent).IsAssignableFrom(objectType))
                {
                    s = typeof(CalendarEvent).IsAssignableFrom(objectType)
                        ? new EventSerializer(ctx)
                        : new ComponentSerializer(ctx);
                }
                else if (typeof(ICalendarProperty).IsAssignableFrom(objectType))
                {
                    s = new PropertySerializer(ctx);
                }
                else if (typeof(CalendarParameter).IsAssignableFrom(objectType))
                {
                    s = new ParameterSerializer(ctx);
                }
                else if (typeof(string).IsAssignableFrom(objectType))
                {
                    s = new StringSerializer(ctx);
                }
#if NET_4
                else if (objectType.IsEnum)
                {
                    s = new EnumSerializer(objectType, ctx);
                }
#else
                else if (objectType.GetTypeInfo().IsEnum)
                {
                    s = new EnumSerializer(objectType, ctx);
                }
#endif
                else if (typeof(TimeSpan).IsAssignableFrom(objectType))
                {
                    s = new TimeSpanSerializer(ctx);
                }
                else if (typeof(int).IsAssignableFrom(objectType))
                {
                    s = new IntegerSerializer(ctx);
                }
                else if (typeof(Uri).IsAssignableFrom(objectType))
                {
                    s = new UriSerializer(ctx);
                }
                else if (typeof(ICalendarDataType).IsAssignableFrom(objectType))
                {
                    s = _mDataTypeSerializerFactory.Build(objectType, ctx);
                }
                // Default to a string serializer, which simply calls
                // ToString() on the value to serialize it.
                else
                {
                    s = new StringSerializer(ctx);
                }

                return(s);
            }
            return(null);
        }
		public void TestDeserializeReadOnly()
		{
			HaveReadOnly o = new HaveReadOnly();
			o.Value = "a";

			PropertySerializer<HaveReadOnly> ser = new PropertySerializer<HaveReadOnly>("Value", "ReadOnly");
			ser.ContinueOnError = false;
			Assert.AreEqual(false, ser.ContinueOnError);

			ser.Serialize(o, Dictionary);

			HaveReadOnly test = new HaveReadOnly();
			ser.Deserialize(test, Dictionary);//should go boom

			Assert.Fail();
		}
 public ImageFilterSerializer(PropertySerializer propertySerializer)
     : base(propertySerializer)
 {
 }
		public void TestSerializeReadOnlyWithContinue()
		{
			HaveReadOnly o = new HaveReadOnly();
			o.Value = "a";

			PropertySerializer<HaveReadOnly> ser = new PropertySerializer<HaveReadOnly>("Value", "WriteOnly");
			ser.ContinueOnError = true;
			Assert.AreEqual(true, ser.ContinueOnError);

			ser.Serialize(o, Dictionary);
		}
Exemple #21
0
 public void Serialize(IFileStream stream)
 {
     PropertySerializer.Serialize(this, stream);
 }
Exemple #22
0
 static AppProperties()
 {
     Preferences = PropertySerializer.Deserialize <AppPreferences>(PropertyFiles.PREFERENCES_FILE);
     Messages    = PropertySerializer.Deserialize <AppMessages>(PropertyFiles.MESSAGES_FILE);
 }
		public void TestObsolete()
		{
			PropertySerializer<HaveReadOnly> ser = new PropertySerializer<HaveReadOnly>();
			try
			{
				ser.GetType().InvokeMember("Serialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null,
					ser, new object[] { new object(), Dictionary });
			}
			catch (TargetInvocationException e)
			{ Assert.AreEqual(typeof(NotSupportedException), e.InnerException.GetType()); }
			try
			{
				ser.GetType().InvokeMember("Deserialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance, null,
					ser, new object[] { new object(), Dictionary });
			}
			catch (TargetInvocationException e)
			{ Assert.AreEqual(typeof(NotSupportedException), e.InnerException.GetType()); }
		}