/// <summary> /// Converts to query data. /// </summary> /// <returns>The query data.</returns> public virtual QueryData ToQueryData(QueryData q) { if (q == null) { q = new QueryData(); } if (string.IsNullOrWhiteSpace(ThreadId)) { q.Set("thread", ThreadId); } if (string.IsNullOrWhiteSpace(SenderAddress)) { q.Set("addr", SenderAddress); } if (SendStartTime.HasValue) { q.Set("start", WebFormat.ParseDate(SendStartTime.Value).ToString("g", CultureInfo.InvariantCulture)); } if (SendEndTime.HasValue) { q.Set("end", WebFormat.ParseDate(SendEndTime.Value).ToString("g", CultureInfo.InvariantCulture)); } if (Priority.HasValue) { q.Set("priority", ((int)Priority.Value).ToString("g", CultureInfo.InvariantCulture)); } if (Flag.HasValue) { q.Set("flag", ((int)Flag.Value).ToString("g", CultureInfo.InvariantCulture)); } return(q); }
/// <summary> /// Adds a string to a collection of System.Net.Http.HttpContent objects that get serialized to multipart/form-data MIME type. /// </summary> /// <param name="content">The HTTP content of multipart form data.</param> /// <param name="name">The name for the HTTP content to add.</param> /// <param name="value">The property value.</param> /// <param name="dateFormat">A standard or custom date and time format string.</param> /// <param name="encoding">The character encoding to use.</param> /// <return>The HTTP content to add.</return> /// <exception cref="FormatException"> /// The length of format is 1, and it is not one of the format specifier characters defined for System.Globalization.DateTimeFormatInfo. /// -or- format does not contain a valid custom format pattern. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// The date and time is outside the range of dates supported by the calendar used by the current culture. /// </exception> public static StringContent Add(this MultipartFormDataContent content, string name, DateTime value, string dateFormat = null, Encoding encoding = null) { if (content == null) { return(null); } var c = new StringContent(string.IsNullOrWhiteSpace(dateFormat) ? WebFormat.ParseDate(value).ToString() : value.ToString(dateFormat), encoding ?? Encoding.UTF8); content.Add(c, name); return(c); }
/// <inheritdoc /> public override DateTime?Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.Null || reader.TokenType == JsonTokenType.False) { return(null); } if (reader.TokenType == JsonTokenType.String) { return(WebFormat.ParseDate(reader.GetString())); } return(WebFormat.ParseDate(reader.GetInt64())); }
/// <inheritdoc /> public override void Write(Utf8JsonWriter writer, DateTime?value, JsonSerializerOptions options) { var num = WebFormat.ParseDate(value); if (num.HasValue) { writer.WriteNumberValue(num.Value); } else { writer.WriteNullValue(); } }
/// <inheritdoc /> public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.String) { var v = WebFormat.ParseDate(reader.GetString()); if (v.HasValue) { return(v.Value); } throw new JsonException("The format is not correct.", new FormatException("The value should be a date time JSON token format.")); } return(WebFormat.ParseDate(reader.GetInt64())); }
/// <summary> /// Converts to JSON format string. /// </summary> /// <param name="options">The data contract serializer settings.</param> /// <returns>A JSON format string.</returns> public virtual string ToJsonString(DataContractJsonSerializerSettings options) { var m = new FragmentModel { Id = Id, Index = Index, State = State.ToString().ToLowerInvariant(), Tag = Tag, Creation = WebFormat.ParseDate(Creation), Modification = WebFormat.ParseDate(Modification) }; return(StringExtensions.ToJson(m, options)); }
/// <summary> /// Sets the System.Runtime.Serialization.SerializationInfo object with the parameter name and additional exception information. /// </summary> /// <param name="info">The object that holds the serialized object data.</param> /// <param name="context">The contextual information about the source or destination.</param> public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { return; } info.AddValue(nameof(Id), Id, typeof(string)); info.AddValue(nameof(Index), Index, typeof(int)); info.AddValue(nameof(State), State.ToString().ToLowerInvariant(), typeof(string)); if (Tag != null) { info.AddValue(nameof(Tag), Tag, typeof(string)); } info.AddValue(nameof(Creation), WebFormat.ParseDate(Creation), typeof(long)); info.AddValue(nameof(Modification), WebFormat.ParseDate(Modification), typeof(long)); }
/// <summary> /// Converts to query data. /// </summary> /// <returns>A query data instance.</returns> public virtual QueryData ToQueryData() { var q = new QueryData { [nameof(Id)] = Id, [nameof(Index)] = Index.ToString(CultureInfo.InvariantCulture), [nameof(State)] = State.ToString().ToLowerInvariant() }; if (Tag != null) { q[nameof(Tag)] = Tag; } q[nameof(Creation)] = WebFormat.ParseDate(Creation).ToString(CultureInfo.InvariantCulture); q[nameof(Modification)] = WebFormat.ParseDate(Modification).ToString(CultureInfo.InvariantCulture); return(q); }
/// <inheritdoc /> public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { var num = WebFormat.ParseDate(value); writer.WriteNumberValue(num); }
/// <summary> /// Parses from a JSON string. /// </summary> /// <param name="s">The string to parse.</param> public static Fragment Parse(string s) { if (string.IsNullOrWhiteSpace(s)) { throw new ArgumentNullException(nameof(s), "str should not be null or empty."); } s = s.Trim(); if (s.IndexOf("<") == 0) { var xml = XElement.Parse(s); string id = null; var index = 0; string stateStr = null; string tag = null; long? creation = null; long? modification = null; foreach (var ele in xml.Elements()) { if (string.IsNullOrWhiteSpace(ele?.Value)) { continue; } switch (ele.Name?.LocalName?.ToLowerInvariant()) { case "id": id = ele.Value; break; case "index": if (!int.TryParse(ele.Value, out index)) { index = 0; } break; case "state": stateStr = ele.Value; break; case "tag": tag = ele.Value; break; case "creation": if (long.TryParse(ele.Value, out var creationV)) { creation = creationV; } break; case "update": case "modification": if (long.TryParse(ele.Value, out var modificationV)) { modification = modificationV; } break; } } if (string.IsNullOrWhiteSpace(stateStr) || !Enum.TryParse(stateStr, true, out FragmentStates state2)) { state2 = FragmentStates.Pending; } return(new Fragment(id, index, state2, WebFormat.ParseDate(creation), WebFormat.ParseDate(modification), tag)); } if (s.IndexOfAny(new[] { '\"', '{' }) < 0) { var q = QueryData.Parse(s); var stateStr = q["State"] ?? q["state"]; if (!int.TryParse(q["Index"] ?? q["index"], out var index)) { index = 0; } if (string.IsNullOrWhiteSpace(stateStr) || !Enum.TryParse(stateStr, true, out FragmentStates state2)) { state2 = FragmentStates.Pending; } long?creation = null; long?modification = null; if (long.TryParse(q["Creation"] ?? q["creation"], out var creationV)) { creation = creationV; } if (long.TryParse(q["Modification"] ?? q["modification"] ?? q["Update"] ?? q["update"], out var modificationV)) { modification = modificationV; } return(new Fragment( q["Id"] ?? q["ID"] ?? q["id"], index, state2, WebFormat.ParseDate(creation), WebFormat.ParseDate(modification), q["Tag"] ?? q["tag"] )); } var m = JsonSerializer.Deserialize <FragmentModel>(s); if (string.IsNullOrWhiteSpace(m.State) || !Enum.TryParse(m.State, true, out FragmentStates state)) { state = FragmentStates.Pending; } return(new Fragment(m.Id, m.Index ?? 0, state, WebFormat.ParseDate(m.Creation), WebFormat.ParseDate(m.Modification), m.Tag)); }
/// <summary> /// Initializes a new instance of the Fragment class. /// </summary> /// <param name="info">The System.Runtime.Serialization.SerializationInfo that holds the serialized object data about the exception being thrown.</param> /// <param name="context">The System.Runtime.Serialization.StreamingContext that contains contextual information about the source or destination.</param> protected Fragment(SerializationInfo info, StreamingContext context) { if (info == null) { Id = Guid.NewGuid().ToString("n"); return; } try { var id = info.GetString(nameof(Id)); if (id != null) { Id = id; } } catch (SerializationException) { } try { Index = info.GetInt32(nameof(Index)); } catch (SerializationException) { } try { var stateStr = info.GetString(nameof(State)); if (!string.IsNullOrWhiteSpace(stateStr) && Enum.TryParse(stateStr, true, out FragmentStates state)) { State = state; } } catch (SerializationException) { } try { Tag = info.GetString(nameof(Tag)); } catch (SerializationException) { } try { Creation = WebFormat.ParseDate(info.GetInt64(nameof(Creation))); } catch (SerializationException) { } try { Modification = WebFormat.ParseDate(info.GetInt64(nameof(Modification))); } catch (SerializationException) { } }
/// <summary> /// Gets the query value as a date and time by a specific key. /// </summary> /// <param name="key">The key.</param> /// <param name="useUnixTimestamp">true if the value is unix timestamp; otherwise, false.</param> /// <returns>The query value as DateTime.</returns> public DateTime?TryGetDateTimeValue(string key, bool useUnixTimestamp = false) { var v = GetFirstValue(key, true); var l = TryGetInt64Value(key); return(l.HasValue ? (useUnixTimestamp ? WebFormat.ParseUnixTimestamp(l.Value) : WebFormat.ParseDate(l.Value)) : WebFormat.ParseDate(v)); }