Esempio n. 1
0
        /// <summary>
        ///   Converts the specified object into a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="obj">Object to convert.</param>
        /// <returns>Dictionary representation of the specified object.</returns>
        public Dictionary <string, object> Serialize(DictionarySerializationContext context, object obj)
        {
            IDictionary dictionary = (IDictionary)obj;
            Type        keyType    = dictionary.GetType().GetGenericArguments()[0];

            if (keyType != typeof(string))
            {
                throw new SerializationException("Only dictionaries with string keys can be serialized right now");
            }

            Type valueType = dictionary.GetType().GetGenericArguments()[1];

            Dictionary <string, object> data = new Dictionary <string, object> {
                { DataType, valueType.FullName }
            };

            bool typeSealed = valueType.IsSealed();

            Dictionary <string, object> keyValuePairs = new Dictionary <string, object>();

            foreach (string key in dictionary.Keys)
            {
                object value     = dictionary[key];
                object valueData = typeSealed || value == null ? value : new ValueWithType(value);
                keyValuePairs.Add(key, context.Serialize(valueData));
            }
            data.Add(DataPairs, keyValuePairs);

            return(data);
        }
        /// <summary>
        ///   Serializes an object to a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="obj">Object to serialize.</param>
        /// <returns>Dictionary which contains object data.</returns>
        public Dictionary <string, object> Serialize(DictionarySerializationContext context, object obj)
        {
            ValueWithType valueWithType = (ValueWithType)obj;

            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { DataValue, context.Serialize(valueWithType.Value) }
            };

            if (!context.IsRawSerializationPossible(valueWithType.Type))
            {
                data.Add(DataType, valueWithType.TypeFullName);
            }

            return(data);
        }
Esempio n. 3
0
        /// <summary>
        ///   Serializes an object to a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="obj">Object to serialize.</param>
        /// <returns>Dictionary which contains object data.</returns>
        public Dictionary <string, object> Serialize(DictionarySerializationContext context, object obj)
        {
            IList list     = (IList)obj;
            Type  itemType = list.GetType().GetGenericArguments()[0];

            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { DataCount, list.Count },
                { DataType, itemType.FullName }
            };

            bool typeSealed = itemType.IsSealed();

            for (int i = 0; i < list.Count; i++)
            {
                object value     = list[i];
                object valueData = typeSealed || value == null ? value : new ValueWithType(value);

                string valueKey = i.ToString(CultureInfo.InvariantCulture);
                data.Add(valueKey, context.Serialize(valueData));
            }

            return(data);
        }
        /// <summary>
        ///   Serializes an object to a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="obj">Object to serialize.</param>
        /// <returns>Dictionary which contains object data.</returns>
        public Dictionary <string, object> Serialize(DictionarySerializationContext context, object obj)
        {
            ICollection collection = (ICollection)obj;
            Type        itemType   = collection.GetType().GetGenericArguments()[0];

            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { DataCount, collection.Count },
                { DataType, itemType.FullName }
            };

            bool typeSealed = itemType.IsSealed();

            int index = 0;

            foreach (object value in collection)
            {
                object valueData = typeSealed || value == null ? value : new ValueWithType(value);
                data.Add(index.ToString(CultureInfo.InvariantCulture), context.Serialize(valueData));
                ++index;
            }

            return(data);
        }