Hi Atif,
Thank you for the code samples. I appreciate your help. I think it
would be real useful to be able to register interface types /abstract
class types for export (and may be import). When I export a class
implementing the interface using the registered exporter, me as a
developer will know that I am only serializing the interface specific
portion of the class. I will have the flexibility to have a class that
implements N interfaces and I can create N exporters to get a
representation of the same class is various formats or serialize only
specific parts of a class.
Also, registering FooExporter<BarA>, FooExporter<BarB>,
FooExporter<BarC> etc, in my opinion is hard to maintain and keep
updated. I think registering interfaces can actually be powerful.
Other than the fact that JayRock doesn't support it right now, do you
see any drawbacks in being able to do this. If there are no drawbacks,
I think I can update the FindType method called by FindExporter in
JSONConvert to support it. Is that something you would like me to do
and submit a patch for?
Also, I would like the ability to import interfaces too...I know that
sounds ridiculous, but hear me out. I actually dislike the support for
interface or abstract class deserialization in XmlSerializer or
DataContractSerializer etc where I have to specify all the types it
can be by marking up the properties with decorator attributes. I would
actually like to be able to configure a factory in the deserialization
process which will get called to create the appropriate type from a
value passed in. Thoughts??/ Ideas???
By the way, *LOVE* the design of JayRock JSON serialization. The fact
that i can read anything into a JSONObject is awesome. I am not sure
why XML deserializers can't provide the same capability. I would
actually love to use the JayRock design for my XML serialization/
deserialization also but unfortunately that can't happen...
Thank You,
Vish
> [InterfaceExportDemo.cs1K ]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));
> }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -