Skip to content

Commit f604086

Browse files
authored
build(deps): Migration from Newtonsoft.Json to System.Text.Json package (#836)
* build(deps): Migration from `Newtonsoft.Json` to `System.Text.Json` package * fix: deserialization issues with System.Text.Json and improve environment variable handling * change string comparison to use Equals * add parentheses for clarity init _env with default values add Guard clause to exit early if already initialized
1 parent e89966c commit f604086

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

test/integration/Appium.Net.Integration.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
1818
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
19-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
19+
<PackageReference Include="System.Text.Json" Version="8.0.4" />
2020
</ItemGroup>
2121
<ItemGroup>
2222
<ProjectReference Include="..\..\src\Appium.Net\Appium.Net.csproj" />

test/integration/helpers/Env.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using Newtonsoft.Json;
4+
using System.Text.Json;
55

66
namespace Appium.Net.Integration.Tests.helpers
77
{
@@ -10,32 +10,45 @@ public class Env
1010
public static TimeSpan InitTimeoutSec = TimeSpan.FromSeconds(180);
1111
public static TimeSpan ImplicitTimeoutSec = TimeSpan.FromSeconds(10);
1212

13-
private static Dictionary<string, string> _env;
13+
private static Dictionary<string, JsonElement> _env;
1414
private static bool _initialized;
1515

1616
private static void Init()
1717
{
18+
_env = new Dictionary<string, JsonElement>
19+
{
20+
{ "DEV", JsonDocument.Parse("true").RootElement },
21+
{ "isRemoteAppiumServer", JsonDocument.Parse("false").RootElement },
22+
{ "remoteAppiumServerUri", JsonDocument.Parse("\"http://localhost:4723\"").RootElement }
23+
};
24+
25+
if (_initialized) return;
26+
1827
try
1928
{
20-
if (!_initialized)
29+
_initialized = true;
30+
var path = AppDomain.CurrentDomain.BaseDirectory;
31+
var sr = new StreamReader(path + "env.json");
32+
var jsonString = sr.ReadToEnd();
33+
_env = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(jsonString, new JsonSerializerOptions
2134
{
22-
_initialized = true;
23-
var path = AppDomain.CurrentDomain.BaseDirectory;
24-
var sr = new StreamReader(path + "env.json");
25-
var jsonString = sr.ReadToEnd();
26-
_env = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
27-
}
35+
PropertyNameCaseInsensitive = true
36+
});
2837
}
29-
catch
38+
catch (JsonException jsonEx)
3039
{
31-
_env = new Dictionary<string, string>();
40+
Console.WriteLine($"Error parsing JSON: {jsonEx.Message}");
41+
}
42+
catch (Exception ex)
43+
{
44+
Console.WriteLine($"Error initializing environment: {ex.Message}");
3245
}
3346
}
3447

35-
private static bool IsTrue(string val)
48+
private static bool IsTrue(object val)
3649
{
37-
val = val?.ToLower().Trim();
38-
return (val == "true") || (val == "1");
50+
val = val?.ToString().ToLower().Trim();
51+
return val.Equals("true") || val.Equals("1");
3952
}
4053

4154
public static bool ServerIsRemote()
@@ -47,16 +60,25 @@ public static bool ServerIsRemote()
4760
public static bool ServerIsLocal()
4861
{
4962
Init();
50-
return _env.ContainsKey("DEV") && IsTrue(_env["DEV"]) || IsTrue(Environment.GetEnvironmentVariable("DEV"));
63+
return (_env.ContainsKey("DEV") && IsTrue(_env["DEV"])) || IsTrue(Environment.GetEnvironmentVariable("DEV"));
5164
}
5265

5366
public static string GetEnvVar(string name)
5467
{
55-
if (_env.ContainsKey(name) && (_env[name] != null))
68+
if (_env.ContainsKey(name))
5669
{
57-
return _env[name];
70+
JsonElement element = _env[name];
71+
72+
return element.ValueKind switch
73+
{
74+
JsonValueKind.String => element.GetString(),
75+
JsonValueKind.Number => element.GetRawText(),
76+
JsonValueKind.True or JsonValueKind.False => element.GetRawText(),
77+
JsonValueKind.Null => null,
78+
_ => element.GetRawText()
79+
};
5880
}
59-
return Environment.GetEnvironmentVariable(name);
81+
return Environment.GetEnvironmentVariable(name);
6082
}
6183
}
6284
}

0 commit comments

Comments
 (0)