Пример #1
0
 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)
 {
     AssetReference assetReference;
     if (!AssetReference.TryParse(context.Descriptor.Type, fromScalar.Value, out assetReference))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode asset reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
     }
     return assetReference;
 }
Пример #3
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     PackageVersion packageVersion;
     if (!PackageVersion.TryParse(fromScalar.Value, out packageVersion))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, "Invalid version format. Unable to decode [{0}]".ToFormat(fromScalar.Value));
     }
     return packageVersion;
 }
 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;
            UFile location;
            if (!AssetReference.TryParse(fromScalar.Value, out guid, out location))
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode asset reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value));
            }

            return AttachedReferenceManager.CreateSerializableVersion(context.Descriptor.Type, guid, location);
        }
Пример #6
0
 public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
 {
     Guid id;
     if (!Guid.TryParse(fromScalar.Value, out id))
     {
         throw new YamlException(fromScalar.Start, fromScalar.End, $"Unable to parse id [{fromScalar.Value}]");
     }
     var materialNull = new MaterialNull();
     IdentifiableHelper.SetId(materialNull, id);
     return materialNull;
 }
Пример #7
0
 public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
 {
     var parameterKey = ParameterKeys.FindByName(fromScalar.Value);
     if (parameterKey == null)
     {
         parameterKey = ParameterKeys.New<object>(null, fromScalar.Value);
         // Dont' throw an exception if keys was not found
         //throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to find registered ParameterKey [{0}]".ToFormat(fromScalar.Value));
     }
     return parameterKey;
 }
        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);

            var containingClass = objectContext.SerializerContext.TypeFromTag("!" + className); // 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);
        }
Пример #9
0
        public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
        {
            Guid guid;
            UFile location;
            Guid referenceId;
            if (!AssetReference.TryParse(fromScalar.Value, out referenceId, out guid, out location))
            {
                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 the referenceId is empty, force its creation, else attach it to the reference
            if (referenceId == Guid.Empty)
            {

                IdentifiableHelper.GetId(instance);
            }
            else
            {
                IdentifiableHelper.SetId(instance, referenceId);
            }
            return instance;
        }
        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)
            {
                return Enum.Parse(type, text, false);
            }

            // 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 [{0}]. Expecting a string of length == 1".DoFormat(text));
                    }
                    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 [{0}] not supported by current schema".DoFormat(scalar));
        }
Пример #11
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;
        }
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     return new AssetName() { Name = fromScalar.Value };
 }
Пример #13
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     Guid guid;
     Guid.TryParse(fromScalar.Value, out guid);
     return guid;
 }
Пример #14
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     return new System.Uri(fromScalar.Value);
 }
Пример #15
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     return new UDirectory(fromScalar.Value);
 }
 public abstract object ConvertFrom(ref ObjectContext context, Scalar fromScalar);
Пример #17
0
		private string EmitScalar(Scalar scalar)
		{
			return Emit(
				new SequenceStart(null, null, false, YamlStyle.Block),
				scalar,
				new SequenceEnd()
			);
		}
Пример #18
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);
		}
Пример #19
0
 public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
 {
     ObjectId objectId;
     ObjectId.TryParse(fromScalar.Value, out objectId);
     return objectId;
 }