Beispiel #1
0
        public void Dynamic1()
        {
            dynamic dob = new JDO(Azos.Serialization.JSON.JSONDynamicObjectKind.Map);

            dob.FirstName      = "Serge";
            dob.LastName       = "Rachmaninoff";
            dob["Middle Name"] = "V";

            var json = JW.Write(dob);

            Console.WriteLine(json);

            Aver.AreEqual("{\"FirstName\":\"Serge\",\"LastName\":\"Rachmaninoff\",\"Middle Name\":\"V\"}", json);
        }
Beispiel #2
0
        public void Dynamic2_withList()
        {
            dynamic dob = new JDO(Azos.Serialization.JSON.JSONDynamicObjectKind.Map);

            dob.FirstName = "Al";
            dob.LastName  = "Kutz";
            dob.Autos     = new List <string> {
                "Buick", "Chevy", "Mazda", "Oka"
            };

            var json = JW.Write(dob);

            Console.WriteLine(json);

            Aver.AreEqual("{\"FirstName\":\"Al\",\"LastName\":\"Kutz\",\"Autos\":[\"Buick\",\"Chevy\",\"Mazda\",\"Oka\"]}", json);
        }
Beispiel #3
0
        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            var arr = m_Data as JsonDataArray;

            result = null;

            if (arr == null)
            {
                var map = m_Data as JsonDataMap;
                if (map != null)
                {
                    if (indexes.Length == 1 && indexes[0] is string)
                    {
                        result = map[(string)indexes[0]];

                        if (result is IJsonDataObject)
                        {
                            result = new JsonDynamicObject((IJsonDataObject)result);
                        }

                        return(true);
                    }
                }
                return(false);
            }
            if (indexes.Length != 1 || !(indexes[0] is int))
            {
                return(false);
            }

            var idx = (int)indexes[0];

            if (idx < arr.Count)
            {
                result = arr[idx];
            }

            if (result is IJsonDataObject)
            {
                result = new JsonDynamicObject((IJsonDataObject)result);
            }

            return(true);
        }
Beispiel #4
0
        public void Dynamic3_WriteRead()
        {
            dynamic dob = new JDO(Azos.Serialization.JSON.JSONDynamicObjectKind.Map);

            dob.FirstName = "Al";
            dob.LastName  = "Kutz";
            dob.Autos     = new List <string> {
                "Buick", "Chevy", "Mazda", "Oka"
            };

            string json = JW.Write(dob);

            var dob2 = json.JsonToDynamic();


            Aver.AreEqual(dob2.FirstName, dob.FirstName);
            Aver.AreEqual(dob2.LastName, dob.LastName);
            Aver.AreEqual(dob2.Autos.Count, dob.Autos.Count);
        }
Beispiel #5
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var map = m_Data as JsonDataMap;

            if (map == null)
            {
                var arr = m_Data as JsonDataArray;
                if (arr != null)
                {
                    if (binder.Name == "Count" || binder.Name == "Length")
                    {
                        result = arr.Count;
                        return(true);
                    }

                    if (binder.Name == "List")
                    {
                        result = arr;
                        return(true);
                    }
                }

                result = null;
                return(false);
            }

            if (map.ContainsKey(binder.Name))
            {
                result = map[binder.Name];
            }
            else
            {
                return(base.TryGetMember(binder, out result));
            }

            if (result is IJsonDataObject)
            {
                result = new JsonDynamicObject((IJsonDataObject)result);
            }

            return(true);
        }