Skip to content

Commit b434388

Browse files
authored
Support IntPtr and UIntPtr serialization (#1746)
1 parent 3bab125 commit b434388

18 files changed

+157
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Expose EnumerateChainedExceptions ([#1733](https://github.com/getsentry/sentry-dotnet/pull/1733))
88
- Android Scope Sync ([#1737](https://github.com/getsentry/sentry-dotnet/pull/1737))
99
- Enable logging in MAUI ([#1738](https://github.com/getsentry/sentry-dotnet/pull/1738))
10+
- Support IntPtr and UIntPtr serialization ([#1746](https://github.com/getsentry/sentry-dotnet/pull/1746))
1011
- Catch permission exceptions on Android ([#1750](https://github.com/getsentry/sentry-dotnet/pull/1750))
1112

1213
## Sentry.Maui 3.18.0-preview.1

src/Sentry/Internal/Extensions/JsonExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@ internal static class JsonExtensions
1111
{
1212
// The Json options with a preset of rules that will remove dangerous and problematic
1313
// data from the serialized object.
14-
private static JsonSerializerOptions serializerOption = new() { Converters = { new SentryJsonConverter() } };
14+
private static JsonSerializerOptions serializerOption = new()
15+
{
16+
Converters =
17+
{
18+
new SentryJsonConverter(),
19+
new IntPtrJsonConverter(),
20+
new IntPtrNullableJsonConverter(),
21+
new UIntPtrJsonConverter(),
22+
new UIntPtrNullableJsonConverter()
23+
}
24+
};
1525

1626
public static void Deconstruct(this JsonProperty jsonProperty, out string name, out JsonElement value)
1727
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Sentry.Internal
6+
{
7+
internal class IntPtrJsonConverter : JsonConverter<IntPtr>
8+
{
9+
public override IntPtr Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
return new IntPtr(reader.GetInt64());
12+
}
13+
14+
public override void Write(Utf8JsonWriter writer, IntPtr value, JsonSerializerOptions options)
15+
{
16+
writer.WriteNumberValue(value.ToInt64());
17+
}
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Sentry.Internal
6+
{
7+
internal class IntPtrNullableJsonConverter : JsonConverter<IntPtr?>
8+
{
9+
public override IntPtr? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
if (reader.TokenType == JsonTokenType.Null)
12+
{
13+
return null;
14+
}
15+
16+
return new IntPtr(reader.GetInt64());
17+
}
18+
19+
public override void Write(Utf8JsonWriter writer, IntPtr? value, JsonSerializerOptions options)
20+
{
21+
if (value == null)
22+
{
23+
writer.WriteNullValue();
24+
}
25+
else
26+
{
27+
writer.WriteNumberValue(value.Value.ToInt64());
28+
}
29+
}
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Sentry.Internal
6+
{
7+
internal class UIntPtrJsonConverter : JsonConverter<UIntPtr>
8+
{
9+
public override UIntPtr Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
return new UIntPtr(reader.GetUInt64());
12+
}
13+
14+
public override void Write(Utf8JsonWriter writer, UIntPtr value, JsonSerializerOptions options)
15+
{
16+
writer.WriteNumberValue(value.ToUInt64());
17+
}
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Sentry.Internal
6+
{
7+
internal class UIntPtrNullableJsonConverter : JsonConverter<UIntPtr?>
8+
{
9+
public override UIntPtr? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
if (reader.TokenType == JsonTokenType.Null)
12+
{
13+
return null;
14+
}
15+
16+
return new UIntPtr(reader.GetUInt64());
17+
}
18+
19+
public override void Write(Utf8JsonWriter writer, UIntPtr? value, JsonSerializerOptions options)
20+
{
21+
if (value == null)
22+
{
23+
writer.WriteNullValue();
24+
}
25+
else
26+
{
27+
writer.WriteNumberValue(value.Value.ToUInt64());
28+
}
29+
}
30+
}
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Value":3}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Value":3}

0 commit comments

Comments
 (0)