Beispiel #1
0
        public static List <object> ConvertArrayFrom(Array array, bool usePrivateFields, string[] exclusion_fields = null)
        {
            List <object> list = new List <object>();

            foreach (var elem in array)
            {
                var type = elem.GetType();
                if (IsStructureType(type))
                {
                    list.Add(Json.FromStructure(elem, usePrivateFields, exclusion_fields));
                }
                else if (elem is Array)
                {
                    list.Add(JsonObjectArray.FromArray(elem as Array, usePrivateFields, exclusion_fields));
                }
                else if (elem is IList)
                {
                    list.Add(JsonObjectArray.FromArray((elem as IList).OfType <object>().ToArray(), usePrivateFields, exclusion_fields));
                }
                else
                {
                    list.Add(elem);
                }
            }

            return(list);
        }
Beispiel #2
0
        public static List <JsonObject> ConvertFrom(object structure, bool usePrivateFields, string[] exclusion_fields = null)
        {
            if (structure is Array)
            {
                throw new Exception("Use JsonObjectArray.FromArray(Array array).");
            }
            if (!IsStructureType(structure.GetType()))
            {
                throw new Exception("Unknown Structure format.");
            }

            var structType = structure.GetType();
            List <FieldInfo> fields;

            if (usePrivateFields)
            {
                fields = structType.GetRuntimeFields().ToList();
            }
            else
            {
                fields = structType.GetFields().ToList();
            }

            return(fields.Select <FieldInfo, JsonObject>(field =>
            {
                string name = field.Name;
                if (!(exclusion_fields is null) && exclusion_fields.Contains(name))
                {
                    return null;
                }

                object value = field.GetValue(structure);

                if (value is Array)
                {
                    value = JsonObjectArray.FromArray(value as Array, usePrivateFields, exclusion_fields);
                }
                else if (value is IList)
                {
                    value = JsonObjectArray.FromArray((value as IList).OfType <object>().ToArray(), usePrivateFields, exclusion_fields);
                }
                else if (IsStructureType(field.FieldType))
                {
                    value = Json.FromStructure(value, usePrivateFields, exclusion_fields);
                }

                return new JsonObject(name, value);
            }).OfType <JsonObject>().ToList());
        }