|
3 | 3 | using System.IO; |
4 | 4 | using System.Text; |
5 | 5 | using Aspose.BarCode.Cloud.Sdk.Api; |
6 | | -using Newtonsoft.Json; |
7 | | -using Newtonsoft.Json.Linq; |
| 6 | +using System.Text.Json; |
8 | 7 | using NUnit.Framework; |
9 | 8 |
|
10 | 9 | namespace Aspose.BarCode.Cloud.Sdk.Tests |
@@ -36,38 +35,44 @@ private static Configuration LoadTestConfiguration() |
36 | 35 | "Configuration.json")); |
37 | 36 | if (File.Exists(configFilename)) |
38 | 37 | { |
39 | | - using StreamReader file = File.OpenText(configFilename); |
40 | | - using JsonTextReader reader = new JsonTextReader(file); |
41 | | - JsonSerializer serializer = new JsonSerializer(); |
42 | | - return serializer.Deserialize<Configuration>(reader); |
| 38 | + using FileStream file = File.OpenRead(configFilename); |
| 39 | + return JsonSerializer.Deserialize<Configuration>(file) |
| 40 | + ?? throw new Exception("Configuration file is empty or invalid"); |
43 | 41 | } |
44 | | - |
45 | 42 | Configuration config = LoadFromEnv(); |
46 | | - |
47 | 43 | return config; |
48 | 44 | } |
49 | 45 |
|
50 | 46 | private static Configuration LoadFromEnv() |
51 | 47 | { |
52 | | - string jsonStr = JsonConvert.SerializeObject(new Configuration()); |
53 | | - JObject obj = JObject.Parse(jsonStr); |
54 | | - foreach (KeyValuePair<string, JToken> i in obj) |
| 48 | + // Serialize default config to JSON then to a dictionary |
| 49 | + string jsonStr = JsonSerializer.Serialize(new Configuration()); |
| 50 | + var dict = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonStr); |
| 51 | + |
| 52 | + if (dict == null) |
| 53 | + throw new Exception("Default configuration invalid"); |
| 54 | + |
| 55 | + foreach (var key in new List<string>(dict.Keys)) |
55 | 56 | { |
56 | | - if (!(i.Value.Type == JTokenType.String || i.Value.Type == JTokenType.Null)) |
57 | | - { |
| 57 | + var value = dict[key]; |
| 58 | + |
| 59 | + // Accept strings and nulls (JSON null becomes null here) |
| 60 | + if (value != null && !(value is string)) |
58 | 61 | continue; |
59 | | - } |
60 | 62 |
|
61 | | - string name = i.Key; |
62 | | - string envName = $"{ENV_NAME_PREFIX}{CamelCaseToUpper(name)}"; |
| 63 | + string envName = $"{ENV_NAME_PREFIX}{CamelCaseToUpper(key)}"; |
63 | 64 | string envValue = Environment.GetEnvironmentVariable(envName); |
64 | 65 | if (!string.IsNullOrEmpty(envValue)) |
65 | 66 | { |
66 | | - obj[name] = envValue; |
| 67 | + dict[key] = envValue; |
67 | 68 | } |
68 | 69 | } |
69 | | - |
70 | | - return JsonConvert.DeserializeObject<Configuration>(obj.ToString()); |
| 70 | + // Serialize back and then deserialize to Configuration |
| 71 | + string updatedJson = JsonSerializer.Serialize(dict); |
| 72 | + var config = JsonSerializer.Deserialize<Configuration>(updatedJson); |
| 73 | + if (config == null) |
| 74 | + throw new Exception("Failed to create Configuration from environment variables"); |
| 75 | + return config; |
71 | 76 | } |
72 | 77 |
|
73 | 78 | private static string CamelCaseToUpper(string name) |
|
0 commit comments