예제 #1
0
        private bool SerializeArray(Array array, IValueMemberInfo valueMemberInfo)
        {
            if (array.Rank > 1)
            {
                return(SerializeMultidimensionalArray(array));
            }

            int len = array.Length;

            if (len == 0)
            {
                _jsonWriter.WriteStartArray();
                _jsonWriter.WriteEndArray();
                return(true);
            }

            var valueMember = valueMemberInfo.ExtendedValueInfo;

            if (valueMember.IsType)
            {
                return(SerializeIListTyped(array, len, valueMember));
            }

            //no typed.
            return(SerializeIListUnTyped(array, len, valueMember));
        }
예제 #2
0
        private bool SerializeIListTyped(IList list, int len, IValueMemberInfo valueMemberInfo)
        {
            _jsonWriter.WriteStartArray();
            try
            {
                //one type
                for (var i = 0; i < len; i++)
                {
                    if (i > 0)
                    {
                        _jsonWriter.WriteComma();
                    }

                    var value = list[i];
                    if (value == null)
                    {
                        _jsonWriter.WriteNull();
                    }
                    else if (!WriteObjectValue(value, valueMemberInfo))
                    {
                        return(false);
                    }
                }
            }
            finally
            {
                _jsonWriter.WriteEndArray();
            }

            return(true);
        }
예제 #3
0
        private bool WriteObjectValue(object value, IValueMemberInfo valueMemberInfo)
        {
            var writeFunc = valueMemberInfo.WriteDelegate;

            if (writeFunc != null)
            {
                writeFunc(_jsonWriter, value);
                return(true);
            }

            return(SerializeNonPrimitiveValue(value, valueMemberInfo));
        }
예제 #4
0
        private bool SerializeCustomObject(object value, IValueMemberInfo valueMemberInfo)
        {
            IValueMemberInfo[] obj = valueMemberInfo.GetCustomObjectMemberInfo();

            if (obj != null)
            {
                return(this.SerializeValueMemberInfo(value, obj));
            }
            else
            {
                _jsonWriter.WriteNull();
                return(true);//was false but when false prevent continuing.
            }
        }
예제 #5
0
        private void CheckForExtendedValueInfo()
        {
            var code = this.Code;

            if (!TypeSerializerUtils.HasExtendedValueInformation(code))
            {
                return;
            }

            Type type = null;

            //array?
            if (code == TypeSerializerUtils.TypeCode.Array)
            {
                type = ObjectType.GetElementType();
            }
#if !NETCOREAPP1_0
            else if (ObjectType.IsGenericType)
#else
            else if (ObjectType.GetTypeInfo().IsGenericType)
#endif
            {
                //generic list / dictionary
                var args = ObjectType.GetGenericArguments();
                var len  = args.Length;
                if (len == 2)
                {
                    //key value pair
                    //need to check if key is string
                    var keyCodeType = TypeSerializerUtils.GetTypeCode(args[0]);
                    if (keyCodeType != TypeSerializerUtils.TypeCode.String)
                    {
                        _errored = true;
                        return;
                    }

                    type = args[1];
                }
                else if (len == 1)
                {
                    //value only
                    type = args[0];
                }
            }

            ExtendedValueInfo = new ValueMemberInfo(type ?? typeof(object));
        }
예제 #6
0
        private bool SerializeGenericDictionary(IDictionary values, IValueMemberInfo valueMemberInfo)
        {
            if (values.Count == 0)
            {
                _jsonWriter.WriteStartObject();
                _jsonWriter.WriteEndObject();
                return(true);
            }

            // Manual use of IDictionaryEnumerator instead of foreach to avoid DictionaryEntry box allocations.
            IDictionaryEnumerator e = values.GetEnumerator();
            var valueMember         = valueMemberInfo.ExtendedValueInfo;

            _jsonWriter.WriteStartObject();
            try
            {
                while (e.MoveNext())
                {
                    DictionaryEntry entry = e.Entry;

                    string name = entry.Key.ToString();
                    if (!name.IsNullOrEmpty())
                    {
                        _jsonWriter.WritePropertyName(name, true);

                        var value = entry.Value;
                        if (value == null)
                        {
                            _jsonWriter.WriteNull();
                        }
                        else if (!WriteObjectValue(value, valueMember))
                        {
                            return(false);
                        }
                    }
                }
            }
            finally
            {
                _jsonWriter.WriteEndObject();
            }

            return(true);
        }
예제 #7
0
        private bool SerializeIListUnTyped(IList list, int len, IValueMemberInfo valueMemberInfo)
        {
            Type lastType = null;

            _jsonWriter.WriteStartArray();
            try
            {
                //dynamic types
                for (var i = 0; i < len; i++)
                {
                    if (i > 0)
                    {
                        _jsonWriter.WriteComma();
                    }

                    var value = list[i];
                    if (value == null)
                    {
                        _jsonWriter.WriteNull();
                    }
                    else
                    {
                        var valueType = value.GetType();
                        if (lastType != valueType)
                        {
                            lastType        = valueType;
                            valueMemberInfo = new ValueMemberInfo(valueType);
                        }

                        if (!WriteObjectValue(value, valueMemberInfo))
                        {
                            return(false);
                        }
                    }
                }
            }
            finally
            {
                _jsonWriter.WriteEndArray();
            }

            return(true);
        }
예제 #8
0
        private bool SerializeList(System.Collections.IList list, IValueMemberInfo valueMemberInfo)
        {
            int len = list.Count;

            if (len == 0)
            {
                _jsonWriter.WriteStartArray();
                _jsonWriter.WriteEndArray();
                return(true);
            }

            IValueMemberInfo valueMember = valueMemberInfo.ExtendedValueInfo;

            if (valueMember.IsType)
            {
                return(SerializeIListTyped(list, len, valueMember));
            }

            //no typed.
            return(SerializeIListUnTyped(list, len, valueMember));
        }
예제 #9
0
        private bool SerializeNonPrimitiveValue(object value, IValueMemberInfo valueMemberInfo)
        {
            //this prevents recursion
            if (_cirobj.IsReferenced(value) && _currentDepth > 0)
            {
                //_circular = true;
                _jsonWriter.WriteNull();
                return(true);
            }
            _currentDepth++;
            //recursion limit or max char length
            if (_currentDepth >= _options.RecursionLimit)
            {
                _currentDepth--;
                _jsonWriter.WriteNull();
                return(true);
            }

            try
            {
                switch (valueMemberInfo.Code)
                {
                case TypeSerializerUtils.TypeCode.CustomObject:
                {
                    return(SerializeCustomObject(value, valueMemberInfo));
                }

                case TypeSerializerUtils.TypeCode.Array:
                {
                    return(SerializeArray((Array)value, valueMemberInfo));
                }

                case TypeSerializerUtils.TypeCode.IList:
                {
                    return(SerializeList((IList)value, valueMemberInfo));
                }

                case TypeSerializerUtils.TypeCode.Enumerable:
                {
                    return(this.SerializeEnumerable((IEnumerable)value, valueMemberInfo));
                }

                case TypeSerializerUtils.TypeCode.Dictionary:
                {
                    return(SerializeNonGenericDictionary((IDictionary)value));
                }

                case TypeSerializerUtils.TypeCode.GenericDictionary:
                {
                    return(SerializeGenericDictionary((IDictionary)value, valueMemberInfo));
                }

                case TypeSerializerUtils.TypeCode.NameValueCollection:
                {
                    return(this.SerializeNameValueCollection((NameValueCollection)value));
                }

                case TypeSerializerUtils.TypeCode.DataSet:
                {
                    return(SerializeDataSet((System.Data.DataSet)value));
                }

                case TypeSerializerUtils.TypeCode.DataTable:
                {
                    return(SerializeDataTable((System.Data.DataTable)value));
                }

                default:
                {
                    throw new NotImplementedException(valueMemberInfo.Code.ToString());
                }
                }
            }
            finally
            {
                _currentDepth--;
            }
        }
예제 #10
0
        private bool SerializeEnumerable(IEnumerable values, IValueMemberInfo valueMemberInfo)
        {
            Type            lastType    = null;
            bool            flag1       = true;
            bool            isTyped     = false;
            ValueMemberInfo valueMember = null;

            _jsonWriter.WriteStartArray();
            try
            {
                var e = values.GetEnumerator();

                // note that an error in the IEnumerable won't be caught
                while (e.MoveNext())
                {
                    var value = e.Current;
                    if (!flag1)
                    {
                        _jsonWriter.WriteComma();
                    }
                    else
                    {
                        //first record.
                        var valueType = GetEnumerableValueType(values, valueMemberInfo.ObjectType);
                        if (valueType != typeof(object))
                        {
                            valueMember = new ValueMemberInfo(valueType);
                            isTyped     = true;
                        }

                        flag1 = false;
                    }

                    if (value == null)
                    {
                        _jsonWriter.WriteNull();
                    }
                    else
                    {
                        if (!isTyped)
                        {
                            //is not generic
                            var valueType = value.GetType();
                            if (valueType != lastType)
                            {
                                lastType    = valueMemberInfo.ObjectType;
                                valueMember = new ValueMemberInfo(valueType);
                            }
                        }

                        if (!WriteObjectValue(value, valueMember))
                        {
                            return(false);
                        }
                    }
                }
            }
            finally
            {
                _jsonWriter.WriteEndArray();
            }

            return(true);
        }
예제 #11
0
        private bool SerializeMultidimensionalArray(Array values, int[] indices)
        {
            bool flag      = true;
            int  dimension = indices.Length;

            int[] newIndices = new int[dimension + 1];
            for (int i = 0; i < dimension; i++)
            {
                newIndices[i] = indices[i];
            }

            Type             lastTypeCode = null;
            IValueMemberInfo valueMember  = null;

            _jsonWriter.WriteStartArray();
            try
            {
                for (int i = values.GetLowerBound(dimension); i <= values.GetUpperBound(dimension); i++)
                {
                    newIndices[dimension] = i;
                    bool isTopLevel = (newIndices.Length == values.Rank);
                    if (isTopLevel)
                    {
                        object value = values.GetValue(newIndices);

                        if (!flag)
                        {
                            _jsonWriter.WriteComma();
                        }
                        else
                        {
                            flag = false;
                        }

                        if (value == null)
                        {
                            _jsonWriter.WriteNull();
                        }
                        else
                        {
                            var typeCode = value.GetType();
                            if (lastTypeCode != typeCode)
                            {
                                lastTypeCode = typeCode;
                                valueMember  = new ValueMemberInfo(typeCode);
                            }
                            if (!WriteObjectValue(value, valueMember))
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        if (i != 0)
                        {
                            _jsonWriter.WriteComma();
                        }

                        SerializeMultidimensionalArray(values, newIndices);
                    }
                }
            }
            finally
            {
                _jsonWriter.WriteEndArray();
            }
            return(true);
        }