Program.cs :  » Development » Jayrock » JsonImportDemo » 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 » JsonImportDemo » Program.cs
namespace JsonImportDemo{
    #region Imports

    using System;
    using System.Collections;
    using Jayrock.Json;
    using Jayrock.Json.Conversion;

    #endregion

    /// <summary>
    /// This project demonstrates using JsonConvert to unpack JSON text into 
    /// a given set of application and general types.
    /// </summary>

    internal static class Program
    {
        private static void Main()
        {
            string text = @"
                {
                    ""header"": ""SVG Viewer"",
                    ""items"": [
                        {""id"": ""Open""},
                        {""id"": ""OpenNew"", ""label"": ""Open New""},
                        null,
                        {""id"": ""ZoomIn"", ""label"": ""Zoom In""},
                        {""id"": ""ZoomOut"", ""label"": ""Zoom Out""},
                        {""id"": ""OriginalView"", ""label"": ""Original View""},
                        null,
                        {""id"": ""Quality""},
                        {""id"": ""Pause""},
                        {""id"": ""Mute""},
                        null,
                        {""id"": ""Find"", ""label"": ""Find...""},
                        {""id"": ""FindAgain"", ""label"": ""Find Again""},
                        {""id"": ""Copy""},
                        {""id"": ""CopyAgain"", ""label"": ""Copy Again""},
                        {""id"": ""CopySVG"", ""label"": ""Copy SVG""},
                        {""id"": ""ViewSVG"", ""label"": ""View SVG""},
                        {""id"": ""ViewSource"", ""label"": ""View Source""},
                        {""id"": ""SaveAs"", ""label"": ""Save As""},
                        null,
                        {""id"": ""Help""},
                        {""id"": ""About"", ""label"": ""About Adobe CVG Viewer...""}
                    ]
                }";

            //
            // The above menu definition in JSON text is imported using
            // two approaches in the demonstrations. The first method 
            // unpacks the data into application-supplied types. The 
            // second, unpacks into general types supplied by Jayrock.
            //

            ImportByTypeDemo(text);
            Console.WriteLine();
            AutoImportDemo(text);
        }

        private static void ImportByTypeDemo(string text) 
        {
            Menu menu = (Menu) JsonConvert.Import(typeof(Menu), text);

            string separator = new string('-', 40);

            Console.WriteLine(menu.Header);
            Console.WriteLine(separator);
            
            foreach (MenuItem item in menu.Items)
            {
                Console.Write('\t');

                if (item != null)
                    Console.WriteLine("{0} ({1})", item.Label ?? item.Id, item.Id);
                else
                    Console.WriteLine(separator);
            }
        }

        //
        // Disable warning CS0649:
        // Field '...' is never assigned to, and will always have its default value null
        //

        #pragma warning disable 649

        public class Menu
        {
            public string Header;
            public MenuItem[] Items;
        }

        public class MenuItem
        {
            public string Id;
            public string Label;
        }

        private static void AutoImportDemo(string text)
        {
            //
            // Without any type specification, Import will unpack
            // a JSON object as JsonObject, array as JsonArray,
            // and string as System.String, number as JsonNumber,
            // Boolean as System.Boolean and null simply as a 
            // local null reference.
            //

            JsonObject menu = (JsonObject) JsonConvert.Import(text);

            string separator = new string('-', 40);

            Console.WriteLine(menu["header"]);
            Console.WriteLine(separator);

            JsonArray items = (JsonArray) menu["items"];

            foreach (JsonObject item in items)
            {
                Console.Write('\t');

                if (item != null)
                {
                    string id = (string) item["id"];
                    string label = (string) item["label"];
                    Console.WriteLine("{0} ({1})", label ?? id, id);
                }
                else
                {
                    Console.WriteLine(separator);
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.