private static string ConvertCollection(Type ctype, IEnumerable data)
        {
            var json = new StringBuilder("[", JsonConverter.StringBuilderCapacity);             // collection type
            var type = Common.TryGetSerializationItemType(ctype, data, Text.Method.ToJson);     // item type

            CollectionType      collectionType;
            ClrMappingInfo      clrInfo = null;
            Type                clrType;
            QueryTalkException  exception;
            List <JsonProperty> jsonProperties = null;

            // empty collection
            if (type == null)
            {
                collectionType = CollectionType.Object;
                data           = null;
            }
            else if (type == typeof(System.Object))
            {
                collectionType = CollectionType.Object;
            }
            else if (type == typeof(Value))
            {
                collectionType = CollectionType.Value;
            }
            // CLR compliant scalar type
            else if (Mapping.CheckClrCompliance(type, out clrType, out exception) == Mapping.ClrTypeMatch.ClrMatch)
            {
                collectionType = CollectionType.Clr;
                clrInfo        = Mapping.ClrMapping[clrType];
            }
            // class
            else
            {
                collectionType = CollectionType.Class;
                jsonProperties = ReflectClass(type);
            }

            var first = true;

            if (data != null)
            {
                foreach (var row in data)
                {
                    if (!first)
                    {
                        json.Append(",");
                    }

                    if (row.IsUndefined())
                    {
                        json.Append(Text.ClrNull);
                        first = false;
                        continue;
                    }

                    switch (collectionType)
                    {
                    case CollectionType.Object:
                        var rowType = row.GetType();
                        if (Mapping.CheckClrCompliance(rowType, out clrType, out exception) != Mapping.ClrTypeMatch.ClrMatch)
                        {
                            throw exception;
                        }
                        clrInfo = Mapping.ClrMapping[rowType];
                        json.Append(clrInfo.ToJson(row));
                        break;

                    case CollectionType.Value:
                        var o = ((Value)row).Original;
                        clrInfo = Mapping.ClrMapping[o.GetType()];
                        json.Append(clrInfo.ToJson(o));
                        break;

                    case CollectionType.Clr:
                        json.Append(clrInfo.ToJson(row));
                        break;

                    // class
                    default:
                        json.Append(ConvertClass(row, jsonProperties));
                        break;
                    }

                    first = false;
                }
            }

            json.Append("]");
            return(json.ToString());
        }
示例#2
0
        private static ColumnInfo[] ReflectReader(IDataRecord reader, out QueryTalkException exception)
        {
            exception = null;
            var       columns      = new List <Loader.ColumnInfo>();
            DataTable readerSchema = ((SqlDataReader)reader).GetSchemaTable();

            int           i           = 0;
            int           index       = 1;
            List <string> columnNames = new List <string>();

            foreach (var row in readerSchema.AsEnumerable())
            {
                Loader.ColumnInfo column = new ColumnInfo();
                column.ColumnName = Naming.GetClrName((string)row[_columnNameText]);

                if (String.IsNullOrEmpty(column.ColumnName))
                {
                    column.ColumnName = GetColumnName(readerSchema, ref index);
                }

                column.ReaderOrdinal = (int)row[_columnOrdinalText];
                column.ValueType     = (Type)row[_dataTypeText];

                Type clrType;
                if (Mapping.CheckClrCompliance(column.ValueType, out clrType, out exception) != Mapping.ClrTypeMatch.ClrMatch)
                {
                    exception = null;
                    continue;
                }

                if ((bool)row[_allowDBNullText] && !column.ValueType.IsClass)
                {
                    column.IsNullable   = true;
                    column.NullableType = Mapping.ClrMapping[column.ValueType].NullableType;
                }

                ClrMappingInfo mapping = Mapping.ClrMapping[column.ValueType];
                column.ReaderGetMethod = typeof(SqlDataReader).GetMethod(mapping.SqlDataReaderGetMethodName,
                                                                         new Type[] { typeof(int) });
                column.IsUnbox           = mapping.IsUnbox;
                column.CtorParameterType = mapping.CtorParameterType;

                // provide unique column name
                int j       = 1;
                var renamed = column.ColumnName;
                while (columnNames.Contains(renamed))
                {
                    renamed = String.Format("{0}{1}{2}",
                                            column.ColumnName,
                                            Text.Free.Renamed,
                                            j > 1 ? j.ToString() : String.Empty);
                    ++j;
                }
                if (j > 1)
                {
                    column.ColumnName = renamed;
                }

                columns.Add(column);
                columnNames.Add(column.ColumnName);
                ++i;
            }

            return(columns.ToArray());
        }
示例#3
0
 private static void _AppendValue(StringBuilder json, object value, ClrMappingInfo info, string propertyName, int indent)
 {
     json.AppendFormat("{0}:{1}{2}", propertyName.DelimitJsonString(), indent > 0 ? " " : "", info.ToJson(value));
 }