Skip to content

Instantly share code, notes, and snippets.

@wayne-o
Created March 17, 2012 20:05
Show Gist options
  • Save wayne-o/2064843 to your computer and use it in GitHub Desktop.
Save wayne-o/2064843 to your computer and use it in GitHub Desktop.
NodaZoneDateTimeConverter
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(
JsonConvert.SerializeObject(zoneDateTime.LocalDateTime, new NodaLocalDateTimeConverter()),
JsonConvert.SerializeObject(zoneDateTime.Offset, new NodaOffsetConverter()),
zoneDateTime.Zone.Id)));
}
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());
var ret = new ZonedDateTime()
return new ZonedDateTime;
}
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>
/// <param name="zone">
/// The zone.
/// </param>
public Info(string localDateTime, string offset, string zone)
{
LocalDateTime = localDateTime;
Offset = offset;
Zone = zone;
}
/// <summary>
/// Gets or sets LocalDateTime.
/// </summary>
public string LocalDateTime { get; set; }
/// <summary>
/// Gets or sets Offset.
/// </summary>
public string Offset { get; set; }
/// <summary>
/// Gets or sets Zone.
/// </summary>
public string Zone { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment