JsonDateTime
Namespace: SnowBank.Data.Json · class
Implements: JsonValue, IEquatable<JsonValue>, IComparable<JsonValue>, IJsonSerializable, IFormattable, ISliceSerializable, IConvertible, IParsable<JsonValue>, ISpanParsable<JsonValue>, ISpanFormattable, IUtf8SpanFormattable, INumberBase<JsonValue>, IUtf8SpanParsable<JsonValue>, IAdditionOperators<JsonValue, JsonValue, JsonValue>, IAdditiveIdentity<JsonValue, JsonValue>, IDecrementOperators<JsonValue>, IDivisionOperators<JsonValue, JsonValue, JsonValue>, IEqualityOperators<JsonValue, JsonValue, bool>, IIncrementOperators<JsonValue>, IMultiplicativeIdentity<JsonValue, JsonValue>, IMultiplyOperators<JsonValue, JsonValue, JsonValue>, ISubtractionOperators<JsonValue, JsonValue, JsonValue>, IUnaryNegationOperators<JsonValue, JsonValue>, IUnaryPlusOperators<JsonValue, JsonValue>, IComparisonOperators<JsonValue, JsonValue, bool>, IEquatable<JsonDateTime>, IEquatable<DateTime>, IEquatable<DateTimeOffset>, IEquatable<LocalDateTime>, IEquatable<LocalDate>
JSON DateTime
Constructors
JsonDateTime
JsonDateTime(DateTime value)
Wraps a DateTime instance
JsonDateTime(DateTimeOffset value)
Wraps a DateTimeOffset instance
JsonDateTime(DateOnly value)
Wraps a DateOnly instance
JsonDateTime(long ticks, DateTimeKind kind)
JsonDateTime(int year, int month, int day)
JsonDateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
JsonDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind)
Properties
Date
DateTime Date { get; }
DateWithOffset
DateTimeOffset DateWithOffset { get; }
DateWithoutTime
DateOnly DateWithoutTime { get; }
HasOffset
bool HasOffset { get; }
IsDefault
bool IsDefault { get; }
Tests if this value corresponds to the logical default for this type (0, false, null or missing, ...)
Returns: true for values like 0, false, or null; or false for non-zero integers, true, strings, arrays and objects
The empty string, array and object are NOT considered to be the default value of their type!
JsonNumber.Zero.IsDefault == true, JsonNumber.Return(123).IsDefault == false, JsonString.Return("").IsDefault == false, new JsonArray().IsDefault == false, new JsonObject().IsDefault == false
IsLocalTime
bool IsLocalTime { get; }
IsReadOnly
bool IsReadOnly { get; }
Returns true if this value is read-only, and cannot be modified, or false if it allows mutations.
Only JSON Objects and Arrays can return false. All other "value types" (string, boolean, numbers, ...) are always immutable, and will always be read-only.
If you need to modify a JSON Object or Array that is read-only, you should first create a copy, by calling either Copy or ToMutable, perform any changes required, and then either Freeze the copy, or call ToReadOnly again.
IsUtc
bool IsUtc { get; }
LocalDateTime
DateTime LocalDateTime { get; }
Ticks
long Ticks { get; }
Type
JsonType Type { get; }
Type of JSON value (Boolean, String, Number, Object, Array, ...)
UnixTime
long UnixTime { get; }
Number of milliseconds since Unix Epoch (1970-01-01 00:00:00.000 UTC)
UnixTimeDays
double UnixTimeDays { get; }
Number of days since Unix Epoch (1970-01-01 00:00:00.000 UTC)
UtcDateTime
DateTime UtcDateTime { get; }
UtcOffset
TimeSpan UtcOffset { get; }
UtcTicks
long UtcTicks { get; }
Methods
AreEqual
static bool AreEqual(DateTime left, DateTime right)
Tests if two dates are considered equal within the minimum supported precision
static bool AreEqual(DateTimeOffset left, DateTimeOffset right)
Tests if two dates are considered equal within the minimum supported precision
Bind
TValue Bind<TValue>(TValue defaultValue = null, ICrystalJsonTypeResolver resolver = null)
Bind this value into an instance of type TValue
defaultValue— Default value to return if the current instance is null or missingresolver— Optional custom resolver used to bind the value into a managed type.
Returns: An instance of the type TValue that is equivalent to the original JSON value, if there exists a valid conversion path. Otherwise, an exception will be thrown.
JsonNumber.Return(123).Bind
object Bind(Type type, ICrystalJsonTypeResolver resolver = null)
Bind this value into an instance of the specified type
type— Target managed typeresolver— Optional custom resolver used to bind the value into a managed type.
Returns: An instance of the target type that is equivalent to the original JSON value, if there exists a valid conversion path or convention. Otherwise, an exception will be thrown.
If the target type is a Value Type, the instance will be boxed, which may cause extra memory allocations. Consider calling instance, or use any of the convenience methods like , , ...
JsonNumber.Return(123).Bind(typeof(long)) will return a boxed Int64 with value 123.
CompareTo
int CompareTo(JsonValue other)
Compares two JSON values, and returns an integer that indicates whether the first value precedes, follows, or occurs in the same position in the sort order as the second value.
Equals
bool Equals(object obj)
bool Equals(JsonValue obj)
Tests if these two JSON values are considered equal, using the relaxed JSON comparison semantics
Two values are considered "equal" if they would produce the same result when serialized using a canonical representation.
Numbers are equal if they represent the "same" number (ie: 1 and 1.0 are equal)
Numbers and strings can be equal, if the later can be parsed into a number equal to the former (ie: 123 and "123" are equal)
Arrays are equal if they have the same length and all their elements are equals pairwise
Objects are equal if they have the same key/value pairs, irrespective of their order.
A Missing field is not considered equal to an explicit Null field, and vice versa
If you require strict equality (ie: "123" and 123 are different), consider calling StrictEquals instead.
bool Equals(JsonDateTime obj)
bool Equals(DateTime value)
bool Equals(DateTimeOffset value)
bool Equals(LocalDateTime value)
bool Equals(LocalDate value)
GetHashCode
int GetHashCode()
Returns a hash code that can be used to quickly identify a JSON value.
The hash code is guaranteed to remain unchanged during the lifetime of the object.
Caution: there is NO guarantee that two equivalent Objects or Arrays will have the same hash code! This means that it is NOT safe to use a JSON object or array has the key of a Dictionary or other collection that uses hash codes to quickly compare two instances.
JsonSerialize
void JsonSerialize(CrystalJsonWriter writer)
Serializes this instance as JSON
writer— Writer that will output the content of this instance
Return
static JsonDateTime Return(DateTime value)
Returns the equivalent JsonDateTime
static JsonValue Return(DateTime? value)
Returns the equivalent JsonDateTime
static JsonDateTime Return(DateTimeOffset value)
Returns the equivalent JsonDateTime
static JsonValue Return(DateTimeOffset? value)
Returns the equivalent JsonDateTime
static JsonDateTime Return(DateOnly value)
Returns the equivalent JsonDateTime
static JsonValue Return(DateOnly? value)
Returns the equivalent JsonDateTime
static JsonValue Return(Instant value)
static JsonValue Return(Instant? value)
static JsonValue Return(ZonedDateTime value)
static JsonValue Return(ZonedDateTime? value)
static JsonValue Return(LocalDateTime value)
static JsonValue Return(LocalDateTime? value)
static JsonValue Return(LocalDate value)
static JsonValue Return(LocalDate? value)
static JsonValue Return(LocalTime value)
static JsonValue Return(LocalTime? value)
static JsonValue Return(OffsetDateTime value)
static JsonValue Return(OffsetDateTime? value)
StrictEquals
bool StrictEquals(JsonValue other)
Tests if these two JSON values are considered equal, using the strict JSON comparison semantics
For two values to be considered "equal", they must be of the same type (string, number, array, ...) AND have the same result when serialized using a canonical representation.
Arrays are equals if they have the same length and all their elements are also strictly equal.
Objects are equal if they have the same keys, irrespective of their order, and each value is strictly equal.
A Missing field is not considered equal to an explicit Null field, and vice versa
bool StrictEquals(JsonDateTime other)
bool StrictEquals(JsonString other)
ToBoolean
bool ToBoolean(bool _ = false)
Returns the equivalent Boolean, if there exists a valid conversion
ToDateOnly
DateOnly ToDateOnly(DateOnly _ = null)
Returns the equivalent DateOnly, if there exists a valid conversion
ToDateTime
DateTime ToDateTime(DateTime _ = null)
Returns the equivalent DateTime, if there exists a valid conversion
ToDateTimeOffset
DateTimeOffset ToDateTimeOffset(DateTimeOffset _ = null)
Returns the equivalent DateTimeOffset, if there exists a valid conversion
ToDecimal
decimal ToDecimal(decimal _ = 0)
Returns the number of days elapsed since January 1st 1970 UTC
ToDouble
double ToDouble(double _ = 0)
Returns the number of days elapsed since January 1st 1970 UTC
ToDuration
Duration ToDuration(Duration _ = null)
Returns the elapsed time since January 1st 1970 UTC
ToInstant
Instant ToInstant(Instant _ = null)
Returns the equivalent Instant, if there exists a valid conversion
ToInt32
int ToInt32(int _ = 0)
Returns the number of milliseconds elapsed since Unix Epoch (1970-01-01 00:00:00.000 UTC)
Note: will throw after 2038 instead of returning a negative number. Please use instead, wich returns a .
ToInt64
long ToInt64(long _ = 0)
Returns the number of ticks
Similar to
ToJsonText
string ToJsonText(CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)
Serializes this JSON value into a JSON string literal
settings— Settings used to change the serialized JSON output (optional)resolver— Optional custom resolver.
Returns: JSON text literal that can be written to disk, returned as the body of an HTTP request, or
var jsonText = new JsonObject() { ["hello"] = "world" }.ToJsonText(); // == "{ "hello": "world" }"
ToLocalDate
LocalDate ToLocalDate()
ToLocalDateTime
LocalDateTime ToLocalDateTime()
ToLocalTime
DateTime ToLocalTime()
ToObject
object ToObject()
Converts this into a CLR object (with a type that matches the value)
Prefer casting to a specific type, using As
ToSingle
float ToSingle(float _ = 0)
Returns the number of days elapsed since January 1st 1970 UTC
ToString
string ToString(string format, IFormatProvider provider = null)
Converts this JSON value into a printable string, using the specified format and provider
format— Desired format, or "D" (default) if omittedprovider— This parameter is ignored. JSON values are always formatted usingInvariantCulture.
Supported values for are:
- D Default, returns the value in the default format (no quotes for strings, ...)
- N Normal, equivalent to calling ToJsonText with Json
- C Compact, equivalent to calling ToJsonText with JsonCompact
- P Pretty, equivalent to calling ToJsonText with JsonIndented
- Q Quick, equivalent to calling
GetCompactRepresentation, that will return a simplified/partial version, suitable for logs/traces. - J JavaScript, equivalent to calling ToJsonText with JavaScript.
- B returns a doubly-encoded JSON version of this value, that can be passed through inside another JSON payload, or copy-pasted as a valid C# string literal.
ToTimeSpan
TimeSpan ToTimeSpan(TimeSpan _ = null)
Returns the elapsed time since January 1st 1970 UTC
ToUInt32
uint ToUInt32(uint _ = 0)
Returns the number of milliseconds elapsed since Unix Epoch (1970-01-01 00:00:00.000 UTC)
Note: will throw for years after 2100, for reasons similar to the Y2038 bug
ToUInt64
ulong ToUInt64(ulong _ = 0)
Returns the number of ticks
Similar to
ToUniversalTime
DateTime ToUniversalTime()
TryConvertToString
bool TryConvertToString(out string value)
TryFormat
bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = null, IFormatProvider provider = null)
bool TryFormat(Span<byte> destination, out int bytesWritten, ReadOnlySpan<char> format = null, IFormatProvider provider = null)
WriteTo
void WriteTo(ref SliceWriter writer)
Serializes the current instance to the specified output buffer
Fields
DateOnlyMaxValue
static readonly JsonDateTime DateOnlyMaxValue
Singleton equivalent to MinValue
MaxValue
static readonly JsonDateTime MaxValue
Singleton equivalent to MinValue
MinValue
static readonly JsonDateTime MinValue
Singleton equivalent to MinValue
PrecisionTicks
static readonly long PrecisionTicks
Default precision for JSON dates (due to truncation during serialization)
Dates that differ by less than this value will be considered equal, and produce the same JSON text literal