Created
January 26, 2015 21:59
-
-
Save atifaziz/1e3638128e9f28a6c2ce to your computer and use it in GitHub Desktop.
Demo showing how to export TimeSpan as DateTime in Jayrock
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
// https://groups.google.com/d/msg/jayrock/ZArN2unguMs/Y-aNUp7eqAQJ | |
using System; | |
using Jayrock.Json; | |
using Jayrock.Json.Conversion; | |
using Jayrock.Json.Conversion.Converters; | |
static class Program | |
{ | |
static void Main() | |
{ | |
var ecf = JsonConvert.CurrentExportContextFactory; | |
JsonConvert.CurrentExportContextFactory = () => | |
{ | |
var ctx = ecf(); | |
ctx.Register(new TimeSpanExporter()); | |
return ctx; | |
}; | |
var json = JsonConvert.ExportToString(TimeSpan.FromDays(23.456)); | |
Console.WriteLine(json); | |
} | |
} | |
sealed class TimeSpanExporter : ExporterBase | |
{ | |
public TimeSpanExporter() : base(typeof(TimeSpan)) {} | |
protected override void ExportValue(ExportContext ctx, object value, JsonWriter writer) | |
{ | |
ctx.Export(DateTime.Now + (TimeSpan) value, writer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment