Beispiel #1
0
        private void ReadValues(ArrayValue av, Validator v)
        {
            Validator ev = v.ElementValidator();

            while (true)
            {
                Object value = ReadValue(ev, true);
                if (value == NONE)
                {
                    break;
                }

                av.Add(value);
            }
        }
Beispiel #2
0
        public void AddArrayValue(JavaScriptValue value)
        {
            if (value == null)
            {
                return;
            }

            if (value.IsProperty)
            {
                throw new ArgumentException("Can not add property to an array.");
            }

            CheckIsArray();

            ArrayValue.Add(value);
        }
Beispiel #3
0
        public void SetValue(int index, JavaScriptValue value)
        {
            if (value == null)
            {
                return;
            }

            if (value.IsProperty)
            {
                throw new ArgumentException("Can not add property to an array.");
            }

            CheckIsArray();

            if (index >= ArrayValue.Count)
            {
                ArrayValue.Add(value);
            }
            else
            {
                ArrayValue[index] = value;
            }
        }
Beispiel #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);
        }
Beispiel #5
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;
        }