Example #1
0
        private static JsonObject serializeObject(object obj)
        {
            PropertyInfo[] propInfo = obj.GetType().GetProperties();
            string propertyName;
            object propertyValue;
            JsonValueType type;

            JsonObject o = new JsonObject(propInfo.Length);
            foreach (var p in propInfo)
            {
                propertyName = p.Name;
                propertyValue = p.GetValue(obj);
                type = JsonValue.GetPrimitiveType(p.PropertyType);
                if (type == JsonValueType.Object)
                {
                    JsonObject o2 = serializeObject(propertyValue);
                    o.Add(propertyName, o2);
                    continue;
                }
                else if (type == JsonValueType.Array)
                {
                    JsonArray a = serializeArray((IEnumerable)propertyValue);
                    o.Add(propertyName, a);
                    continue;
                }
                else
                {
                    string value = propertyValue.ToString();
                    JsonPrimitive primitive = new JsonPrimitive(value, type);
                    o.Add(propertyName, primitive);
                }
            }
            return o;
        }