/// <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)); }
/// <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.ParseUnixTimestamp(reader.GetInt64())); }
/// <inheritdoc /> public override void Write(Utf8JsonWriter writer, DateTime?value, JsonSerializerOptions options) { var num = WebFormat.ParseUnixTimestamp(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.Number) { return(WebFormat.ParseUnixTimestamp(reader.GetInt64())); } 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.")); }
/// <inheritdoc /> public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { var num = WebFormat.ParseUnixTimestamp(value); writer.WriteNumberValue(num); }