Exemple #1
0
 public static IJsonWriter Node(this IJsonWriter writer, IJsonNode node)
 {
   switch (node.Type)
   {
     case JsonNodeType.Array:
       if (!string.IsNullOrEmpty(node.Name)) writer.Prop(node.Name);
       writer.Array();
       break;
     case JsonNodeType.ArrayEnd:
       writer.ArrayEnd();
       break;
     case JsonNodeType.Object:
       if (!string.IsNullOrEmpty(node.Name)) writer.Prop(node.Name);
       writer.Object();
       break;
     case JsonNodeType.ObjectEnd:
       writer.ObjectEnd();
       break;
     case JsonNodeType.SimpleProperty:
       writer.Prop(node.Name, node.Value);
       break;
     default:
       writer.Value(node.Value);
       break;
   }
   return writer;
 }
        private static void JsonSerializeObject(this JsonWriter writer,
                                                object obj)
        {
            if (null == obj)
            {
                return;
            }

            var properties = obj
                .GetType()
                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .ToList();
            foreach (var property in properties)
            {
                if (!property.CanRead)
                {
                    continue;
                }

                if (0 != property.GetIndexParameters().Length)
                {
                    continue;
                }

                var ignore = property.GetCustomAttributes(typeof(JsonIgnoreAttribute), true);
                if (0 != ignore.Length)
                {
                    continue;
                }

                var name = property.Name.ToCamelCase();
                foreach (JsonNameAttribute attribute in property.GetCustomAttributes(typeof(JsonNameAttribute), true))
                {
                    name = attribute.Name;
                }

                var value = property.GetValue(obj, null);

                if (writer.JsonSerializeBaseClassLibraryType(name, value))
                {
                    continue;
                }

                writer.Object(name);

                var serializable = value as IJsonSerializable;
                if (null == serializable)
                {
                    writer.JsonSerializeObject(value);
                }
                else
                {
                    serializable.WriteJson(writer);
                    writer.EndObject();
                }
            }

            var list = obj as IEnumerable;
            if (null != list)
            {
                writer.Array(writer.Settings.ItemsName);
                foreach (var item in list)
                {
                    if (writer.JsonSerializeBaseClassLibraryType(null, item))
                    {
                        continue;
                    }

                    writer.Object();
                    writer.JsonSerializeObject(item);
                }

                writer.EndArray();
            }

            writer.EndObject();
        }
 public static IEmitVal Array(
     this IEmitBlock block,
     IEmitType elementType,
     IEnumerable<IEmitVal> elements)
 {
     return block.Array(elementType, elements.ToArray());
 }