Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
 /// <summary>Write an array item with no inner nodes.
 /// Element type is inferred by calling obj.GetType().</summary>
 public static void WriteValueArrayItem(this ITreeWriter obj, object value)
 {
     // Writes null or empty value as BSON null
     obj.WriteStartArrayItem();
     obj.WriteStartValue();
     obj.WriteValue(value);
     obj.WriteEndValue();
     obj.WriteEndArrayItem();
 }
Exemplo n.º 3
0
 /// <summary>Write a single array item.</summary>
 public static void WriteArrayItem(this ITreeWriter obj, object value)
 {
     // Will serialize null or empty value
     obj.WriteStartArrayItem();
     obj.WriteStartValue();
     obj.WriteValue(value);
     obj.WriteEndValue();
     obj.WriteEndArrayItem();
 }
Exemplo n.º 4
0
 /// <summary>Write an element with no inner nodes.
 /// Element type is inferred by calling obj.GetType().</summary>
 public static void WriteValueElement(this ITreeWriter obj, string elementName, object value)
 {
     // Do not serialize null or empty value
     if (!value.IsEmpty())
     {
         obj.WriteStartElement(elementName);
         obj.WriteStartValue();
         obj.WriteValue(value);
         obj.WriteEndValue();
         obj.WriteEndElement(elementName);
     }
 }