/// <summary>WriteStartArrayItem(...) followed by WriteStartDict().</summary> public static void WriteStartDictArrayItem(this ITreeWriter obj) { obj.WriteStartArrayItem(); obj.WriteStartDict(); }
/// <summary>Serialize by writing into ITreeWriter.</summary> public void SerializeTo(ITreeWriter writer) { // Write start tag writer.WriteStartDict(); // Iterate over the list of elements var innerElementInfoList = DataTypeInfo.GetOrCreate(this).DataElements; foreach (var innerElementInfo in innerElementInfoList) { // Get element name and value string innerElementName = innerElementInfo.Name; object innterElementValue = innerElementInfo.GetValue(this); // Serialize based on type of element value switch (innterElementValue) { case null: // Do not serialize null value break; case string stringValue: case double doubleValue: case bool boolValue: case int intValue: case long longValue: case LocalDate dateValue: case LocalTime timeValue: case LocalMinute minuteValue: case LocalDateTime dateTimeValue: case Instant instantValue: case Enum enumValue: // Embedded as string value writer.WriteValueElement(innerElementName, innterElementValue); break; case IEnumerable enumerableElement: // Embedded enumerable such as array or list enumerableElement.SerializeTo(innerElementName, writer); break; case Data dataElement: if (dataElement is Key) { // Embedded as string key writer.WriteValueElement(innerElementName, innterElementValue); break; } else { // Embedded as data writer.WriteStartElement(innerElementName); dataElement.SerializeTo(writer); writer.WriteEndElement(innerElementName); } break; case TemporalId idElement: // Do not serialize break; default: // Argument type is unsupported, error message throw new Exception($"Element type {innerElementInfo.PropertyType} is not supported for tree serialization."); } } // Write end tag writer.WriteEndDict(); }
/// <summary>WriteStartElement(...) followed by WriteStartDict().</summary> public static void WriteStartDictElement(this ITreeWriter obj, string elementName) { obj.WriteStartElement(elementName); obj.WriteStartDict(); }