Skip to content

Commit

Permalink
Fix IL2CPP JSON parsing issue (#1583)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Apr 21, 2022
1 parent 4afb2f2 commit e615aa9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Workaround `System.Text.Json` issue with Unity IL2CPP. ([#1583](https://github.com/getsentry/sentry-dotnet/pull/1583))

## 3.16.0

### Features
Expand Down
26 changes: 25 additions & 1 deletion src/Sentry/Internal/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,37 @@ public static void Deconstruct(this JsonProperty jsonProperty, out string name,
{
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Number => json.GetDouble(),
JsonValueKind.Number => json.GetNumber(), // see implementation for why we don't just call GetDouble
JsonValueKind.String => json.GetString(),
JsonValueKind.Array => json.EnumerateArray().Select(GetDynamicOrNull).ToArray(),
JsonValueKind.Object => json.GetDictionaryOrNull(),
_ => null
};

private static object? GetNumber(this JsonElement json)
{
var result = json.GetDouble();
if (result != 0)
{
// We got a value, as expected.
return result;
}

// We might have 0 when there's actually a value there.
// This happens on Unity IL2CPP targets. Let's workaround that.
// See https://github.com/getsentry/sentry-unity/issues/690

// If the number is an integer, we can avoid extra string parsing
if (json.TryGetInt64(out var longResult))
{
return longResult;
}

// Otherwise, let's get the value as a string and parse it ourselves.
// Note that we already know this will succeed due to JsonValueKind.Number
return double.Parse(json.ToString()!);
}

public static string GetStringOrThrow(this JsonElement json) =>
json.GetString() ?? throw new InvalidOperationException("JSON string is null.");

Expand Down

0 comments on commit e615aa9

Please sign in to comment.