fix(commons): codec handles non-finite list floats, ISO-8601 UTC scalar DateTime, JSON null converts to null (CMN-3, CMN-4, CMN-10, WP3 task 3.11)
Wire stays compatible: the scalar DateTime is a string either way and every reader parses with RoundtripKind/invariant, which still accepts the old invariant general form.
This commit is contained in:
@@ -14,7 +14,26 @@ namespace ZB.MOM.WW.ScadaBridge.Commons.Types;
|
||||
/// </summary>
|
||||
public static class AttributeValueCodec
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOpts = new() { WriteIndented = false };
|
||||
// AllowNamedFloatingPointLiterals: a List<double>/List<float> holding NaN or ±Infinity
|
||||
// (a bad-scaled analog, a divide-by-zero) encodes as the JSON strings "NaN" /
|
||||
// "Infinity" / "-Infinity" instead of throwing ArgumentException from the serializer
|
||||
// (CMN-3). Decode reads them back through the invariant-culture double/float parse,
|
||||
// which accepts the same literals.
|
||||
private static readonly JsonSerializerOptions JsonOpts = new()
|
||||
{
|
||||
WriteIndented = false,
|
||||
NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The scalar DateTime wire format: ISO-8601 round-trip (<c>"O"</c>), always UTC, with
|
||||
/// full sub-second precision. Replaced the invariant general format
|
||||
/// (<c>"06/15/2026 13:45:30"</c> — no zone, whole seconds) that the
|
||||
/// <see cref="IFormattable"/> branch used to produce (CMN-4). Every reader parses with
|
||||
/// <see cref="DateTimeStyles.RoundtripKind"/> + invariant culture, which still accepts
|
||||
/// the old form, so values persisted or sent by an older node keep decoding.
|
||||
/// </summary>
|
||||
private const string DateTimeWireFormat = "O";
|
||||
|
||||
/// <summary>Encodes a value to its canonical string form.</summary>
|
||||
/// <param name="value">The value to encode; scalars use invariant-culture formatting, enumerables encode as a JSON array.</param>
|
||||
@@ -25,6 +44,12 @@ public static class AttributeValueCodec
|
||||
{
|
||||
case null: return null;
|
||||
case string s: return s; // already canonical
|
||||
// DateTime/DateTimeOffset before the generic IFormattable branch: ISO-8601
|
||||
// round-trip so the scalar keeps its zone and sub-second precision (CMN-4).
|
||||
// All timestamps are UTC system-wide, so an Unspecified kind is taken as UTC
|
||||
// and a Local one is converted.
|
||||
case DateTime dt: return ToUtc(dt).ToString(DateTimeWireFormat, CultureInfo.InvariantCulture);
|
||||
case DateTimeOffset dto: return dto.ToString(DateTimeWireFormat, CultureInfo.InvariantCulture);
|
||||
case IFormattable f: return f.ToString(null, CultureInfo.InvariantCulture);
|
||||
case IEnumerable e:
|
||||
// Native-typed JSON: serialize the runtime collection so System.Text.Json emits
|
||||
@@ -35,6 +60,13 @@ public static class AttributeValueCodec
|
||||
}
|
||||
}
|
||||
|
||||
private static DateTime ToUtc(DateTime dt) => dt.Kind switch
|
||||
{
|
||||
DateTimeKind.Utc => dt,
|
||||
DateTimeKind.Local => dt.ToUniversalTime(),
|
||||
_ => DateTime.SpecifyKind(dt, DateTimeKind.Utc),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a canonical string. For <see cref="DataType.List"/> returns a typed
|
||||
/// <c>List<T></c>; for scalars returns the string unchanged. Throws
|
||||
|
||||
@@ -96,7 +96,20 @@ public class DynamicJsonElement : DynamicObject
|
||||
return true;
|
||||
}
|
||||
|
||||
result = ConvertTo(binder.Type);
|
||||
// A JSON null converts to null for any target that can hold null (reference
|
||||
// types and Nullable<T>), e.g. (string)resp or (int?)resp on a JSON-null body.
|
||||
// A non-nullable value type (e.g. int) genuinely cannot represent it: report
|
||||
// failure so the binder surfaces a binding error (CMN-10).
|
||||
var underlying = Nullable.GetUnderlyingType(binder.Type);
|
||||
if (_element.ValueKind == JsonValueKind.Null)
|
||||
{
|
||||
result = null;
|
||||
return !binder.Type.IsValueType || underlying != null;
|
||||
}
|
||||
|
||||
// Nullable<T> targets convert through their underlying type: (int?)resp on a
|
||||
// JSON number yields the boxed int, which the binder lifts to int?.
|
||||
result = ConvertTo(underlying ?? binder.Type);
|
||||
// A non-object target with a null result means ConvertTo could not handle the
|
||||
// element/type pair — report failure so the binder surfaces a binding error.
|
||||
return result != null;
|
||||
|
||||
Reference in New Issue
Block a user