Ejemplo n.º 1
0
 /// <summary>WriteEndDict(...) followed by WriteEndArrayItem(...).</summary>
 public static void WriteEndDictArrayItem(this ITreeWriter obj)
 {
     obj.WriteEndDict();
     obj.WriteEndArrayItem();
 }
Ejemplo n.º 2
0
        /// <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();
        }
Ejemplo n.º 3
0
 /// <summary>WriteEndDict(...) followed by WriteEndElement(...).</summary>
 public static void WriteEndDictElement(this ITreeWriter obj, string elementName)
 {
     obj.WriteEndDict();
     obj.WriteEndElement(elementName);
 }