Esempio n. 1
0
    private static void ReadRows(IParser parser, DataTable table)
    {
        var columns = parser.Expect <Scalar>();

        if (columns.Value != "rows")
        {
            throw new YamlException(columns.Start, columns.End,
                                    "Expected a scalar named 'rows'");
        }
        parser.Expect <SequenceStart>();
        while (parser.Allow <SequenceEnd>() == null)
        {
            var row         = table.NewRow();
            var columnIndex = 0;
            parser.Expect <SequenceStart>();
            while (parser.Allow <SequenceEnd>() == null)
            {
                var value      = parser.Expect <Scalar>();
                var columnType = table.Columns[columnIndex].DataType;
                row[columnIndex] = TypeConverter.ChangeType(value.Value, columnType);
                ++columnIndex;
            }
            table.Rows.Add(row);
        }
    }
        public object Deserialize(IParser parser, Type type)
        {
            if (parser == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            bool   flag   = parser.Allow <StreamStart>() != null;
            bool   flag2  = parser.Allow <DocumentStart>() != null;
            object result = null;

            if (!parser.Accept <DocumentEnd>() && !parser.Accept <StreamEnd>())
            {
                using (SerializerState serializerState = new SerializerState())
                {
                    result = valueDeserializer.DeserializeValue(parser, type, serializerState, valueDeserializer);
                    serializerState.OnDeserialization();
                }
            }
            if (flag2)
            {
                parser.Expect <DocumentEnd>();
            }
            if (flag)
            {
                parser.Expect <StreamEnd>();
            }
            return(result);
        }
Esempio n. 3
0
        private PSNoteProperty ReadNoteProperty(IParser parser)
        {
            var name = parser.Allow <Scalar>().Value;

            if (parser.Accept <SequenceStart>())
            {
                parser.MoveNext();

                var values = new List <PSObject>();

                while (!parser.Accept <SequenceEnd>())
                {
                    if (parser.Accept <MappingStart>())
                    {
                        values.Add(PSObject.AsPSObject(ReadYaml(parser, typeof(PSObject))));
                    }
                    else if (parser.Accept <Scalar>())
                    {
                        values.Add(PSObject.AsPSObject(parser.Allow <Scalar>().Value));
                    }
                }
                parser.MoveNext();
                return(new PSNoteProperty(name, values.ToArray()));
            }
            else if (parser.Accept <MappingStart>())
            {
                return(new PSNoteProperty(name, ReadYaml(parser, typeof(PSObject))));
            }
            else if (parser.Accept <Scalar>())
            {
                return(new PSNoteProperty(name, parser.Allow <Scalar>().Value));
            }
            return(null);
        }
Esempio n. 4
0
        public object ReadYaml(IParser parser, Type type)
        {
            var result = new FieldMap();

            if (parser.Accept <MappingStart>())
            {
                parser.MoveNext();
                while (!parser.Accept <MappingEnd>())
                {
                    var fieldName = parser.Allow <Scalar>().Value;
                    if (parser.Accept <SequenceStart>())
                    {
                        parser.MoveNext();
                        var fields = new List <string>();

                        while (!parser.Accept <SequenceEnd>())
                        {
                            fields.Add(parser.Allow <Scalar>().Value);
                        }

                        result.Set(fieldName, fields.ToArray());
                        parser.MoveNext();
                    }
                }
                parser.MoveNext();
            }
            return(result);
        }
        internal static void SetProperty(IParser parser, PhasesTemplateReference reference, Scalar scalar)
        {
            if (String.Equals(scalar.Value, YamlConstants.Phases, StringComparison.Ordinal))
            {
                parser.Expect<SequenceStart>();
                var selectors = new List<PhaseSelector>();
                while (parser.Allow<SequenceEnd>() == null)
                {
                    var selector = new PhaseSelector();
                    parser.Expect<MappingStart>();
                    ReadExactString(parser, YamlConstants.Name);
                    selector.Name = ReadNonEmptyString(parser);
                    while (parser.Allow<MappingEnd>() == null)
                    {
                        scalar = parser.Expect<Scalar>();
                        SetProperty(parser, selector, scalar);
                    }
                }

                reference.PhaseSelectors = selectors;
            }
            else
            {
                SetProperty(parser, reference as StepsTemplateReference, scalar);
            }
        }
Esempio n. 6
0
        internal static ServerTarget ReadServerTarget(IParser parser)
        {
            // Handle the simple case "server: true"
            Scalar scalar = parser.Peek <Scalar>();

            if (scalar != null)
            {
                if (ReadBoolean(parser))
                {
                    return(new ServerTarget());
                }

                return(null);
            }

            var result = new ServerTarget();

            parser.Expect <MappingStart>();
            while (parser.Allow <MappingEnd>() == null)
            {
                scalar = parser.Expect <Scalar>();
                switch (scalar.Value ?? String.Empty)
                {
                case YamlConstants.ContinueOnError:
                    result.ContinueOnError = ReadNonEmptyString(parser);
                    break;

                case YamlConstants.Matrix:
                    parser.Expect <MappingStart>();
                    result.Matrix = new Dictionary <String, IDictionary <String, String> >(StringComparer.OrdinalIgnoreCase);
                    while (parser.Allow <MappingEnd>() == null)
                    {
                        String key = ReadNonEmptyString(parser);
                        result.Matrix[key] = ReadMappingOfStringString(parser, StringComparer.OrdinalIgnoreCase);
                    }

                    break;

                case YamlConstants.Parallel:
                    result.Parallel = ReadNonEmptyString(parser);
                    break;

                case YamlConstants.TimeoutInMinutes:
                    result.TimeoutInMinutes = ReadNonEmptyString(parser);
                    break;

                default:
                    throw new SyntaxErrorException(scalar.Start, scalar.End, $"Unexpected property: '{scalar.Value}'");
                }
            }

            return(result);
        }
Esempio n. 7
0
        public object ReadYaml(IParser parser, Type type)
        {
            var result = new SuppressionRule();

            if (parser.Accept <SequenceStart>())
            {
                parser.MoveNext();

                var targetNames = new List <string>();

                while (!parser.Accept <SequenceEnd>())
                {
                    targetNames.Add(parser.Allow <Scalar>().Value);
                }

                result.TargetName = targetNames.ToArray();

                parser.MoveNext();
            }
            else if (parser.Accept <MappingStart>())
            {
                parser.MoveNext();

                while (!parser.Accept <MappingEnd>())
                {
                    var name = parser.Allow <Scalar>().Value;

                    if (name == "targetName" && parser.Accept <SequenceStart>())
                    {
                        parser.MoveNext();

                        var targetNames = new List <string>();

                        while (!parser.Accept <SequenceEnd>())
                        {
                            targetNames.Add(parser.Allow <Scalar>().Value);
                        }

                        result.TargetName = targetNames.ToArray();

                        parser.MoveNext();
                    }
                }

                parser.MoveNext();
            }

            return(result);
        }
        bool INodeDeserializer.Deserialize(IParser reader, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value)
        {
            var mapping = reader.Allow <MappingStart>();

            if (mapping == null)
            {
                value = null;
                return(false);
            }

            value = _objectFactory.Create(expectedType);
            while (!reader.Accept <MappingEnd>())
            {
                var propertyName = reader.Expect <Scalar>();
                var property     = _typeDescriptor.GetProperty(expectedType, value, propertyName.Value, _ignoreUnmatched);
                if (property == null)
                {
                    reader.SkipThisAndNestedEvents();
                    continue;
                }

                var propertyValue = nestedObjectDeserializer(reader, property.Type);
                if (!(propertyValue is IValuePromise propertyValuePromise))
                {
                    var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type);
                    property.Write(value, convertedValue);
                }
Esempio n. 9
0
        public Object ReadYaml(IParser parser, Type type)
        {
            var result = new PhasesTemplate();

            parser.Expect <MappingStart>();
            while (parser.Allow <MappingEnd>() == null)
            {
                Scalar scalar = parser.Expect <Scalar>();
                switch (scalar.Value ?? String.Empty)
                {
                //
                // Phases template properties
                //

                case YamlConstants.Phases:
                    ConverterUtil.ValidateNull(result.Steps, YamlConstants.Steps, YamlConstants.Phases, scalar);
                    result.Phases = ConverterUtil.ReadPhases(parser, simpleOnly: true);
                    break;

                //
                // Steps template properties
                //

                case YamlConstants.Steps:
                    ConverterUtil.ValidateNull(result.Phases, YamlConstants.Phases, YamlConstants.Steps, scalar);
                    result.Steps = ConverterUtil.ReadSteps(parser, simpleOnly: false);
                    break;

                default:
                    throw new SyntaxErrorException(scalar.Start, scalar.End, $"Unexpected process property: '{scalar.Value}'");
                }
            }

            return(result);
        }
Esempio n. 10
0
        public object ReadYaml(IParser parser, Type type)
        {
            // Handle empty objects
            if (parser.Accept <Scalar>())
            {
                parser.Allow <Scalar>();
                return(null);
            }

            var result = new PSObject();

            if (parser.Accept <MappingStart>())
            {
                parser.MoveNext();
                while (!parser.Accept <MappingEnd>())
                {
                    var property = ReadNoteProperty(parser);
                    if (property == null)
                    {
                        throw new NotImplementedException();
                    }

                    result.Properties.Add(property);
                }
                parser.MoveNext();
            }
            return(result);
        }
Esempio n. 11
0
        public object ReadYaml(IParser parser, Type type)
        {
            RuleFilter filter = null;

            parser.Expect <MappingStart>();
            while (parser.Allow <MappingEnd>() == null)
            {
                var filterType  = parser.Expect <Scalar>().Value;
                var filterValue = parser.Expect <Scalar>().Value;

                switch (filterType)
                {
                case "processName":
                    filter = new ProcessNameRuleFilter(filterValue);
                    break;

                case "processPath":
                    filter = new ProcessImagePathRuleFilter(filterValue);
                    break;

                case "serviceName":
                    filter = new WindowsServiceNameRuleFilter(filterValue);
                    break;
                }
            }

            return(filter);
        }
Esempio n. 12
0
        internal static IList <ProcessResource> ReadProcessResources(IParser parser)
        {
            var result = new List <ProcessResource>();

            parser.Expect <SequenceStart>();
            while (parser.Allow <SequenceEnd>() == null)
            {
                parser.Expect <MappingStart>();
                Scalar scalar = parser.Expect <Scalar>();
                switch (scalar.Value ?? String.Empty)
                {
                case YamlConstants.Endpoint:
                case YamlConstants.Repo:
                    break;

                default:
                    throw new SyntaxErrorException(scalar.Start, scalar.End, $"Unexpected resource type: '{scalar.Value}'");
                }

                var resource = new ProcessResource {
                    Type = scalar.Value
                };
                resource.Name = ReadNonEmptyString(parser);;
                while (parser.Allow <MappingEnd>() == null)
                {
                    string dataKey = ReadNonEmptyString(parser);
                    if (parser.Accept <MappingStart>())
                    {
                        resource.Data[dataKey] = ReadMapping(parser);
                    }
                    else if (parser.Accept <SequenceStart>())
                    {
                        resource.Data[dataKey] = ReadSequence(parser);
                    }
                    else
                    {
                        resource.Data[dataKey] = parser.Expect <Scalar>().Value ?? String.Empty;
                    }
                }

                result.Add(resource);
            }

            return(result);
        }
Esempio n. 13
0
        private IResource MapResource(IParser reader, Func <IParser, Type, object> nestedObjectDeserializer, CommentMetadata comment)
        {
            IResource        result   = null;
            string           kind     = null;
            ResourceMetadata metadata = null;

            if (reader.Accept <MappingStart>())
            {
                reader.MoveNext();
                while (!reader.Accept <MappingEnd>())
                {
                    // Read kind
                    var propertyName = reader.Allow <Scalar>().Value;

                    if (propertyName == "kind")
                    {
                        kind = reader.Allow <Scalar>().Value;
                    }
                    else if (propertyName == "metadata")
                    {
                        if (!TryMetadata(reader, nestedObjectDeserializer, out metadata))
                        {
                            reader.SkipThisAndNestedEvents();
                        }
                    }
                    else if (propertyName == "spec" && kind != null)
                    {
                        if (!TryResource(kind, reader, nestedObjectDeserializer, metadata, comment, out IResource resource))
                        {
                            reader.SkipThisAndNestedEvents();
                        }

                        result = resource;
                    }
                    else
                    {
                        reader.SkipThisAndNestedEvents();
                    }
                }
                reader.MoveNext();
            }
            return(result);
        }
Esempio n. 14
0
            public void Read(IParser parser, Type expectedType, ObjectDeserializer nestedObjectDeserializer)
            {
                var comment = parser.Allow <Comment>();

                if (comment != null)
                {
                    Comment = comment.Value;
                }

                Value = (T)nestedObjectDeserializer(typeof(T));
            }
        public object ReadYaml(IParser parser, Type type)
        {
            var speciesName = parser.Allow <Scalar>().Value;

            if (speciesName is null)
            {
                return(null);
            }

            return(new Species(speciesName, "Dummy"));
        }
        internal static IList<IPhase> ReadPhases(IParser parser, Boolean simpleOnly)
        {
            var result = new List<IPhase>();
            parser.Expect<SequenceStart>();
            while (parser.Allow<SequenceEnd>() == null)
            {
                result.Add(ReadPhase(parser, simpleOnly));
            }

            return result;
        }
        internal static IList <String> ReadSequenceOfString(IParser parser)
        {
            parser.Expect <SequenceStart>();
            var sequence = new List <String>();

            while (parser.Allow <SequenceEnd>() == null)
            {
                sequence.Add(parser.Expect <Scalar>().Value);
            }

            return(sequence);
        }
        internal static IList <IStep> ReadSteps(IParser parser, Boolean simpleOnly = false)
        {
            var result = new List <IStep>();

            parser.Expect <SequenceStart>();
            while (parser.Allow <SequenceEnd>() == null)
            {
                result.Add(ReadStep(parser, simpleOnly));
            }

            return(result);
        }
Esempio n. 19
0
        public object DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
        {
            object value;
            var    alias = parser.Allow <AnchorAlias>();

            if (alias != null)
            {
                var          aliasState = state.Get <AliasState>();
                ValuePromise valuePromise;
                if (!aliasState.TryGetValue(alias.Value, out valuePromise))
                {
                    valuePromise = new ValuePromise(alias);
                    aliasState.Add(alias.Value, valuePromise);
                }

                return(valuePromise.HasValue ? valuePromise.Value : valuePromise);
            }

            string anchor = null;

            var nodeEvent = parser.Peek <NodeEvent>();

            if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
            {
                anchor = nodeEvent.Anchor;
            }

            value = innerDeserializer.DeserializeValue(parser, expectedType, state, nestedObjectDeserializer);

            if (anchor != null)
            {
                var aliasState = state.Get <AliasState>();

                ValuePromise valuePromise;
                if (!aliasState.TryGetValue(anchor, out valuePromise))
                {
                    aliasState.Add(anchor, new ValuePromise(value));
                }
                else if (!valuePromise.HasValue)
                {
                    valuePromise.Value = value;
                }
                else
                {
                    throw new DuplicateAnchorException(nodeEvent.Start, nodeEvent.End, string.Format(
                                                           "Anchor '{0}' already defined",
                                                           anchor
                                                           ));
                }
            }

            return(value);
        }
Esempio n. 20
0
        private static void ReadCommand(IParser parser, FileContainer.Message.Page page)
        {
            parser.Expect <MappingStart>();

            var    cmdType    = parser.Expect <Scalar>();
            var    type       = (CommandType)Enum.Parse(typeof(CommandType), cmdType.Value);
            string symbol     = string.Empty;
            var    parameters = new List <string>();

            switch (type)
            {
            case CommandType.UNKNOWN_TYPE:
            case CommandType.Other:
                parser.Expect <SequenceStart>();
                symbol = parser.Expect <Scalar>().Value;
                while (parser.Allow <SequenceEnd>() == null)
                {
                    parameters.Add(parser.Expect <Scalar>().Value);
                }
                break;

            case CommandType.Format:
            case CommandType.PageEnd:
                symbol = parser.Expect <Scalar>().Value;
                break;

            default:
                parser.Expect <SequenceStart>();
                while (parser.Allow <SequenceEnd>() == null)
                {
                    parameters.Add(parser.Expect <Scalar>().Value);
                }
                break;
            }

            var cmd = new Command(symbol, parameters.ToArray(), type);

            page.Commands.Add(cmd);
            parser.Expect <MappingEnd>();
        }
Esempio n. 21
0
        /// <summary>
        /// Deserializes an object of the specified type.
        /// </summary>
        /// <param name="parser">The <see cref="IParser" /> where to deserialize the object.</param>
        /// <param name="type">The static type of the object to deserialize.</param>
        /// <returns>Returns the deserialized object.</returns>
        public object Deserialize(IParser parser, Type type, IValueDeserializer deserializer = null)
        {
            if (parser == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var hasStreamStart = parser.Allow <StreamStart>() != null;

            var hasDocumentStart = parser.Allow <DocumentStart>() != null;

            deserializer = deserializer ?? _valueDeserializer;
            object result = null;

            if (!parser.Accept <DocumentEnd>() && !parser.Accept <StreamEnd>())
            {
                using (var state = new SerializerState())
                {
                    result = deserializer.DeserializeValue(parser, type, state, deserializer);
                    state.OnDeserialization();
                }
            }

            if (hasDocumentStart)
            {
                parser.Expect <DocumentEnd>();
            }

            if (hasStreamStart)
            {
                parser.Expect <StreamEnd>();
            }

            return(result);
        }
Esempio n. 22
0
        /// <summary>
        /// Ensures that the current event is of the specified type, returns it and moves to the next event.
        /// </summary>
        /// <typeparam name="T">Type of the <see cref="ParsingEvent"/>.</typeparam>
        /// <returns>Returns the current event.</returns>
        /// <exception cref="YamlException">If the current event is not of the specified type.</exception>
        public static T Expect <T>(this IParser parser) where T : ParsingEvent
        {
            var expectedEvent = parser.Allow <T>();

            if (expectedEvent == null)
            {
                // TODO: Throw a better exception
                var @event = parser.Current;
                throw new YamlException(@event.Start, @event.End, string.Format(CultureInfo.InvariantCulture,
                                                                                "Expected '{0}', got '{1}' (at {2}).", typeof(T).Name, @event.GetType().Name, @event.Start));
            }
            return(expectedEvent);
        }
        internal static IDictionary <String, IList <ISimpleStep> > ReadStepOverrides(IParser parser)
        {
            var result = new Dictionary <String, IList <ISimpleStep> >();

            parser.Expect <MappingStart>();
            while (parser.Allow <MappingEnd>() == null)
            {
                String key = ReadNonEmptyString(parser);
                result[key] = ReadSteps(parser, simpleOnly: true).Cast <ISimpleStep>().ToList();
            }

            return(result);
        }
Esempio n. 24
0
            public object DeserializeValue(IParser reader, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
            {
                object value;
                var    alias = reader.Allow <AnchorAlias>();

                if (alias != null)
                {
                    var          aliasState = state.Get <AliasState>();
                    ValuePromise valuePromise;
                    if (!aliasState.TryGetValue(alias.Value, out valuePromise))
                    {
                        valuePromise = new ValuePromise(alias);
                        aliasState.Add(alias.Value, valuePromise);
                    }

                    return(valuePromise.HasValue ? valuePromise.Value : valuePromise);
                }

                string anchor = null;

                var nodeEvent = reader.Peek <NodeEvent>();

                if (nodeEvent != null && !string.IsNullOrEmpty(nodeEvent.Anchor))
                {
                    anchor = nodeEvent.Anchor;
                }

                value = _innerDeserializer.DeserializeValue(reader, expectedType, state, nestedObjectDeserializer);

                if (anchor != null)
                {
                    var aliasState = state.Get <AliasState>();

                    ValuePromise valuePromise;
                    if (!aliasState.TryGetValue(anchor, out valuePromise))
                    {
                        aliasState.Add(anchor, new ValuePromise(value));
                    }
                    else if (!valuePromise.HasValue)
                    {
                        valuePromise.Value = value;
                    }
                    else
                    {
                        aliasState[anchor] = new ValuePromise(value);
                    }
                }

                return(value);
            }
Esempio n. 25
0
        public object ReadYaml(IParser parser, Type type)
        {
            var list = new GameRuleList();

            parser.Expect <MappingStart>();
            while (parser.Allow <MappingEnd>() == null)
            {
                var ruleName  = parser.Expect <Scalar>().Value;
                var ruleValue = parser.Expect <Scalar>().Value;
                list.Add(ruleName, ParseGameRuleObject(ruleValue));
            }

            return(list);
        }
        public bool Deserialize(IParser reader, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value)
        {
            if (reader == null)
            {
                value = null;
                return(false);
            }

            // only try this if we're targeting a boolean or an untyped object
            if (expectedType == typeof(object) || expectedType == typeof(bool))
            {
                // peek at the current token
                Scalar scalar = reader.Peek <Scalar>();

                // if it's unquoted
                if (scalar != null && scalar.Style == ScalarStyle.Plain)
                {
                    // and the value is actually true or false
                    switch (scalar.Value.ToUpperInvariant())
                    {
                    case "TRUE":
                        value = true;
                        reader.Allow <Scalar>();
                        return(true);

                    case "FALSE":
                        value = false;
                        reader.Allow <Scalar>();
                        return(true);
                    }
                }
            }

            // otherwise, fall thru
            value = null;
            return(false);
        }
Esempio n. 27
0
        internal static ProcessTemplateReference ReadProcessTemplateReference(IParser parser)
        {
            parser.Expect <MappingStart>();
            ReadExactString(parser, YamlConstants.Name);
            var result = new ProcessTemplateReference {
                Name = ReadNonEmptyString(parser)
            };

            while (parser.Allow <MappingEnd>() == null)
            {
                Scalar scalar = parser.Expect <Scalar>();
                SetProperty(parser, result, scalar);
            }

            return(result);
        }
Esempio n. 28
0
        public object ReadYaml([NotNull] IParser parser, [NotNull] Type type)
        {
            var rawColour = parser.Allow <Scalar>().Value;

            if (rawColour is null)
            {
                return(null);
            }

            if (!Colour.TryParse(rawColour, out var value))
            {
                throw new ArgumentException("Failed to parse a valid colour.");
            }

            return(value);
        }
Esempio n. 29
0
        public object ReadYaml(IParser parser, Type type)
        {
            var segments = new List <Tuple <int, int> >();

            parser.Expect <SequenceStart>();
            while (parser.Allow <SequenceEnd>() == null)
            {
                //TODO: tuple converter
                parser.Expect <SequenceStart>();
                var v1 = parser.Expect <Scalar>().Value;
                var v2 = parser.Expect <Scalar>().Value;
                parser.Expect <SequenceEnd>();
                segments.Add(Tuple.Create(int.Parse(v1), int.Parse(v2)));
            }
            return(new PiecewiseLinearFunction(segments));
        }
Esempio n. 30
0
        public void Read(IParser parser, Type expectedType, ObjectDeserializer nestedObjectDeserializer)
        {
            var scalar = parser.Allow <Scalar>();

            if (scalar != null)
            {
                var parsedValue = scalar.Value;
                var data        = parsedValue.Split('=');
                name  = data[0];
                value = data[1];
            }
            else
            {
                Console.WriteLine(nestedObjectDeserializer(typeof(string)));
            }
        }
Esempio n. 31
0
        bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
        {
            var mapping = parser.Allow<MappingStart>();
            if (mapping == null)
            {
                value = null;
                return false;
            }

            value = _objectFactory.Create(expectedType);
            while (!parser.Accept<MappingEnd>())
            {
                var propertyName = parser.Expect<Scalar>();
                var property = _typeDescriptor.GetProperty(expectedType, null, propertyName.Value, _ignoreUnmatched);
                if (property == null)
                {
                    parser.SkipThisAndNestedEvents();
                    continue;
                }

                var propertyValue = nestedObjectDeserializer(parser, property.Type);
                var propertyValuePromise = propertyValue as IValuePromise;
                if (propertyValuePromise == null)
                {
                    var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type);
                    property.Write(value, convertedValue);
                }
                else
                {
                    var valueRef = value;
                    propertyValuePromise.ValueAvailable += v =>
                    {
                        var convertedValue = TypeConverter.ChangeType(v, property.Type);
                        property.Write(valueRef, convertedValue);
                    };
                }
            }

            parser.Expect<MappingEnd>();
            return true;
        }
Esempio n. 32
0
        bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
        {
            var scalar = parser.Allow<Scalar>();
            if (scalar == null)
            {
                value = null;
                return false;
            }

            if (expectedType.IsEnum())
            {
                value = Enum.Parse(expectedType, scalar.Value, true);
            }
            else
            {
                var typeCode = expectedType.GetTypeCode();
                switch (typeCode)
                {
                    case TypeCode.Boolean:
                        value = DeserializeBooleanHelper(scalar.Value);
                        break;

                    case TypeCode.Byte:
                    case TypeCode.Int16:
                    case TypeCode.Int32:
                    case TypeCode.Int64:
                    case TypeCode.SByte:
                    case TypeCode.UInt16:
                    case TypeCode.UInt32:
                    case TypeCode.UInt64:
                        value = DeserializeIntegerHelper(typeCode, scalar.Value);
                        break;

                    case TypeCode.Single:
                        value = Single.Parse(scalar.Value, YamlFormatter.NumberFormat);
                        break;

                    case TypeCode.Double:
                        value = Double.Parse(scalar.Value, YamlFormatter.NumberFormat);
                        break;

                    case TypeCode.Decimal:
                        value = Decimal.Parse(scalar.Value, YamlFormatter.NumberFormat);
                        break;

                    case TypeCode.String:
                        value = scalar.Value;
                        break;

                    case TypeCode.Char:
                        value = scalar.Value[0];
                        break;

                    case TypeCode.DateTime:
                        // TODO: This is probably incorrect. Use the correct regular expression.
                        value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture);
                        break;

                    default:
                        if (expectedType == typeof(object))
                        {
                            // Default to string
                            value = scalar.Value;
                        }
                        else
                        {
                            value = TypeConverter.ChangeType(scalar.Value, expectedType);
                        }
                        break;
                }
            }
            return true;
        }