Esempio n. 1
0
        /// <summary>
        /// Writes enumerable collection as a JSON array.
        /// </summary>
        public void Write(IEnumerable array)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            // already implements serialization interface?
            IJSonWritable jValue = array as IJSonWritable;

            if (jValue != null)
            {
                jValue.Write(this);
                return;
            }

            // is marked with serialization attribute?
            Type oType = array.GetType();
            JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType);

            if (jsonAttribute != null)
            {
                WriteAttributedObject(array, oType, jsonAttribute);
                return;
            }

            WriteArrayBegin();
            foreach (object value in array)
            {
                WriteEmbeddedValue(value);
            }
            WriteArrayEnd();
        }
Esempio n. 2
0
        /// <summary>
        /// Writes a dictionary as a JSON object.
        /// </summary>
        public void Write(IDictionary dictionary)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            // already implements serialization interface?
            IJSonWritable jValue = dictionary as IJSonWritable;

            if (jValue != null)
            {
                jValue.Write(this);
                return;
            }

            // is marked with serialization attribute?
            Type oType = dictionary.GetType();
            JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType);

            if (jsonAttribute != null)
            {
                WriteAttributedObject(dictionary, oType, jsonAttribute);
                return;
            }

            WriteObjectBegin();
            foreach (DictionaryEntry entry in dictionary)
            {
                WriteMember(entry.Key.ToString());
                WriteEmbeddedValue(entry.Value);
            }
            WriteObjectEnd();
        }
Esempio n. 3
0
        private void WriteAttributedObject(object o, Type oType, JSonSerializableAttribute jsonAttribute)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }
            if (oType == null)
            {
                throw new ArgumentNullException("oType");
            }
            if (jsonAttribute == null)
            {
                throw new ArgumentNullException("jsonAttribute");
            }

            // get members that will be serialized:
#if WINDOWS_STORE
            IEnumerable <FieldInfo>    fieldMembers    = ReflectionHelper.GetFields(oType);
            IEnumerable <PropertyInfo> propertyMembers = ReflectionHelper.GetProperties(oType);
#else
            IEnumerable <FieldInfo>    fieldMembers    = ReflectionHelper.GetFields(oType, jsonAttribute.Flags);
            IEnumerable <PropertyInfo> propertyMembers = ReflectionHelper.GetProperties(oType, jsonAttribute.Flags);
#endif

            WriteObjectBegin();

            // serialize all fields:
            foreach (FieldInfo f in fieldMembers)
            {
                if (!f.IsLiteral)
                {
                    JSonMemberAttribute attr = ReflectionHelper.GetCustomAttribute <JSonMemberAttribute>(f);
                    if (attr != null || jsonAttribute.AllowAllFields)
                    {
                        WriteAttributedMember(f.Name, f.GetValue(o), attr);
                    }
                }
            }

            // serialize all properties:
            foreach (PropertyInfo p in propertyMembers)
            {
                // there must be a getter defined:
                if (p.CanRead)
                {
                    JSonMemberAttribute attr = ReflectionHelper.GetCustomAttribute <JSonMemberAttribute>(p);
                    if (attr != null || jsonAttribute.AllowAllProperties)
                    {
                        WriteAttributedMember(p.Name, p.GetValue(o, null), attr);
                    }
                }
            }

            WriteObjectEnd();
        }
Esempio n. 4
0
        /// <summary>
        /// Writes whole object represented as dictionary or enumerable collection.
        /// </summary>
        public void Write(object o)
        {
            // try to access object as IJSonWritable class:
            IJSonWritable jValue = o as IJSonWritable;

            if (jValue != null)
            {
                Write(jValue);
                return;
            }

            if (o != null)
            {
                Type oType = o.GetType();
                JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType);

                if (jsonAttribute != null)
                {
                    WriteAttributedObject(o, oType, jsonAttribute);
                    return;
                }
            }

            // try to access object as dictionary:
            IDictionary dict = o as IDictionary;

            if (dict != null)
            {
                Write(dict);
                return;
            }

            // try to access object as enumerable:
            IEnumerable array = o as IEnumerable;

            if (array != null)
            {
                Write(array);
                return;
            }

            throw new ArgumentException("Invalid object type to serialize to JSON", "o");
        }
Esempio n. 5
0
        private void WriteAttributedObject(object o, Type oType, JSonSerializableAttribute jsonAttribute)
        {
            if (o == null)
                throw new ArgumentNullException("o");
            if (oType == null)
                throw new ArgumentNullException("oType");
            if (jsonAttribute == null)
                throw new ArgumentNullException("jsonAttribute");

            // get members that will be serialized:
#if WINDOWS_STORE
            IEnumerable<FieldInfo> fieldMembers = ReflectionHelper.GetFields(oType);
            IEnumerable<PropertyInfo> propertyMembers = ReflectionHelper.GetProperties(oType);
#else
            IEnumerable<FieldInfo> fieldMembers = ReflectionHelper.GetFields(oType, jsonAttribute.Flags);
            IEnumerable<PropertyInfo> propertyMembers = ReflectionHelper.GetProperties(oType, jsonAttribute.Flags);
#endif

            WriteObjectBegin();

            // serialize all fields:
            foreach (FieldInfo f in fieldMembers)
            {
                if (!f.IsLiteral)
                {
                    JSonMemberAttribute attr = ReflectionHelper.GetCustomAttribute<JSonMemberAttribute>(f);
                    if (attr != null || jsonAttribute.AllowAllFields)
                    {
                        WriteAttributedMember(f.Name, f.GetValue(o), attr);
                    }
                }
            }

            // serialize all properties:
            foreach (PropertyInfo p in propertyMembers)
            {
                // there must be a getter defined:
                if (p.CanRead)
                {
                    JSonMemberAttribute attr = ReflectionHelper.GetCustomAttribute<JSonMemberAttribute>(p);
                    if (attr != null || jsonAttribute.AllowAllProperties)
                    {
                        WriteAttributedMember(p.Name, p.GetValue(o, null), attr);
                    }
                }
            }

            WriteObjectEnd();
        }
Esempio n. 6
0
        private void WriteEmbeddedValue(object oValue)
        {
            // check if this is a struct:
            if (oValue is Single)
            {
                WriteValue((Single)oValue);
                return;
            }
            if (oValue is Double)
            {
                WriteValue((Double)oValue);
                return;
            }
            if (oValue is Decimal)
            {
                WriteValue((Decimal)oValue);
                return;
            }
            if (oValue is DateTime)
            {
                WriteValue((DateTime)oValue);
                return;
            }
            if (oValue is TimeSpan)
            {
                WriteValue((TimeSpan)oValue);
                return;
            }
            if (oValue is Boolean)
            {
                WriteValue((Boolean)oValue);
                return;
            }
            // check if this is a class object:
            if (oValue == null)
            {
                WriteValueNull();
                return;
            }
            if (oValue is DBNull)
            {
                WriteValueNull();
                return;
            }
            if (oValue is String || oValue is Char)
            {
                WriteValue(oValue.ToString());
                return;
            }
            if (oValue is Guid)
            {
                WriteValue((Guid)oValue);
                return;
            }
            if (oValue is Enum)
            {
                WriteValue(oValue.ToString());
                return;
            }

            // does it implement the dedicated serialization interface?
            IJSonWritable jValue = oValue as IJSonWritable;

            if (jValue != null)
            {
                jValue.Write(this);
                return;
            }

            // is it marked with serialization attribute?
            Type oType = oValue.GetType();
            JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType);

            if (jsonAttribute != null)
            {
                WriteAttributedObject(oValue, oType, jsonAttribute);
                return;
            }

            // is a dictionary?
            IDictionary dict = oValue as IDictionary;

            if (dict != null)
            {
                Write(dict);
                return;
            }

            // is an array of elements?
            IEnumerable array = oValue as IEnumerable;

            if (array != null)
            {
                Write(array);
            }
            else
            {
                WriteValueInternal(oValue.ToString(), false);
            }
        }
Esempio n. 7
0
        private void WriteAttributedObject(object o, Type oType, JSonSerializableAttribute jsonAttribute)
        {
            if (o == null)
                throw new ArgumentNullException("o");
            if (oType == null)
                throw new ArgumentNullException("oType");
            if (jsonAttribute == null)
                throw new ArgumentNullException("jsonAttribute");

            // get members that will be serialized:
            FieldInfo[] fieldMembers = oType.GetFields(jsonAttribute.Flags);
            PropertyInfo[] propertyMembers = oType.GetProperties(jsonAttribute.Flags);

            WriteObjectBegin();

            // serialize all fields:
            foreach (FieldInfo f in fieldMembers)
            {
                if (!f.IsLiteral)
                {
                    JSonMemberAttribute attr = (JSonMemberAttribute)Attribute.GetCustomAttribute(f, typeof(JSonMemberAttribute));
                    if (attr != null || jsonAttribute.AllowAllFields)
                    {
                        WriteAttributedMember(f.Name, f.GetValue(o), attr);
                    }
                }
            }

            // serialize all properties:
            foreach (PropertyInfo p in propertyMembers)
            {
                // there must be a getter defined:
                if (p.CanRead)
                {
                    JSonMemberAttribute attr = (JSonMemberAttribute)Attribute.GetCustomAttribute(p, typeof(JSonMemberAttribute));
                    if (attr != null || jsonAttribute.AllowAllProperties)
                    {
                        WriteAttributedMember(p.Name, p.GetValue(o, null), attr);
                    }
                }
            }

            WriteObjectEnd();
        }