/// <summary>Creates array at current writer level.</summary> public static void SerializeTo(this IEnumerable obj, string elementName, ITreeWriter writer) { // Write start element tag writer.WriteStartArrayElement(elementName); // Iterate over sequence elements foreach (var item in obj) { // Write array item start tag writer.WriteStartArrayItem(); // Serialize based on type of the item switch (item) { case null: case string stringItem: case double doubleItem: case bool boolItem: case int intItem: case long longItem: // Write item as is for these types writer.WriteStartValue(); writer.WriteValue(item); writer.WriteEndValue(); break; case IEnumerable enumerableItem: throw new Exception($"Serialization is not supported for element {elementName} " + $"which is collection containing another collection."); case Data dataItem: if (dataItem is Key) { // Write key as serialized delimited string writer.WriteStartValue(); writer.WriteValue(dataItem.AsString()); writer.WriteEndValue(); } else { // Embedded data element dataItem.SerializeTo(writer); } break; default: // Argument type is unsupported, error message throw new Exception( $"Element type {item.GetType()} is not supported for tree serialization."); } // Write array item end tag writer.WriteEndArrayItem(); } // Write matching end element tag writer.WriteEndArrayElement(elementName); }
/// <summary>Write an array of elements with no inner nodes. /// Element type is inferred by calling obj.GetType().</summary> public static void WriteValueArray(this ITreeWriter obj, string elementName, IEnumerable <object> values) { obj.WriteStartArrayElement(elementName); foreach (object value in values) { obj.WriteArrayItem(value); } obj.WriteEndArrayElement(elementName); }