DuckCollectionImporter.cs :  » Development » Jayrock » JsonConversionsDemo » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » Jayrock 
Jayrock » JsonConversionsDemo » DuckCollectionImporter.cs
namespace JsonConversionsDemo{
    #region Imports

    using System;
    using System.Reflection;
    using Jayrock.Json;
    using Jayrock.Json.Conversion;

    #endregion

    /// <summary>
    /// An importer for importing a duck-typed collection of elements from a 
    /// JSON array.
    /// </summary>
    /// <remarks>
    /// The importer can infer the element type provided that
    /// the collection has an instance-base and public Add method that 
    /// takes a single argument of the element type.
    /// </remarks>

    public class DuckCollectionImporter : DuckCollectionImporterBase
    {
        private readonly MethodInfo _adder;

        public DuckCollectionImporter(Type outputType) :
            base(outputType, DuckCollectionReflector.InferElementType(outputType))
        {
            try
            {
                _adder = DuckCollectionReflector.FindAddMethod(outputType, ElementType);
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException(e.Message, "outputType", e);
            }
        }

        protected override void InvokeAdd(object collection, object[] args)
        {
            _adder.Invoke(collection, args);
        }
    }

    /// <summary>
    /// This is the generic version of <see cref="DuckCollectionImporter"/>.
    /// </summary>

    public sealed class DuckCollectionImporter<Collection, Element> : DuckCollectionImporterBase
        where Collection : new()
    {
        public DuckCollectionImporter() :
            base(typeof(Collection), typeof(Element)) { }

        protected override void ImportElements(object collection, ImportContext context, JsonReader reader)
        {
            if (collection == null) throw new ArgumentNullException("collection");
            if (context == null) throw new ArgumentNullException("context");
            if (reader == null) throw new ArgumentNullException("reader");

            Action<Element> adder = DuckCollectionReflector.GetAdder<Element>(collection);

            while (reader.TokenClass != JsonTokenClass.EndArray)
                adder((Element) context.Import(typeof(Element), reader));
        }

        protected override object CreateCollection()
        {
            return new Collection();
        }

        protected override void InvokeAdd(object collection, object[] args)
        {
            if (collection == null) throw new ArgumentNullException("collection");
            if (args == null) throw new ArgumentNullException("args");
            if (args.Length != 1) throw new ArgumentException(null, "args");

            //
            // NOTE! This implementation is horribly slow.
            // It is provided here only for sake of completeness but it 
            // should never be needed for any practical reason.
            //

            DuckCollectionReflector.GetAdder<Element>(collection)((Element) args[0]);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.