Created
March 17, 2012 20:49
-
-
Save wayne-o/2065149 to your computer and use it in GitHub Desktop.
NodaZoneDateTimeConverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using Newtonsoft.Json; | |
namespace NodaTime.Serialization.JsonNet | |
{ | |
public class NodaZoneDateTimeConverter : JsonConverter | |
{ | |
public NodaZoneDateTimeConverter() | |
{ | |
// default values | |
DateTimeFormat = DefaultDateTimeFormat; | |
Culture = CultureInfo.InvariantCulture; | |
} | |
private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFF"; | |
/// <summary> | |
/// Gets or sets the date time format used when converting a date to and from JSON. | |
/// </summary> | |
/// <value>The date time format used when converting a date to and from JSON.</value> | |
public string DateTimeFormat { get; set; } | |
/// <summary> | |
/// Gets or sets the culture used when converting a date to and from JSON. | |
/// </summary> | |
/// <value>The culture used when converting a date to and from JSON.</value> | |
public CultureInfo Culture { get; set; } | |
public override bool CanConvert(Type objectType) | |
{ | |
return objectType == typeof(ZonedDateTime) || objectType == typeof(ZonedDateTime?); | |
} | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
if (!(value is ZonedDateTime)) | |
throw new Exception(string.Format("Unexpected value when converting. Expected NodaTime.ZonedDateTime, got {0}.", value.GetType().FullName)); | |
var zoneDateTime = (ZonedDateTime)value; | |
writer.WriteValue(JsonConvert.SerializeObject(new Info(zoneDateTime.LocalDateTime, zoneDateTime.Offset))); | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
if (reader.TokenType == JsonToken.Null) | |
{ | |
if (objectType != typeof(ZonedDateTime?)) | |
throw new Exception(string.Format("Cannot convert null value to {0}.", objectType)); | |
return null; | |
} | |
var info = JsonConvert.DeserializeObject<Info>(reader.Value.ToString()); | |
return null; | |
} | |
private class Info | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Info"/> class. | |
/// </summary> | |
/// <param name="localDateTime"> | |
/// The local date time. | |
/// </param> | |
/// <param name="offset"> | |
/// The offset. | |
/// </param> | |
public Info(LocalDateTime localDateTime, Offset offset) | |
{ | |
LocalDateTime = localDateTime; | |
Offset = offset; | |
} | |
/// <summary> | |
/// Gets or sets LocalDateTime. | |
/// </summary> | |
public LocalDateTime LocalDateTime { get; set; } | |
/// <summary> | |
/// Gets or sets Offset. | |
/// </summary> | |
public Offset Offset { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment