Пример #1
0
        public IEditable Create(Schema schema, bool nullable)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            if (EditablePrimitive.IsSupportedType(schema.DataType))
            {
                var x = new EditablePrimitive(this, schema, nullable);
                if (!nullable && schema.DataType == DataType.Choice && schema.DeclarationItem != null)
                {
                    var defaults = schema.DeclarationItem.NavigateTo(FieldPath.Create("Data", "Defaults"));
                    if (!defaults.IsNull && defaults.Count > 0)
                    {
                        x.Set(defaults.GoTo(0).Get <int>());
                    }
                }
                return(x);
            }

            if (schema.DataType == DataType.Class)
            {
                if (schema == Schema.BuiltIn[BuiltInSchema.Variable])
                {
                    return(new EditableVariable(this, nullable));
                }
                var x = new EditableObject(this, schema, nullable);
                if (!nullable && schema.DeclarationItem != null)
                {
                }
                return(x);
            }
            else if (schema.DataType == DataType.List || schema.DataType == DataType.MultiChoice)
            {
                var x = new EditableList(this, schema, nullable);
                if (!nullable && schema.DataType == DataType.MultiChoice && schema.DeclarationItem != null)
                {
                    var defaults = schema.DeclarationItem.NavigateTo(FieldPath.Create("Data", "Defaults"));
                    foreach (var d in defaults.Children)
                    {
                        x.Add().Set(d.Get <int>());
                    }
                }
                return(x);
            }
            else
            {
                throw new ArgumentException("Unsupported schema specified", "schema");
            }
        }
Пример #2
0
        public static void WriteTo(this ICursor source, JsonWriter writer, bool stripNullValues = true)
        {
            if (source == null || source.IsNull)
            {
                writer.WriteNull();
            }
            else
            {
                switch (source.Schema.DataType)
                {
                case DataType.Class:
                    if (source.Schema.Id == (int)BuiltInSchema.Variable)
                    {
                        var content = source.GoTo((int)VariableLayout.Data, true);
                        if (!content.IsNull &&
                            content.Schema.DataType != DataType.String &&
                            content.Schema.DataType != DataType.Boolean &&
                            (content.Schema.DataType == DataType.List ||
                             content.Schema.DataType == DataType.MultiChoice ||
                             EditablePrimitive.IsSupportedType(content.Schema.DataType)
                            )
                            )
                        {
                            writer.WriteStartObject();
                            writer.WritePropertyName("@schema");
                            writer.WriteValue(source.Schema.Name);
                            writer.WritePropertyName("DataSchema");
                            writer.WriteValue(content.Schema.Name);
                            writer.WritePropertyName("Data");
                            content.WriteTo(writer, stripNullValues);
                            writer.WriteEndObject();
                        }
                        else
                        {
                            content.WriteTo(writer, stripNullValues);
                        }
                    }
                    else
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("@schema");
                        writer.WriteValue(source.Schema.Name);
                        foreach (var f in source.Schema.Fields)
                        {
                            var field = source.GoTo(f, false);
                            if (!stripNullValues || !field.IsNull)
                            {
                                writer.WritePropertyName(f.Name);
                                field.WriteTo(writer, stripNullValues);
                            }
                        }
                        writer.WriteEndObject();
                    }
                    break;

                case DataType.MultiChoice:
                case DataType.List:
                {
                    writer.WriteStartArray();
                    for (int i = 0; i < source.Count; ++i)
                    {
                        var field = source.GoTo(i, false);
                        if (!stripNullValues || !field.IsNull)
                        {
                            field.WriteTo(writer, stripNullValues);
                        }
                    }
                    writer.WriteEndArray();
                }
                break;

                default:
                {
                    EditablePrimitive.PrimitiveType typeHelper;
                    if (EditablePrimitive.TryGetPrimitiveTypeHelper(source.Schema, out typeHelper))
                    {
                        typeHelper.JsonWrite(writer, source.Get());
                    }
                    else
                    {
                        throw new Exception(string.Format("Unable to convert data type '{0}' to JSON string.", source.Schema.DataType));
                    }
                }
                break;
                }
            }
        }
Пример #3
0
        object FromCursorInternal(ICursor cursor, Type targetType)
        {
            if (cursor == null || cursor.IsNull)
            {
                return(null);
            }

            if (EditablePrimitive.IsSupportedType(cursor.Schema.DataType))
            {
                return(cursor.Get());
            }
            else if (cursor.Schema.Id == (int)BuiltInSchema.DateTimeOffset)
            {
                return(new DateTimeOffset(cursor.GoTo("Time").Get <DateTime>(), cursor.GoTo("Offset").Get <TimeSpan>()));
            }
            else if (cursor.Schema.Id == (int)BuiltInSchema.Version)
            {
                int major    = cursor.GoTo("Major").Get <int>();
                int minor    = cursor.GoTo("Minor").Get <int>();
                int build    = cursor.GoTo("Build").Get <int>();
                int revision = cursor.GoTo("Revision").Get <int>();

                if (revision >= 0)
                {
                    return(new Version(major, minor, build, revision));
                }

                if (build >= 0)
                {
                    return(new Version(major, minor, build));
                }

                return(new Version(major, minor));
            }
            else if (cursor.Schema.Id == (int)BuiltInSchema.Item)
            {
                return(DeserializeItem(cursor));
            }

            Type type;

            if (!schemaTypeMap.TryGetTypeForSchema(cursor.Schema, out type))
            {
                if (cursor.Schema.DataType == DataType.Choice)
                {
                    return(new Choice(cursor.Schema, cursor.Get <int?>(), true));
                }
                else
                {
                    if (targetType != null && (targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditable)) || targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditableVariable))))
                    {
                        return(editableFactory.Create(cursor));
                    }

                    throw new MissingTypeMappingException(cursor.Schema);
                }
            }
            else
            {
                if (targetType != null && !targetType.GetTypeInfo().IsAssignableFrom(type))
                {
                    if (targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditable)) || targetType.GetTypeInfo().IsAssignableFrom(typeof(IEditableVariable)))
                    {
                        return(editableFactory.Create(cursor));
                    }
                    else if (targetType.IsArray && typeof(System.Collections.IEnumerable).GetTypeInfo().IsAssignableFrom(type))
                    {
                        type = targetType;
                    }
                    else
                    {
                        throw new InvalidCastException(string.Format("Cannot cast from {0} to {1}.", type.Name, targetType.Name));
                    }
                }
            }

            object obj = Construct(type, cursor);

            ApplyToObject(obj, cursor);
            return(obj);
        }