Skip to content

Commit

Permalink
build(deps): Migration from Newtonsoft.Json to System.Text.Json p…
Browse files Browse the repository at this point in the history
…ackage (#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
  • Loading branch information
Dor-bl authored Sep 28, 2024
1 parent e89966c commit f604086
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion test/integration/Appium.Net.Integration.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Appium.Net\Appium.Net.csproj" />
Expand Down
58 changes: 40 additions & 18 deletions test/integration/helpers/Env.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Text.Json;

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

private static Dictionary<string, string> _env;
private static Dictionary<string, JsonElement> _env;
private static bool _initialized;

private static void Init()
{
_env = new Dictionary<string, JsonElement>
{
{ "DEV", JsonDocument.Parse("true").RootElement },
{ "isRemoteAppiumServer", JsonDocument.Parse("false").RootElement },
{ "remoteAppiumServerUri", JsonDocument.Parse("\"http://localhost:4723\"").RootElement }
};

if (_initialized) return;

try
{
if (!_initialized)
_initialized = true;
var path = AppDomain.CurrentDomain.BaseDirectory;
var sr = new StreamReader(path + "env.json");
var jsonString = sr.ReadToEnd();
_env = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(jsonString, new JsonSerializerOptions
{
_initialized = true;
var path = AppDomain.CurrentDomain.BaseDirectory;
var sr = new StreamReader(path + "env.json");
var jsonString = sr.ReadToEnd();
_env = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
}
PropertyNameCaseInsensitive = true
});
}
catch
catch (JsonException jsonEx)
{
_env = new Dictionary<string, string>();
Console.WriteLine($"Error parsing JSON: {jsonEx.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error initializing environment: {ex.Message}");
}
}

private static bool IsTrue(string val)
private static bool IsTrue(object val)
{
val = val?.ToLower().Trim();
return (val == "true") || (val == "1");
val = val?.ToString().ToLower().Trim();
return val.Equals("true") || val.Equals("1");
}

public static bool ServerIsRemote()
Expand All @@ -47,16 +60,25 @@ public static bool ServerIsRemote()
public static bool ServerIsLocal()
{
Init();
return _env.ContainsKey("DEV") && IsTrue(_env["DEV"]) || IsTrue(Environment.GetEnvironmentVariable("DEV"));
return (_env.ContainsKey("DEV") && IsTrue(_env["DEV"])) || IsTrue(Environment.GetEnvironmentVariable("DEV"));
}

public static string GetEnvVar(string name)
{
if (_env.ContainsKey(name) && (_env[name] != null))
if (_env.ContainsKey(name))
{
return _env[name];
JsonElement element = _env[name];

return element.ValueKind switch
{
JsonValueKind.String => element.GetString(),
JsonValueKind.Number => element.GetRawText(),
JsonValueKind.True or JsonValueKind.False => element.GetRawText(),
JsonValueKind.Null => null,
_ => element.GetRawText()
};
}
return Environment.GetEnvironmentVariable(name);
return Environment.GetEnvironmentVariable(name);
}
}
}

0 comments on commit f604086

Please sign in to comment.