Ejemplo n.º 1
0
        public override void ValueToJson(JsonWriter writer, Row row, JsonSerializer serializer)
        {
            var value = _getValue(row);

            if (value == null)
            {
                writer.WriteNull();
            }
            else if (EnumType == null)
            {
                writer.WriteValue(value.Value);
            }
            else if (EnumType.IsEnum)
            {
                writer.WriteValue(Enum.GetName(EnumType, value.Value));
            }
            else if (EnumType.IsSubclassOf(typeof(DataEnum)))
            {
                writer.WriteValue(DataEnum.ConvertFromInt32(EnumType, value.Value).Key);
            }
            else
            {
                throw new InvalidProgramException(String.Format("{0} geçerli bir enum tipi deðil!", EnumType.Name));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Reads the JSON representation of the object.</summary>
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="objectType">
        ///   Type of the object.</param>
        /// <param name="existingValue">
        ///   The existing value of object being read.</param>
        /// <param name="serializer">
        ///   The calling serializer.</param>
        /// <returns>
        ///   The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.Null:
            case JsonToken.Undefined:
                return(null);

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Boolean:
                return(DataEnum.ConvertFromInt32(objectType, Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture)));

            case JsonToken.String:
                return(DataEnum.ConvertFromString(objectType, (string)reader.Value));

            default:
                throw new JsonSerializationException("Unexpected token when deserializing row: " + reader.TokenType);
            }
        }
Ejemplo n.º 3
0
        public override void ValueToJson(JsonWriter writer, Row row, JsonSerializer serializer)
        {
            var value = _getValue(row);

            if (value == null)
            {
                writer.WriteNull();
            }
            else if (EnumType == null)
            {
                writer.WriteValue(value.Value);
            }
            else if (EnumType.IsEnum)
            {
                writer.WriteValue(Enum.GetName(EnumType, value.Value));
            }
            else if (EnumType.IsSubclassOf(typeof(DataEnum)))
            {
                writer.WriteValue(DataEnum.ConvertFromInt32(EnumType, (Int32)value.Value).Key);
            }
        }
Ejemplo n.º 4
0
        internal static Int64 ConvertEnumFromInt(Type enumType, Int64 v)
        {
            if (enumType.IsEnum)
            {
                if (!Enum.IsDefined(enumType, v))
                {
                    throw new InvalidCastException(String.Format("{0} geçerli bir {1} deðeri deðil!", v, enumType.Name));
                }

                return(v);
            }
            else if (enumType.IsSubclassOf(typeof(DataEnum)))
            {
                DataEnum.ConvertFromInt32(enumType, (Int32)v);
                return(v);
            }
            else
            {
                throw new InvalidProgramException(String.Format("{0} geçerli bir enum tipi deðil!", enumType.Name));
            }
        }