Exemplo n.º 1
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; 
            }
Exemplo n.º 2
0
        public void Map_GetSet_ByIndexer()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);

            obj["A"] = 7;
            Assert.AreEqual(7, obj["A"]);
        }
Exemplo n.º 3
0
        public void Map_GetSet_ByMember()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);

            obj.A = 7;
            Assert.AreEqual(7, obj.A);
        }
Exemplo n.º 4
0
        public void Map_GetSet_DateTimeAddDays()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);

            obj.StartDate = new DateTime(1980, 12, 05);
            obj.Interval = 5;
            obj.EndDate = obj.StartDate.AddDays(obj.Interval);

            Assert.AreEqual(new DateTime(1980, 12, 10), obj.EndDate);
        }
Exemplo n.º 5
0
        public void Map_GetSet_DoubleAddInt()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);

            obj.A = 3.4d;
            obj.B = 5;
            obj.C = obj.A + obj.B;

            Assert.AreEqual(8.4d, obj.C);
        }
Exemplo n.º 6
0
        public void Map_GetSet_IntAdd()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);

            obj.A = 3;
            obj.B = 5;
            obj.C = obj.A + obj.B;

            Assert.AreEqual(8, obj.C);
        }
Exemplo n.º 7
0
        public void Array_Autogrow_2()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Array);

            Assert.AreEqual(0, obj.Count);
            Assert.AreEqual(0, obj.Length);

            obj[150] = 100;

            Assert.AreEqual(151, obj.Count);
            Assert.AreEqual(100, obj[150]);
        }
Exemplo n.º 8
0
        public void Dynamic1()
        {
            dynamic dob = new JDO(NFX.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);
        }
Exemplo n.º 9
0
        public void Array_GetBeyondRange()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Array);

            Assert.AreEqual(0, obj.Count);
            Assert.AreEqual(0, obj.Length);

            obj[0] = 100;
            obj[1] = 120;

            Assert.AreEqual(2, obj.Count);
            Assert.AreEqual(100, obj[0]);
            Assert.AreEqual(120, obj[1]);

            Assert.IsNull(obj[10001]);
        }
Exemplo n.º 10
0
        public void Dynamic2_withList()
        {
            dynamic dob = new JDO(NFX.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);
        }
Exemplo n.º 11
0
        public void Dynamic3_WriteRead()
        {
            dynamic dob = new JDO(NFX.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);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        public void Dynamic2_withList()
        {
            dynamic dob = new JDO(NFX.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);

            Assert.AreEqual("{\"FirstName\":\"Al\",\"LastName\":\"Kutz\",\"Autos\":[\"Buick\",\"Chevy\",\"Mazda\",\"Oka\"]}", json);
        }
Exemplo n.º 14
0
        public void WithSubDocumentsAndConversionAccessors()
        {
            // http://stackoverflow.com/questions/2954531/lots-of-first-chance-microsoft-csharp-runtimebinderexceptions-thrown-when-dealin
              dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);
              obj.type = "abc";
              obj.startDate = "5/15/2001 6:00pm";
              obj.target = new JSONDynamicObject(JSONDynamicObjectKind.Map);
              obj.target.id = "A678";
              obj.target.image = "hello";
              obj.target.type = "good";
              obj.target.description = "Thank You";
              obj.target.Age = 123;
              obj.target.Salary = 125000m;

              string s1 = obj.ToJSON();

            var ro1 = s1.JSONToDynamic();
            Assert.AreEqual("abc", ro1.type);
            Assert.AreEqual(new DateTime(2001,5,15,18,00,00), ((string)ro1.startDate).AsDateTime());
            Assert.AreEqual("A678", ro1.target.id);
            Assert.AreEqual("hello", ro1.target.image);
            Assert.AreEqual("good", ro1.target.type);
            Assert.AreEqual("Thank You", ro1.target.description);

            Assert.AreEqual(123, ro1.target.Age);

            Assert.AreEqual(125000, ro1.target.Salary);

              string s2 = ((object)obj).ToJSON();

            var ro2 = s2.JSONToDynamic();
            Assert.AreEqual("abc", ro2.type);
            Assert.AreEqual(new DateTime(2001,5,15,18,00,00), ((string)ro2.startDate).AsDateTime());
            Assert.AreEqual("A678", ro2.target.id);
            Assert.AreEqual("hello", ro2.target.image);
            Assert.AreEqual("good", ro2.target.type);
            Assert.AreEqual("Thank You", ro2.target.description);
        }
Exemplo n.º 15
0
        public void Dynamic1()
        {
            dynamic dob = new JDO(NFX.Serialization.JSON.JSONDynamicObjectKind.Map);

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

            var json = JW.Write(dob);

            Console.WriteLine(json);

            Assert.AreEqual("{\"FirstName\":\"Serge\",\"LastName\":\"Rachmaninoff\",\"Middle Name\":\"V\"}", json);
        }
Exemplo n.º 16
0
        public void Map_GetMemberNames()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);

            obj.A = 3;
            obj.B = 5;
            obj.C = obj.A + obj.B;

            IEnumerable<string> nms = obj.GetDynamicMemberNames();
            var names = nms.ToList();
            Assert.AreEqual(3, names.Count);
            Assert.AreEqual("A", names[0]);
            Assert.AreEqual("B", names[1]);
            Assert.AreEqual("C", names[2]);
        }
Exemplo n.º 17
0
        public void Dynamic3_WriteRead()
        {
            dynamic dob = new JDO(NFX.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();

            Assert.AreEqual(dob2.FirstName, dob.FirstName);
            Assert.AreEqual(dob2.LastName, dob.LastName);
            Assert.AreEqual(dob2.Autos.Count, dob.Autos.Count);
        }
Exemplo n.º 18
0
        public void WithSubDocumentsAndConversionAccessors()
        {
            dynamic obj = new JSONDynamicObject(JSONDynamicObjectKind.Map);
              obj.type = "abc";
              obj.startDate = "5/15/2001 6:00pm";
              obj.target = new JSONDynamicObject(JSONDynamicObjectKind.Map);
              obj.target.id = "A678";
              obj.target.image = "hello";
              obj.target.type = "good";
              obj.target.description = "Thank You";
              obj.target.Age = 123;
              obj.target.Salary = 125000m;

              string s1 = obj.ToJSON();

            var ro1 = s1.JSONToDynamic();
            Assert.AreEqual("abc", ro1.type);
            Assert.AreEqual(new DateTime(2001,5,15,18,00,00), ((string)ro1.startDate).AsDateTime());
            Assert.AreEqual("A678", ro1.target.id);
            Assert.AreEqual("hello", ro1.target.image);
            Assert.AreEqual("good", ro1.target.type);
            Assert.AreEqual("Thank You", ro1.target.description);

            Assert.AreEqual(123, ro1.target.Age);

            Assert.AreEqual(125000, ro1.target.Salary);

              string s2 = ((object)obj).ToJSON();

            var ro2 = s2.JSONToDynamic();
            Assert.AreEqual("abc", ro2.type);
            Assert.AreEqual(new DateTime(2001,5,15,18,00,00), ((string)ro2.startDate).AsDateTime());
            Assert.AreEqual("A678", ro2.target.id);
            Assert.AreEqual("hello", ro2.target.image);
            Assert.AreEqual("good", ro2.target.type);
            Assert.AreEqual("Thank You", ro2.target.description);
        }
Exemplo n.º 19
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;
        }
Exemplo n.º 20
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;
        }