GetColumnDataType() private static method

private static GetColumnDataType ( JsonToken tokenType ) : Type
tokenType JsonToken
return System.Type
Example #1
0
        // Token: 0x06001499 RID: 5273 RVA: 0x0006D498 File Offset: 0x0006B698
        private static Type GetColumnDataType(JsonReader reader)
        {
            JsonToken tokenType = reader.TokenType;

            switch (tokenType)
            {
            case JsonToken.StartArray:
                reader.ReadAndAssert();
                if (reader.TokenType == JsonToken.StartObject)
                {
                    return(typeof(DataTable));
                }
                return(DataTableConverter.GetColumnDataType(reader).MakeArrayType());

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.String:
            case JsonToken.Boolean:
            case JsonToken.Date:
            case JsonToken.Bytes:
                return(reader.ValueType);

            case JsonToken.Null:
            case JsonToken.Undefined:
            case JsonToken.EndArray:
                return(typeof(string));
            }
            throw JsonSerializationException.Create(reader, "Unexpected JSON token when reading DataTable: {0}".FormatWith(CultureInfo.InvariantCulture, tokenType));
        }
Example #2
0
        // Token: 0x06001498 RID: 5272 RVA: 0x0006D2E0 File Offset: 0x0006B4E0
        private static void CreateRow(JsonReader reader, DataTable dt, JsonSerializer serializer)
        {
            DataRow dataRow = dt.NewRow();

            reader.ReadAndAssert();
            while (reader.TokenType == JsonToken.PropertyName)
            {
                string text = (string)reader.Value;
                reader.ReadAndAssert();
                DataColumn dataColumn = dt.Columns[text];
                if (dataColumn == null)
                {
                    Type columnDataType = DataTableConverter.GetColumnDataType(reader);
                    dataColumn = new DataColumn(text, columnDataType);
                    dt.Columns.Add(dataColumn);
                }
                if (dataColumn.DataType == typeof(DataTable))
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.ReadAndAssert();
                    }
                    DataTable dataTable = new DataTable();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        DataTableConverter.CreateRow(reader, dataTable, serializer);
                        reader.ReadAndAssert();
                    }
                    dataRow[text] = dataTable;
                }
                else if (dataColumn.DataType.IsArray && dataColumn.DataType != typeof(byte[]))
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.ReadAndAssert();
                    }
                    List <object> list = new List <object>();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        list.Add(reader.Value);
                        reader.ReadAndAssert();
                    }
                    Array array = Array.CreateInstance(dataColumn.DataType.GetElementType(), list.Count);
                    ((ICollection)list).CopyTo(array, 0);
                    dataRow[text] = array;
                }
                else
                {
                    object value = (reader.Value != null) ? (serializer.Deserialize(reader, dataColumn.DataType) ?? DBNull.Value) : DBNull.Value;
                    dataRow[text] = value;
                }
                reader.ReadAndAssert();
            }
            dataRow.EndEdit();
            dt.Rows.Add(dataRow);
        }
Example #3
0
        private static void CreateRow(JsonReader reader, DataTable dt)
        {
            DataRow dataRow = dt.NewRow();

            reader.Read();
            while (reader.TokenType == JsonToken.PropertyName)
            {
                string text = (string)reader.Value;
                reader.Read();
                DataColumn dataColumn = dt.Columns[text];
                if (dataColumn == null)
                {
                    Type columnDataType = DataTableConverter.GetColumnDataType(reader);
                    dataColumn = new DataColumn(text, columnDataType);
                    dt.Columns.Add(dataColumn);
                }
                if (dataColumn.DataType == typeof(DataTable))
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.Read();
                    }
                    DataTable dataTable = new DataTable();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        DataTableConverter.CreateRow(reader, dataTable);
                        reader.Read();
                    }
                    dataRow[text] = dataTable;
                }
                else if (dataColumn.DataType.IsArray)
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.Read();
                    }
                    List <object> list = new List <object>();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        list.Add(reader.Value);
                        reader.Read();
                    }
                    Array array = Array.CreateInstance(dataColumn.DataType.GetElementType(), list.Count);
                    Array.Copy(list.ToArray(), array, list.Count);
                    dataRow[text] = array;
                }
                else
                {
                    dataRow[text] = (reader.Value ?? DBNull.Value);
                }
                reader.Read();
            }
            dataRow.EndEdit();
            dt.Rows.Add(dataRow);
        }
Example #4
0
        private static Type GetColumnDataType(JsonReader reader)
        {
            JsonToken tokenType = reader.TokenType;

            switch (tokenType)
            {
            case JsonToken.StartArray:
            {
                reader.Read();
                if (reader.TokenType == JsonToken.StartObject)
                {
                    return(typeof(DataTable));
                }
                Type columnDataType = DataTableConverter.GetColumnDataType(reader);
                return(columnDataType.MakeArrayType());
            }

            case JsonToken.Integer:
                return(typeof(long));

            case JsonToken.Float:
                return(typeof(double));

            case JsonToken.String:
            case JsonToken.Null:
            case JsonToken.Undefined:
                return(typeof(string));

            case JsonToken.Boolean:
                return(typeof(bool));

            case JsonToken.Date:
                return(typeof(DateTime));
            }
            throw new JsonException("Unexpected JSON token while reading DataTable: {0}".FormatWith(CultureInfo.InvariantCulture, tokenType));
        }
Example #5
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            DataTable dataTable;

            if (reader.TokenType == JsonToken.PropertyName)
            {
                dataTable = new DataTable((string)reader.Value);
                reader.Read();
            }
            else
            {
                dataTable = new DataTable();
            }
            reader.Read();
            while (reader.TokenType == JsonToken.StartObject)
            {
                DataRow row = dataTable.NewRow();
                reader.Read();
                while (reader.TokenType == JsonToken.PropertyName)
                {
                    string index = (string)reader.Value;
                    reader.Read();
                    if (!dataTable.Columns.Contains(index))
                    {
                        Type columnDataType = DataTableConverter.GetColumnDataType(reader.TokenType);
                        dataTable.Columns.Add(new DataColumn(index, columnDataType));
                    }
                    row[index] = reader.Value ?? (object)DBNull.Value;
                    reader.Read();
                }
                row.EndEdit();
                dataTable.Rows.Add(row);
                reader.Read();
            }
            return((object)dataTable);
        }
Example #6
0
        private static void CreateRow(JsonReader reader, DataTable dt, JsonSerializer serializer)
        {
            object  value;
            DataRow dataRow = dt.NewRow();

            reader.ReadAndAssert();
            while (reader.TokenType == JsonToken.PropertyName)
            {
                string str = (string)reader.Value;
                reader.ReadAndAssert();
                DataColumn item = dt.Columns[str];
                if (item == null)
                {
                    item = new DataColumn(str, DataTableConverter.GetColumnDataType(reader));
                    dt.Columns.Add(item);
                }
                if (item.DataType == typeof(DataTable))
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.ReadAndAssert();
                    }
                    DataTable dataTable = new DataTable();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        DataTableConverter.CreateRow(reader, dataTable, serializer);
                        reader.ReadAndAssert();
                    }
                    dataRow[str] = dataTable;
                }
                else if (!item.DataType.IsArray || !(item.DataType != typeof(byte[])))
                {
                    if (reader.Value != null)
                    {
                        value = serializer.Deserialize(reader, item.DataType);
                        if (value == null)
                        {
                            value = DBNull.Value;
                        }
                    }
                    else
                    {
                        value = DBNull.Value;
                    }
                    dataRow[str] = value;
                }
                else
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.ReadAndAssert();
                    }
                    List <object> objs = new List <object>();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        objs.Add(reader.Value);
                        reader.ReadAndAssert();
                    }
                    Array arrays = Array.CreateInstance(item.DataType.GetElementType(), objs.Count);
                    ((ICollection)objs).CopyTo(arrays, 0);
                    dataRow[str] = arrays;
                }
                reader.ReadAndAssert();
            }
            dataRow.EndEdit();
            dt.Rows.Add(dataRow);
        }