Created
February 25, 2009 19:46
-
-
Save atifaziz/70369 to your computer and use it in GitHub Desktop.
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
// Demo for: | |
// http://groups.google.com/group/jayrock/t/878df871a14df35b | |
using System; | |
using Jayrock.Json; | |
using Jayrock.Json.Conversion; | |
interface IFoo | |
{ | |
string Text { get; } | |
} | |
class Bar : IFoo | |
{ | |
public string Text { get { return "Bar"; } } | |
} | |
class Baz : IFoo | |
{ | |
public string Text { get { return "Baz"; } } | |
} | |
internal class FooExporter<T> : IExporter where T : IFoo | |
{ | |
public Type InputType { get { return typeof(T); } } | |
void IExporter.Export(ExportContext context, object value, JsonWriter writer) | |
{ | |
Export(context, (T) value, writer); | |
} | |
public virtual void Export(ExportContext context, T value, JsonWriter writer) | |
{ | |
writer.WriteStartObject(); | |
writer.WriteMember("text"); | |
context.Export(value.Text.ToUpperInvariant(), writer); | |
writer.WriteEndObject(); | |
} | |
} | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var ec = new ExportContext(); | |
ec.Register(new FooExporter<Bar>()); | |
ec.Register(new FooExporter<Baz>()); | |
ec.Export(new Bar(), new JsonTextWriter(Console.Out)); | |
ec.Export(new Baz(), new JsonTextWriter(Console.Out)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment