Skip to content

Commit 9911cf6

Browse files
refactor(audience-sdk): name JSON timestamp and round-trip number formats
Replaces the bare format-spec strings "o" and "R" passed to DateTime and float / double ToString calls with named constants on Constants so the wire-shape requirement reads as a contract rather than a mystery character. - Constants.cs: adds IsoTimestampFormat ("o") and RoundTripNumberFormat ("R") with comments noting the backend schema requirement and the round-trip preservation guarantee. - MessageBuilder.cs: BuildBase eventTimestamp uses Constants.IsoTimestampFormat. - Json.cs: float and double serialisation use Constants.RoundTripNumberFormat. - AudienceSample.UI.cs: log-row export timestamp uses Constants.IsoTimestampFormat.
1 parent ce2ddb0 commit 9911cf6

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

examples/audience/Assets/SampleApp/Scripts/AudienceSample.UI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ private VisualElement BuildLogRow(LogEntry entry)
780780
private static string FormatLogEntry(LogEntry entry, bool singleLine)
781781
{
782782
var sb = new StringBuilder()
783-
.Append(entry.Timestamp.ToString("o", CultureInfo.InvariantCulture))
783+
.Append(entry.Timestamp.ToString(Constants.IsoTimestampFormat, CultureInfo.InvariantCulture))
784784
.Append(" [").Append(entry.Source == LogSource.Sdk ? "SDK" : "APP").Append("] ")
785785
.Append(entry.Label);
786786
if (!string.IsNullOrEmpty(entry.Body))

src/Packages/Audience/Runtime/Core/Constants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ internal static class Constants
1717
internal const int MaxBatchSize = 100;
1818
internal const int StaleEventDays = 30;
1919
internal const int MaxFieldLength = 256; // Backend schema limit.
20+
21+
// Timestamp format the backend wants on every event.
22+
internal const string IsoTimestampFormat = "o";
23+
24+
// Format that lets numbers survive a JSON round-trip unchanged.
25+
internal const string RoundTripNumberFormat = "R";
2026
internal const int ControlPlaneRequestTimeoutSeconds = 30;
2127

2228
internal const string LibraryName = "com.immutable.audience";

src/Packages/Audience/Runtime/Events/MessageBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static Dictionary<string, object> BuildBase(string type, string packageV
7979
{
8080
[MessageFields.Type] = type,
8181
["messageId"] = Guid.NewGuid().ToString(),
82-
["eventTimestamp"] = DateTime.UtcNow.ToString("o"),
82+
["eventTimestamp"] = DateTime.UtcNow.ToString(Constants.IsoTimestampFormat),
8383
["context"] = new Dictionary<string, object>
8484
{
8585
["library"] = Constants.LibraryName,

src/Packages/Audience/Runtime/Utility/Json.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ private static void WriteValue(StringBuilder sb, object? value, int indent, int
6060
if (float.IsNaN(f) || float.IsInfinity(f))
6161
sb.Append("null");
6262
else
63-
sb.Append(f.ToString("R", CultureInfo.InvariantCulture));
63+
sb.Append(f.ToString(Constants.RoundTripNumberFormat, CultureInfo.InvariantCulture));
6464
}
6565
else if (value is double d)
6666
{
6767
if (double.IsNaN(d) || double.IsInfinity(d))
6868
sb.Append("null");
6969
else
70-
sb.Append(d.ToString("R", CultureInfo.InvariantCulture));
70+
sb.Append(d.ToString(Constants.RoundTripNumberFormat, CultureInfo.InvariantCulture));
7171
}
7272
else if (value is decimal dec)
7373
{

0 commit comments

Comments
 (0)