/// <summary> /// Converts the JSON stored in a reader into the relevant Noda Time type. /// </summary> /// <param name="reader">The Json.NET reader to read data from.</param> /// <param name="objectType">The type to convert the JSON to.</param> /// <param name="existingValue">An existing value; ignored by this converter.</param> /// <param name="serializer">A serializer to use for any embedded deserialization.</param> /// <exception cref="InvalidNodaDataException">The JSON was invalid for this converter.</exception> /// <returns>The deserialized value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { Preconditions.CheckData(objectType == NullableT, "Cannot convert null value to {0}", objectType); return(null); } // Handle empty strings automatically if (reader.TokenType == JsonToken.String) { string value = (string)reader.Value; if (value == "") { Preconditions.CheckData(objectType == NullableT, "Cannot convert null value to {0}", objectType); return(null); } } // Delegate to the concrete subclass. At this point we know that we don't want to return null, so we // can ask the subclass to return a T, which we will box. That will be valid even if objectType is // T? because the boxed form of a non-null T? value is just the boxed value itself. // Note that we don't currently pass existingValue down; we could change this if we ever found a use for it. return(ReadJsonImpl(reader, serializer)); }
/// <summary> /// Reads the time zone ID (which must be a string) from the reader, and converts it to a time zone. /// </summary> /// <param name="reader">The JSON reader to fetch data from.</param> /// <param name="serializer">The serializer for embedded serialization.</param> /// <exception cref="DateTimeZoneNotFoundException">The provider does not support a time zone with the given ID.</exception> /// <returns>The <see cref="DateTimeZone"/> identified in the JSON, or null.</returns> protected override DateTimeZone ReadJsonImpl(JsonReader reader, JsonSerializer serializer) { Preconditions.CheckData(reader.TokenType == JsonToken.String, "Unexpected token parsing instant. Expected String, got {0}.", reader.TokenType); var timeZoneId = reader.Value.ToString(); return(provider[timeZoneId]); }