/// <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 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(); }
/// <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(); }
/// <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); } }
private static void TestIntegers(TreeFormat format) { TreeSerializationSettings settings = new TreeSerializationSettings() { LeaveStreamOpen = true }; using (MemoryStream stream = new MemoryStream()) { using (ITreeWriter writer = Writer(format, stream, settings)) { writer.WriteStartArray(); for (int i = -1; i < 300; i += 7) { writer.WriteValue(i); } writer.WriteEndArray(); } long bytesWritten = stream.Position; stream.Seek(0, SeekOrigin.Begin); using (ITreeReader reader = Reader(format, stream, settings)) { Assert.Equal(TreeToken.StartArray, reader.TokenType); for (int i = -1; i < 300; i += 7) { Assert.True(reader.Read()); Assert.Equal(TreeToken.Integer, reader.TokenType); Assert.Equal(i, reader.ReadAsInt32()); } Assert.True(reader.Read()); Assert.Equal(TreeToken.EndArray, reader.TokenType); Assert.False(reader.Read()); } Assert.Equal(bytesWritten, stream.Position); } }
public static void Write(this ITreeWriter writer, string name, string value) { writer.WritePropertyName(name); writer.WriteValue(value); }
public static void WriteValue(this ITreeWriter writer, Guid value) { string writtenValue = value.ToString("D"); writer.WriteValue(writtenValue); }
public static void WriteValue(this ITreeWriter writer, DateTime value) { long writtenValue = value.ToUniversalTime().Ticks; writer.WriteValue(writtenValue); }