/// <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) { if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); } return(null); } bool isNullable = ReflectionUtils.IsNullableType(objectType); Type t = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType; try { if (reader.TokenType == JsonToken.String) { string enumText = reader.Value.ToString(); return(EnumUtils.ParseEnumName(enumText, isNullable, t)); } if (reader.TokenType == JsonToken.Integer) { if (!AllowIntegerValues) { throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value)); } return(ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, t)); } } catch (Exception ex) { throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.FormatValueForPrint(reader.Value), objectType), ex); } // we don't actually expect to get here. throw JsonSerializationException.Create(reader, "Unexpected token {0} when parsing enum.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); }
/// <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) { bool isNullable = ReflectionUtils.IsNullableType(objectType); Type t = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType; if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); } return(null); } try { if (reader.TokenType == JsonToken.String) { string enumText = reader.Value.ToString(); if (enumText == string.Empty && isNullable) { return(null); } string finalEnumText; BidirectionalDictionary <string, string> map = EnumMemberNamesPerType.Get(t); if (enumText.IndexOf(',') != -1) { string[] names = enumText.Split(','); for (int i = 0; i < names.Length; i++) { string name = names[i].Trim(); names[i] = ResolvedEnumName(map, name); } finalEnumText = string.Join(", ", names); } else { finalEnumText = ResolvedEnumName(map, enumText); } return(Enum.Parse(t, finalEnumText, true)); } if (reader.TokenType == JsonToken.Integer) { if (!AllowIntegerValues) { throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value)); } return(ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, t)); } } catch (Exception ex) { throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.FormatValueForPrint(reader.Value), objectType), ex); } // we don't actually expect to get here. throw JsonSerializationException.Create(reader, "Unexpected token {0} when parsing enum.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); }
/// <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) { bool isNullable = ReflectionUtils.IsNullableType(objectType); Type t = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType; if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); } return(null); } try { if (reader.TokenType == JsonToken.String) { string enumText = reader.Value.ToString(); if (enumText == string.Empty && isNullable) { return(null); } var map = GetEnumNameMap(t); string resolvedEnumName; map.TryGetBySecond(enumText, out resolvedEnumName); resolvedEnumName = resolvedEnumName ?? enumText; return(Enum.Parse(t, resolvedEnumName, true)); } if (reader.TokenType == JsonToken.Integer) { return(ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, t)); } } catch (Exception ex) { throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.FormatValueForPrint(reader.Value), objectType), ex); } throw JsonSerializationException.Create(reader, "Unexpected token when parsing enum. Expected String or Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { object obj; if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); } return(null); } bool flag = ReflectionUtils.IsNullableType(objectType); Type type = (flag ? Nullable.GetUnderlyingType(objectType) : objectType); try { if (reader.TokenType == JsonToken.String) { obj = EnumUtils.ParseEnumName(reader.Value.ToString(), flag, type); } else if (reader.TokenType != JsonToken.Integer) { throw JsonSerializationException.Create(reader, "Unexpected token {0} when parsing enum.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } else { if (!this.AllowIntegerValues) { throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value)); } obj = ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, type); } } catch (Exception exception1) { Exception exception = exception1; throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.FormatValueForPrint(reader.Value), objectType), exception); } return(obj); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { bool flag = ReflectionUtils.IsNullableType(objectType); Type type = flag ? Nullable.GetUnderlyingType(objectType) : objectType; if (reader.TokenType != JsonToken.Null) { try { if (reader.TokenType == JsonToken.String) { string text = reader.Value.ToString(); object result; if (text == string.Empty && flag) { result = null; return(result); } BidirectionalDictionary <string, string> enumNameMap = this.GetEnumNameMap(type); string value; if (text.IndexOf(',') != -1) { string[] array = text.Split(new char[] { ',' }); for (int i = 0; i < array.Length; i++) { string enumText = array[i].Trim(); array[i] = StringEnumConverter.ResolvedEnumName(enumNameMap, enumText); } value = string.Join(", ", array); } else { value = StringEnumConverter.ResolvedEnumName(enumNameMap, text); } result = Enum.Parse(type, value, true); return(result); } else if (reader.TokenType == JsonToken.Integer) { if (!this.AllowIntegerValues) { throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value)); } object result = ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, type); return(result); } } catch (Exception ex) { throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.FormatValueForPrint(reader.Value), objectType), ex); } throw JsonSerializationException.Create(reader, "Unexpected token {0} when parsing enum.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); } return(null); }