public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     PackageVersionRange packageVersion;
     if (!PackageVersionRange.TryParse(fromScalar.Value, out packageVersion))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Invalid version dependency format. Unable to decode [{0}]".ToFormat(fromScalar.Value));
     }
     return packageVersion;
 }
 public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
 {
     var parameterKey = ParameterKeys.FindByName(fromScalar.Value);
     if (parameterKey == null)
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to find registered ParameterKey [{0}]".ToFormat(fromScalar.Value));
     }
     return parameterKey;
 }
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     PackageReference packageReference;
     if (!PackageReference.TryParse(fromScalar.Value, out packageReference))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode package reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
     }
     return packageReference;
 }
        public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
        {
            Guid guid;
            if (!Guid.TryParse(fromScalar.Value, out guid))
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode asset part reference [{0}]. Expecting an ENTITY_GUID".ToFormat(fromScalar.Value));
            }

            var result = context.Instance as IdentifiableAssetPartReference ?? (IdentifiableAssetPartReference)(context.Instance = new IdentifiableAssetPartReference());
            result.Id = guid;

            return result;
        }
        public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
        {
            Guid entityGuid, componentGuid;

            if (!TryParse(fromScalar.Value, out entityGuid, out componentGuid))
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode entity component reference [{0}]. Expecting format ENTITY_GUID/COMPONENT_GUID".ToFormat(fromScalar.Value));
            }

            var result = context.Instance as EntityComponentReference ?? (EntityComponentReference)(context.Instance = new EntityComponentReference());
            result.Entity = new EntityComponentReference.EntityReference { Id = entityGuid };
            result.Id = componentGuid;
            return result;
        }
        public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
        {
            AssetId guid;
            UFile location;
            Guid referenceId;
            if (!AssetReference.TryParse(fromScalar.Value, out guid, out location, out referenceId))
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode asset reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
            }

            var instance = AttachedReferenceManager.CreateProxyObject(context.Descriptor.Type, guid, location);
            if (referenceId != Guid.Empty)
            {
                IdentifiableHelper.SetId(instance, referenceId);
            }
            return instance;
        }
        public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
        {
            var lastDot = fromScalar.Value.LastIndexOf('.');
            if (lastDot == -1)
                return null;

            var className = fromScalar.Value.Substring(0, lastDot);

            bool alias;
            var containingClass = objectContext.SerializerContext.TypeFromTag("!" + className, out alias); // Readd initial '!'
            if (containingClass == null)
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to find class from tag [{0}]".ToFormat(className));
            }

            var propertyName = fromScalar.Value.Substring(lastDot + 1);
            var propertyField = containingClass.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
            if (propertyField == null)
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to find property [{0}] in class [{1}]".ToFormat(propertyName, containingClass.Name));
            }

            return propertyField.GetValue(null);
        }
        /// <inheritdoc/>
        public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
        {
            var idIndex = fromScalar.Value.IndexOf('~');
            var id = ItemId.Empty;
            var keyString = fromScalar.Value;
            if (idIndex >= 0)
            {
                var idString = fromScalar.Value.Substring(0, idIndex);
                keyString = fromScalar.Value.Substring(idIndex + 1);
                id = ItemId.Parse(idString);
            }
            var keyType = objectContext.Descriptor.Type.GetGenericArguments()[0];
            var keyDescriptor = objectContext.SerializerContext.FindTypeDescriptor(keyType);
            var keySerializer = objectContext.SerializerContext.Serializer.GetSerializer(objectContext.SerializerContext, keyDescriptor);
            var scalarKeySerializer = keySerializer as ScalarSerializerBase;
            // TODO: deserialize non-scalar keys!
            if (scalarKeySerializer == null)
                throw new InvalidOperationException("Non-scalar key not yet supported!");

            var context = new ObjectContext(objectContext.SerializerContext, null, keyDescriptor);
            var key = scalarKeySerializer.ConvertFrom(ref context, new Scalar(keyString));
            var result = Activator.CreateInstance(typeof(KeyWithId<>).MakeGenericType(keyType), id, key);
            return result;
        }
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     return new System.Uri(fromScalar.Value);
 }
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     ObjectId objectId;
     ObjectId.TryParse(fromScalar.Value, out objectId);
     return objectId;
 }
예제 #11
0
 /// <inheritdoc/>
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     ObjectId id;
     ObjectId.TryParse(fromScalar.Value, out id);
     return new ItemId(id);
 }
예제 #12
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     AssetId assetId;
     AssetId.TryParse(fromScalar.Value, out assetId);
     return assetId;
 }
예제 #13
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     Guid guid;
     Guid.TryParse(fromScalar.Value, out guid);
     return guid;
 }
예제 #14
0
 private string EmitScalar(Scalar scalar)
 {
     return Emit(
         new SequenceStart(null, null, false, DataStyle.Normal),
         scalar,
         new SequenceEnd()
         );
 }
예제 #15
0
 /// <summary>
 /// Gets the default tag and value for the specified <see cref="Scalar" />. The default tag can be different from a actual tag of this <see cref="NodeEvent" />.
 /// </summary>
 /// <param name="scalar">The scalar event.</param>
 /// <param name="defaultTag">The default tag decoded from the scalar.</param>
 /// <param name="value">The value extracted from a scalar.</param>
 /// <returns>System.String.</returns>
 public bool TryParseScalar(Scalar scalar, out string defaultTag, out object value)
 {
     return Settings.Schema.TryParse(scalar, true, out defaultTag, out value);
 }
        public override object ConvertFrom(ref ObjectContext context, Scalar scalar)
        {
            var primitiveType = (PrimitiveDescriptor) context.Descriptor;
            var type = primitiveType.Type;
            var text = scalar.Value;

            // Return null if expected type is an object and scalar is null
            if (text == null)
            {
                switch (Type.GetTypeCode(type))
                {
                    case TypeCode.Object:
                    case TypeCode.Empty:
                    case TypeCode.String:
                        return null;
                    default:
                        // TODO check this
                        throw new YamlException(scalar.Start, scalar.End, "Unexpected null scalar value");
                }
            }

            // If type is an enum, try to parse it
            if (type.IsEnum)
            {
                bool enumRemapped;
                var result = primitiveType.ParseEnum(text, out enumRemapped);
                if (enumRemapped)
                {
                    context.SerializerContext.HasRemapOccurred = true;
                }
                return result;
            }

            // Parse default types 
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.Boolean:
                    object value;
                    context.SerializerContext.Schema.TryParse(scalar, type, out value);
                    return value;
                case TypeCode.DateTime:
                    return DateTime.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.String:
                    return text;
            }

            if (type == typeof(TimeSpan))
            {
                return TimeSpan.Parse(text, CultureInfo.InvariantCulture);
            }

            // Remove _ character from numeric values
            text = text.Replace("_", string.Empty);

            // Parse default types 
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.Char:
                    if (text.Length != 1)
                    {
                        throw new YamlException(scalar.Start, scalar.End, $"Unable to decode char from [{text}]. Expecting a string of length == 1");
                    }
                    return text.ToCharArray()[0];
                    break;
                case TypeCode.Byte:
                    return byte.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.SByte:
                    return sbyte.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.Int16:
                    return short.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.UInt16:
                    return ushort.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.Int32:
                    return int.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.UInt32:
                    return uint.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.Int64:
                    return long.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.UInt64:
                    return ulong.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.Single:
                    return float.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.Double:
                    return double.Parse(text, CultureInfo.InvariantCulture);
                case TypeCode.Decimal:
                    return decimal.Parse(text, CultureInfo.InvariantCulture);
            }

            // If we are expecting a type object, return directly the string
            if (type == typeof(object))
            {
                // Try to parse the scalar directly
                string defaultTag;
                object scalarValue;
                if (context.SerializerContext.Schema.TryParse(scalar, true, out defaultTag, out scalarValue))
                {
                    return scalarValue;
                }

                return text;
            }

            throw new YamlException(scalar.Start, scalar.End, $"Unable to decode scalar [{scalar}] not supported by current schema");
        }
 public abstract object ConvertFrom(ref ObjectContext context, Scalar fromScalar);
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     return realScalarSerializer.ConvertFrom(ref context, fromScalar);
 }
예제 #19
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     return new UFile(fromScalar.Value);
 }
예제 #20
0
        public override bool TryParse(Scalar scalar, bool parseValue, out string defaultTag, out object value)
        {
            if (base.TryParse(scalar, parseValue, out defaultTag, out value))
            {
                return true;
            }

            if (AllowFailsafeString)
            {
                value = parseValue ? scalar.Value : null;
                defaultTag = StrShortTag;
                return true;
            }

            return false;
        }