Skip to content

Commit 86bc175

Browse files
committed
Do not serialize each string as a datetime
1 parent f431639 commit 86bc175

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

samples/AzureMapsControl.Sample/Pages/Layers/SymbolLayerWithFeatures.razor

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
.None()
88
.Enable(MapEventType.Ready, MapEventType.Click)"
99
OnReady="OnMapReady" />
10-
i
1110
@code {
1211
public async Task OnMapReady(MapEventArgs eventArgs)
1312
{
@@ -18,7 +17,7 @@ i
1817
var feature = new AzureMapsControl.Components.Atlas.Feature<AzureMapsControl.Components.Atlas.Point>(new AzureMapsControl.Components.Atlas.Point(
1918
new AzureMapsControl.Components.Atlas.Position(-122.13284, 47.63699)
2019
), new Dictionary<string, object> {
21-
{ "title", "Cafeteria" },
20+
{ "title", new DateTime(2021, 6, 3) },
2221
{ "subtitle", "Building 40"}
2322
});
2423

src/AzureMapsControl.Components/Atlas/Feature.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
[ExcludeFromCodeCoverage]
1010
public abstract class Feature
1111
{
12-
[JsonConverter(typeof(StringConverter))]
12+
[JsonConverter(typeof(FeatureIdConverter))]
1313
public string Id { get; set; }
1414

15+
[JsonConverter(typeof(FeaturePropertiesConverter))]
1516
public IDictionary<string, object> Properties { get; set; }
1617
public BoundingBox BBox { get; set; }
1718

@@ -30,8 +31,7 @@ public sealed class Feature<TGeometry> : Feature
3031

3132
public TGeometry Geometry
3233
{
33-
get
34-
{
34+
get {
3535
if (_geometry != null && _geometry.Id != Id)
3636
{
3737
_geometry.Id = Id;
@@ -55,7 +55,7 @@ public Feature(TGeometry geometry, IDictionary<string, object> properties) : thi
5555
public Feature(string id, TGeometry geometry, IDictionary<string, object> properties) : base(id, properties) => Geometry = geometry;
5656
}
5757

58-
internal sealed class StringConverter : JsonConverter<string>
58+
internal sealed class FeatureIdConverter : JsonConverter<string>
5959
{
6060
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
6161
{
@@ -70,4 +70,27 @@ public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
7070

7171
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => writer.WriteStringValue(value);
7272
}
73+
74+
internal sealed class FeaturePropertiesConverter : JsonConverter<IDictionary<string, object>>
75+
{
76+
public override IDictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options);
77+
public override void Write(Utf8JsonWriter writer, IDictionary<string, object> value, JsonSerializerOptions options)
78+
{
79+
writer.WriteStartObject();
80+
foreach (var kvp in value)
81+
{
82+
writer.WritePropertyName(kvp.Key);
83+
if (kvp.Value.GetType() == typeof(DateTime) ||
84+
(kvp.Value.GetType() == typeof(string) && DateTime.TryParse(kvp.Value.ToString(), out _)))
85+
{
86+
writer.WriteStringValue($"azureMapsControl.datetime:{kvp.Value}");
87+
}
88+
else
89+
{
90+
writer.WriteStringValue(JsonSerializer.Serialize(kvp.Value));
91+
}
92+
}
93+
writer.WriteEndObject();
94+
}
95+
}
7396
}

src/AzureMapsControl.Components/typescript/core/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ export class Core {
569569
public static formatProperties(properties: { [key: string]: any }): { [key: string]: any } {
570570
if (properties) {
571571
for (const key in properties) {
572-
if (typeof properties[key] === 'string') {
573-
const date = Date.parse(properties[key]);
572+
if (typeof properties[key] === 'string' && properties[key].startsWith('azureMapsControl.datetime:')) {
573+
const date = Date.parse(properties[key].replace('azureMapsControl.datetime:', ''));
574574
if (!isNaN(date)) {
575575
properties[key] = new Date(date);
576576
}

0 commit comments

Comments
 (0)