Skip to content

Commit 3bc9c47

Browse files
Erik Bylundkirre-bylund
authored andcommitted
feat: Add TryDeserialize methods to LootLockerJson
1 parent 317eae7 commit 3bc9c47

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

Runtime/Client/LootLockerServerRequest.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,29 @@ public static T DeserializeObject<T>(string json)
4747
return DeserializeObject<T>(json, LootLockerJsonSettings.Default);
4848
}
4949

50-
5150
public static T DeserializeObject<T>(string json, JsonSerializerSettings settings)
5251
{
5352
return JsonConvert.DeserializeObject<T>(json, settings ?? LootLockerJsonSettings.Default);
5453
}
54+
55+
public static bool TryDeserializeObject<T>(string json, out T output)
56+
{
57+
return TryDeserializeObject<T>(json, LootLockerJsonSettings.Default, out output);
58+
}
59+
60+
public static bool TryDeserializeObject<T>(string json, JsonSerializerSettings options, out T output)
61+
{
62+
try
63+
{
64+
output = JsonConvert.DeserializeObject<T>(json, options ?? LootLockerJsonSettings.Default);
65+
return true;
66+
}
67+
catch (Exception)
68+
{
69+
output = default(T);
70+
return false;
71+
}
72+
}
5573
#else //LOOTLOCKER_USE_NEWTONSOFTJSON
5674
public static string SerializeObject(object obj)
5775
{
@@ -72,6 +90,24 @@ public static T DeserializeObject<T>(string json, JsonOptions options)
7290
{
7391
return Json.Deserialize<T>(json, options ?? LootLockerJsonSettings.Default);
7492
}
93+
94+
public static bool TryDeserializeObject<T>(string json, out T output)
95+
{
96+
return TryDeserializeObject<T>(json, LootLockerJsonSettings.Default, out output);
97+
}
98+
99+
public static bool TryDeserializeObject<T>(string json, JsonOptions options, out T output)
100+
{
101+
try
102+
{
103+
output = Json.Deserialize<T>(json, options ?? LootLockerJsonSettings.Default);
104+
return true;
105+
} catch (Exception)
106+
{
107+
output = default(T);
108+
return false;
109+
}
110+
}
75111
#endif //LOOTLOCKER_USE_NEWTONSOFTJSON
76112
}
77113

0 commit comments

Comments
 (0)