Esempio n. 1
0
        static void Main(string[] args)
        {
            //             Test t = new Test();
            //             t.Key.Add(1);
            //             t.Key.Add(2);
            //             string json = JsonConvert.SerializeObject(t);
            //
            //             var ne = JsonConvert.DeserializeObject<Test>(json);

            ObjectValue obj = new ObjectValue();

            obj.Add("key", new StringValue("123"));
            MapValue map = new MapValue();

            map.Add(new NumberValue("456"), new StringValue("hhhh"));
            map.Add(new NumberValue("4567"), new StringValue("hh222"));
            obj.Add("map", map);

            ObjectValue obj1 = new ObjectValue();

            obj1.Add("key", new StringValue("123"));
            obj1.Add("map", map);
            ListValue list = new ListValue();

            list.Add(obj1);
            list.Add(obj1);
            list.Add(obj1);
            obj.Add("list", list);

            File.WriteAllText("test.json", obj.ToJson(0));

            File.WriteAllText("test.lua", obj.ToLua(0));
        }
Esempio n. 2
0
        public void ShouldCreateNewMapValue()
        {
            var val1 = new MapValue <StringValue, StringValue>();

            val1.Add("key1", "toto");
            Assert.NotEqual(val1.Count, 0);
            Assert.Equal(val1["key1"], "toto");
        }
Esempio n. 3
0
        /// <summary>
        /// Serialises the specified object into a value node
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ValueNode Serialise(object obj)
        {
            // Sanity check
            if (obj == null)
            {
                return(null);
            }

            Type t = obj.GetType();

            // Is it a map type?
            if (typeof(IDictionary).IsAssignableFrom(t))
            {
                // Create a map type
                MapValue    mapVal = new MapValue();
                IDictionary dict   = obj as IDictionary;
                foreach (var key in dict.Keys)
                {
                    if (key is string)
                    {
                        ValueNode tmp;
                        if (translateOpts.Serialise(dict[key], out tmp))
                        {
                            mapVal.Add(key as string, tmp);
                        }
                        else
                        {
                            mapVal.Add(key as string, null);
                        }
                    }
                }
                return(mapVal);
            }

            // Is it an array type?
            else if (t.IsArray)
            {
                // Create an array type
                ArrayValue arrVal = new ArrayValue();
                var        arr    = obj as Array;
                for (int i = 0; i < arr.Length; i++)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(arr.GetValue(i), out tmp))
                    {
                        arrVal.Add(tmp);
                    }
                    else
                    {
                        arrVal.Add(null);
                    }
                }
                return(arrVal);
            }
            else if (typeof(IList).IsAssignableFrom(t))
            {
                // Create an array type
                ArrayValue arrVal = new ArrayValue();
                var        list   = obj as IList;
                for (int i = 0; i < list.Count; i++)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(list[i], out tmp))
                    {
                        arrVal.Add(tmp);
                    }
                    else
                    {
                        arrVal.Add(null);
                    }
                }
                return(arrVal);
            }
            else if (typeof(ICollection).IsAssignableFrom(t))
            {
                // Create an array type
                ArrayValue arrVal     = new ArrayValue();
                var        collection = obj as ICollection;
                foreach (object item in collection)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(item, out tmp))
                    {
                        arrVal.Add(tmp);
                    }
                    else
                    {
                        arrVal.Add(null);
                    }
                }
                return(arrVal);
            }

            // Fill a map via reflection
            MapValue mapValue = new MapValue();

            foreach (var field in t.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                ValueNode tmp;
                if (translateOpts.Serialise(field.GetValue(obj), out tmp) && tmp != null)
                {
                    mapValue.Add(field.Name, tmp);
                }
            }
            foreach (var property in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (property.GetGetMethod() != null)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(property.GetValue(obj, null), out tmp) && tmp != null)
                    {
                        mapValue.Add(property.Name, tmp);
                    }
                }
            }

            return(mapValue);
        }
Esempio n. 4
0
        /// <summary>
        /// Serialises the specified object into a value node
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ValueNode Serialise(object obj)
        {
            // Sanity check
            if (obj == null) return null;

            Type t = obj.GetType();

            // Is it a map type?
            if (typeof(IDictionary).IsAssignableFrom(t))
            {
                // Create a map type
                MapValue mapVal = new MapValue();
                IDictionary dict = obj as IDictionary;
                foreach (var key in dict.Keys)
                {
                    if (key is string)
                    {
                        ValueNode tmp;
                        if (translateOpts.Serialise(dict[key], out tmp))
                            mapVal.Add(key as string, tmp);
                        else
                            mapVal.Add(key as string, null);
                    }
                }
                return mapVal;
            }

            // Is it an array type?
            else if (t.IsArray)
            {
                // Create an array type
                ArrayValue arrVal = new ArrayValue();
                var arr = obj as Array;
                for (int i = 0; i < arr.Length; i++)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(arr.GetValue(i), out tmp))
                        arrVal.Add(tmp);
                    else
                        arrVal.Add(null);
                }
                return arrVal;
            }
            else if (typeof(IList).IsAssignableFrom(t))
            {
                // Create an array type
                ArrayValue arrVal = new ArrayValue();
                var list = obj as IList;
                for (int i = 0; i < list.Count; i++)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(list[i], out tmp))
                        arrVal.Add(tmp);
                    else
                        arrVal.Add(null);
                }
                return arrVal;
            }
            else if (typeof(ICollection).IsAssignableFrom(t))
            {
                // Create an array type
                ArrayValue arrVal = new ArrayValue();
                var collection = obj as ICollection;
                foreach (object item in collection)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(item, out tmp))
                        arrVal.Add(tmp);
                    else
                        arrVal.Add(null);
                }
                return arrVal;
            }

            // Fill a map via reflection
            MapValue mapValue = new MapValue();
            foreach (var field in t.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                ValueNode tmp;
                if (translateOpts.Serialise(field.GetValue(obj), out tmp) && tmp != null)
                    mapValue.Add(field.Name, tmp);
            }
            foreach (var property in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (property.GetGetMethod() != null)
                {
                    ValueNode tmp;
                    if (translateOpts.Serialise(property.GetValue(obj, null), out tmp) && tmp != null)
                        mapValue.Add(property.Name, tmp);
                }
            }

            return mapValue;
        }