Пример #1
0
        private JArray(Array source)
        {
            _contents = new JToken[source.Length];
            for (int i = 0; i < source.Length; ++i)
            {
                var value     = source.GetValue(i);
                var fieldType = value.GetType();

                if (value == null)
                {
                    _contents[i] = JValue.Serialize(fieldType, null);
                }
                else if (fieldType.IsValueType || fieldType == typeof(string))
                {
                    _contents[i] = JValue.Serialize(fieldType, value);
                }
                else
                {
                    if (fieldType.IsArray)
                    {
                        _contents[i] = JArray.Serialize(fieldType, value);
                    }
                    else
                    {
                        _contents[i] = JObject.Serialize(fieldType, value);;
                    }
                }
            }
        }
Пример #2
0
        public static JObject Serialize(Type type, object oSource)
        {
            var result  = new JObject();
            var methods = type.GetMethods();

            foreach (var m in methods)
            {
                if (!m.IsPublic)
                {
                    continue;
                }

                if (m.Name.IndexOf("get_") == 0)
                {
                    var name         = m.Name.Substring(4);
                    var methodResult = m.Invoke(oSource, null);
                    if (methodResult == null)
                    {
                        result._members.Add(name.ToLower(), new JProperty(name, JValue.Serialize(m.ReturnType, null)));
                    }
                    if (m.ReturnType.IsArray)
                    {
                        result._members.Add(name.ToLower(), new JProperty(name, JArray.Serialize(m.ReturnType, methodResult)));
                    }
                    else
                    {
                        result._members.Add(name.ToLower(), new JProperty(name, JObject.Serialize(m.ReturnType, methodResult)));
                    }
                }
            }

            var fields = type.GetFields();

            foreach (var f in fields)
            {
                if (f.FieldType.IsNotPublic)
                {
                    continue;
                }

                switch (f.MemberType)
                {
                case MemberTypes.Field:
                case MemberTypes.Property:
                    var value = f.GetValue(oSource);
                    if (value == null)
                    {
                        result._members.Add(f.Name, new JProperty(f.Name, JValue.Serialize(f.FieldType, null)));
                    }
                    else if (f.FieldType.IsValueType || f.FieldType == typeof(string))
                    {
                        result._members.Add(f.Name.ToLower(),
                                            new JProperty(f.Name, JValue.Serialize(f.FieldType, value)));
                    }
                    else
                    {
                        if (f.FieldType.IsArray)
                        {
                            result._members.Add(f.Name.ToLower(),
                                                new JProperty(f.Name, JArray.Serialize(f.FieldType, value)));
                        }
                        else
                        {
                            result._members.Add(f.Name.ToLower(),
                                                new JProperty(f.Name, JObject.Serialize(f.FieldType, value)));
                        }
                    }
                    break;

                default:
                    break;
                }
            }

            return(result);
        }