/// <summary> /// <see cref="Object.ToString()"/> /// </summary> public override string ToString() { StringBuilder sb = new StringBuilder(); if (this.negation) { sb.Append('-'); } sb.Append('P'); if (this.years != 0) { sb.Append(this.years.ToString()); sb.Append('Y'); } if (this.months != 0) { sb.Append(this.months.ToString()); sb.Append('M'); } if (this.days != 0) { sb.Append(this.days.ToString()); sb.Append('D'); } if (this.hours != 0 || this.minutes != 0 || this.seconds != 0) { sb.Append('T'); if (this.hours != 0) { sb.Append(this.hours.ToString()); sb.Append('H'); } if (this.minutes != 0) { sb.Append(this.minutes.ToString()); sb.Append('M'); } if (this.seconds != 0) { sb.Append(CommonTypes.Encode(this.seconds)); sb.Append('S'); } } string Result = sb.ToString(); if (Result.Length <= 2) { Result += "T0S"; } return(Result); }
private static void Encode(object Object, int?Indent, StringBuilder Json) { if (Object is null) { Json.Append("null"); } else { Type T = Object.GetType(); TypeInfo TI = T.GetTypeInfo(); if (TI.IsValueType) { if (Object is bool b) { Json.Append(CommonTypes.Encode(b)); } else if (Object is char ch) { Json.Append('"'); Json.Append(Encode(new string(ch, 1))); Json.Append('"'); } else if (Object is double dbl) { Json.Append(CommonTypes.Encode(dbl)); } else if (Object is float fl) { Json.Append(CommonTypes.Encode(fl)); } else if (Object is decimal dec) { Json.Append(CommonTypes.Encode(dec)); } else if (TI.IsEnum) { Json.Append('"'); Json.Append(Encode(Object.ToString())); Json.Append('"'); } else if (Object is DateTime TP) { Json.Append(((int)((TP.ToUniversalTime() - UnixEpoch).TotalSeconds)).ToString()); } else { Json.Append(Object.ToString()); } } else if (Object is string s) { Json.Append('"'); Json.Append(Encode(s)); Json.Append('"'); } else if (Object is IEnumerable <KeyValuePair <string, object> > Obj) { Encode(Obj, Indent, Json, null); } else if (Object is IEnumerable E) { IEnumerator e = E.GetEnumerator(); bool First = true; Json.Append('['); if (Indent.HasValue) { Indent = Indent + 1; } while (e.MoveNext()) { if (First) { First = false; } else { Json.Append(','); } if (Indent.HasValue) { Json.AppendLine(); Json.Append(new string('\t', Indent.Value)); } Encode(e.Current, Indent, Json); } if (!First && Indent.HasValue) { Json.AppendLine(); Indent = Indent - 1; Json.Append(new string('\t', Indent.Value)); } Json.Append(']'); } else { throw new ArgumentException("Unsupported type: " + T.FullName, nameof(Object)); } } }
private static void Encode(object Object, int?Indent, StringBuilder Json) { if (Object == null) { Json.Append("null"); } else { Type T = Object.GetType(); TypeInfo TI = T.GetTypeInfo(); if (TI.IsValueType) { if (Object is bool b) { Json.Append(CommonTypes.Encode(b)); } else if (Object is char ch) { Json.Append('"'); Json.Append(Encode(new string(ch, 1))); Json.Append('"'); } else if (Object is double dbl) { Json.Append(CommonTypes.Encode(dbl)); } else if (Object is float fl) { Json.Append(CommonTypes.Encode(fl)); } else if (Object is decimal dec) { Json.Append(CommonTypes.Encode(dec)); } else if (TI.IsEnum) { Json.Append('"'); Json.Append(Encode(Object.ToString())); Json.Append('"'); } else { Json.Append(Object.ToString()); } } else if (Object is string s) { Json.Append('"'); Json.Append(Encode(s)); Json.Append('"'); } else if (Object is Dictionary <string, object> Obj) { bool First = true; Json.Append('{'); if (Indent.HasValue) { Indent = Indent + 1; } foreach (KeyValuePair <string, object> Member in Obj) { if (First) { First = false; } else { Json.Append(','); } if (Indent.HasValue) { Json.AppendLine(); Json.Append(new string('\t', Indent.Value)); } Json.Append('"'); Json.Append(Encode(Member.Key)); Json.Append("\":"); if (Indent.HasValue) { Json.Append(' '); } Encode(Member.Value, Indent, Json); } if (!First) { Json.AppendLine(); if (Indent.HasValue) { Indent = Indent - 1; } Json.Append(new string('\t', Indent.Value)); } Json.Append('}'); } else if (Object is IEnumerable E) { IEnumerator e = E.GetEnumerator(); bool First = true; Json.Append('['); if (Indent.HasValue) { Indent = Indent + 1; } while (e.MoveNext()) { if (First) { First = false; } else { Json.Append(','); } if (Indent.HasValue) { Json.AppendLine(); Json.Append(new string('\t', Indent.Value)); } Encode(e.Current, Indent, Json); } if (!First) { Json.AppendLine(); if (Indent.HasValue) { Indent = Indent - 1; } Json.Append(new string('\t', Indent.Value)); } Json.Append(']'); } else { throw new ArgumentException("Unsupported type: " + T.FullName, nameof(Object)); } } }
private static void Encode(object Object, int?Indent, StringBuilder Json) { if (Object is null) { Json.Append("null"); } else { Type T = Object.GetType(); TypeInfo TI = T.GetTypeInfo(); if (TI.IsValueType) { if (Object is bool b) { Json.Append(CommonTypes.Encode(b)); } else if (Object is char ch) { Json.Append('"'); Json.Append(Encode(new string(ch, 1))); Json.Append('"'); } else if (Object is double dbl) { Json.Append(CommonTypes.Encode(dbl)); } else if (Object is float fl) { Json.Append(CommonTypes.Encode(fl)); } else if (Object is decimal dec) { Json.Append(CommonTypes.Encode(dec)); } else if (TI.IsEnum) { Json.Append('"'); Json.Append(Encode(Object.ToString())); Json.Append('"'); } else if (Object is DateTime TP) { Json.Append(((int)((TP.ToUniversalTime() - UnixEpoch).TotalSeconds)).ToString()); } else { Json.Append(Object.ToString()); } } else if (Object is string s) { Json.Append('"'); Json.Append(Encode(s)); Json.Append('"'); } else if (Object is IEnumerable <KeyValuePair <string, object> > Obj) { Encode(Obj, Indent, Json, null); } else if (Object is IEnumerable <KeyValuePair <string, IElement> > Obj2) { Encode(Obj2, Indent, Json); } else if (Object is IEnumerable E) { IEnumerator e = E.GetEnumerator(); bool First = true; Json.Append('['); if (Indent.HasValue) { Indent++; } while (e.MoveNext()) { if (First) { First = false; } else { Json.Append(','); } if (Indent.HasValue) { Json.AppendLine(); Json.Append(new string('\t', Indent.Value)); } Encode(e.Current, Indent, Json); } if (!First && Indent.HasValue) { Json.AppendLine(); Indent--; Json.Append(new string('\t', Indent.Value)); } Json.Append(']'); } else if (Object is ObjectMatrix M && !(M.ColumnNames is null)) { bool First = true; Json.Append('['); if (Indent.HasValue) { Indent++; } foreach (IElement Element in M.VectorElements) { if (First) { First = false; Encode(M.ColumnNames, Indent, Json); } Json.Append(','); if (Indent.HasValue) { Json.AppendLine(); Json.Append(new string('\t', Indent.Value)); } Encode(Element.AssociatedObjectValue, Indent, Json); } if (!First && Indent.HasValue) { Json.AppendLine(); Indent--; Json.Append(new string('\t', Indent.Value)); } Json.Append(']'); }