/// <summary>
        /// If you try to get a value of a property not defined in the class, this method is called.
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
        /// </returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (!m_Dictionary.TryGetValue(binder.Name, out result))
            {
                result = null;
                return true;
            }

            var dictionary = result as IDictionary<string, object>;
            if (dictionary != null)
            {
                result = new DynamicJsonObject(dictionary);
                return true;
            }

            var arrayList = result as ArrayList;
            if (arrayList != null && arrayList.Count > 0)
            {
                if (arrayList[0] is IDictionary<string, object>)
                    result = new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)));
                else
                    result = new List<object>(arrayList.Cast<object>());
            }

            return true;
        }
Example #2
0
        static void Main(string[] args)
        {
            const string json =
            "{" +
            "     \"firstName\": \"John\"," +
            "     \"lastName\" : \"Smith\"," +
            "     \"age\"      : 25," +
            "     \"address\"  :" +
            "     {" +
            "         \"streetAddress\": \"21 2nd Street\"," +
            "         \"city\"         : \"New York\"," +
            "         \"state\"        : \"NY\"," +
            "         \"postalCode\"   : \"11229\"" +
            "     }," +
            "     \"phoneNumber\":" +
            "     [" +
            "         {" +
            "           \"type\"  : \"home\"," +
            "           \"number\": \"212 555-1234\"" +
            "         }," +
            "         {" +
            "           \"type\"  : \"fax\"," +
            "           \"number\": \"646 555-4567\"" +
            "         }" +
            "     ]" +
            " }";

            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });

            dynamic data = serializer.Deserialize<object>(json);

            Console.WriteLine(data.firstName); // John
            Console.WriteLine(data.lastName); // Smith
            Console.WriteLine(data.age); // 25
            Console.WriteLine(data.address.postalCode); // 11229

            Console.WriteLine(data.phoneNumber.Count); // 2

            Console.WriteLine(data.phoneNumber[0].type); // home
            Console.WriteLine(data.phoneNumber[1].type); // fax

            foreach (var pn in data.phoneNumber)
            {
                Console.WriteLine(pn.number); // 212 555-1234, 646 555-4567
            }

            Console.WriteLine(data.ToString());

            // and creating JSON formatted data

            dynamic jdata = new DynamicJsonObject();
            dynamic item1 = new DynamicJsonObject();
            dynamic item2 = new DynamicJsonObject();

            ArrayList items = new ArrayList();

            item1.Name = "Drone";
            item1.Price = 92000.3;
            item2.Name = "Jet";
            item2.Price = 19000000.99;

            items.Add(item1);
            items.Add(item2);

            jdata.Date = "06/06/2004";
            jdata.Items = items;

            Console.WriteLine(jdata.ToString());
        }