예제 #1
0
        public object Serialize(object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (ClrType.IsInstanceOfType(value) &&
                _valueToValues.TryGetValue(value, out EnumValue enumValue))
            {
                return(enumValue.Name);
            }

            // schema first unbound enum type
            if (ClrType == typeof(object) &&
                _nameToValues.TryGetValue(
                    value.ToString().ToUpperInvariant(),
                    out enumValue))
            {
                return(enumValue.Name);
            }

            throw new ArgumentException(
                      TypeResourceHelper.Scalar_Cannot_Serialize(Name));
        }
예제 #2
0
        protected sealed override bool IsInstanceOfType(object value)
        {
            if (value is null)
            {
                return(true);
            }

            if (ClrType.IsInstanceOfType(value))
            {
                return(true);
            }

            Type elementType = DotNetTypeInfoFactory.GetInnerListType(value.GetType());

            if (elementType is null)
            {
                return(false);
            }

            Type clrType = InnerClrType;

            if (elementType == typeof(object))
            {
                return(value is IList l &&
                       (l.Count == 0 || clrType == l[0]?.GetType()));
            }

            return(elementType == clrType);
        }
예제 #3
0
        public virtual bool TryDeserialize(object serialized, out object value)
        {
            try
            {
                if (serialized is null)
                {
                    value = null;
                    return(true);
                }

                if (serialized is IReadOnlyDictionary <string, object> dict)
                {
                    value = _deserialize(dict);
                    return(true);
                }

                if (ClrType != typeof(object) && ClrType.IsInstanceOfType(serialized))
                {
                    value = serialized;
                    return(true);
                }

                value = null;
                return(false);
            }
            catch
            {
                value = null;
                return(false);
            }
        }
예제 #4
0
 /// <summary>
 /// Defines if the specified <paramref name="value" />
 /// is a instance of this type.
 /// </summary>
 /// <param name="value">
 /// A value representation of this type.
 /// </param>
 /// <returns>
 /// <c>true</c> if the value is a value of this type;
 /// otherwise, <c>false</c>.
 /// </returns>
 public virtual bool IsInstanceOfType(object value)
 {
     if (value is null)
     {
         return(true);
     }
     return(ClrType.IsInstanceOfType(value));
 }
예제 #5
0
 private bool IsOfTypeWithClrType(
     IResolverContext context,
     object result)
 {
     if (result == null)
     {
         return(true);
     }
     return(ClrType.IsInstanceOfType(result));
 }
            public override sealed void Validate(object value)
            {
                Validation.CheckNotNull(value, nameof(value));
                if (!ClrType.IsInstanceOfType(value))
                {
                    throw new ArgumentException($"Value of type {value.GetType()} is incompatible with expected type {ClrType}", nameof(value));
                }

                ValidateImpl((T)Validation.CheckNotNull(value, nameof(value)));
            }
        /// <summary>
        /// Creates a message of the appropriate CLR type from the given value,
        /// which may already be of the right type or may be a dynamic message.
        /// </summary>
        private object CoerceType(object value)
        {
            ThrowHelper.ThrowIfNull(value, "value");
            // If it's already of the right type, we're done
            if (ClrType.IsInstanceOfType(value))
            {
                return(value);
            }

            // No... so let's create a builder of the right type, and merge the value in.
            IMessageLite message = (IMessageLite)value;

            return(CreateBuilder().WeakMergeFrom(message).WeakBuild());
        }
예제 #8
0
        public object Serialize(object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (ClrType.IsInstanceOfType(value) &&
                _valueToValues.TryGetValue(value, out EnumValue enumValue))
            {
                return(enumValue.Name);
            }

            throw new ArgumentException(
                      TypeResources.Scalar_Cannot_Serialize(Name));
        }
예제 #9
0
        public object Serialize(object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (ClrType.IsInstanceOfType(value) &&
                _valueToValues.TryGetValue(value, out EnumValue enumValue))
            {
                return(enumValue.Name);
            }

            throw new ArgumentException(
                      "The specified value cannot be handled by the " +
                      $"EnumType `{Name}`.");
        }
예제 #10
0
        public object Deserialize(object serialized)
        {
            if (serialized is null)
            {
                return(null);
            }

            if (serialized is IReadOnlyDictionary <string, object> dict)
            {
                return(_deserialize(dict));
            }

            if (ClrType != typeof(object) && ClrType.IsInstanceOfType(serialized))
            {
                return(serialized);
            }

            throw new InputObjectSerializationException(
                      "The specified value is not a serialized input object.");
        }