Skip to content

Commit 334c7ce

Browse files
committed
feat: Add ability to pretty print json
1 parent a81c074 commit 334c7ce

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

Runtime/Client/LootLockerJson.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ public static bool TryDeserializeObject<T>(string json, JsonSerializerSettings o
8585
return false;
8686
}
8787
}
88+
89+
public static string PrettifyJsonString(string json)
90+
{
91+
try
92+
{
93+
var parsedJson = DeserializeObject<object>(json);
94+
var tempSettings = LootLockerJsonSettings.Default;
95+
tempSettings.Formatting = Formatting.Indented;
96+
return SerializeObject(parsedJson, tempSettings);
97+
}
98+
catch (Exception)
99+
{
100+
return json;
101+
}
102+
}
88103
#else //LOOTLOCKER_USE_NEWTONSOFTJSON
89104
public static string SerializeObject(object obj)
90105
{
@@ -142,6 +157,19 @@ public static bool TryDeserializeObject<T>(string json, JsonOptions options, out
142157
return false;
143158
}
144159
}
160+
161+
public static string PrettifyJsonString(string json)
162+
{
163+
try
164+
{
165+
var parsedJson = DeserializeObject<object>(json);
166+
return Json.SerializeFormatted(parsedJson, LootLockerJsonSettings.Default);
167+
}
168+
catch (Exception)
169+
{
170+
return json;
171+
}
172+
}
145173
#endif //LOOTLOCKER_USE_NEWTONSOFTJSON
146174
}
147175
}

Runtime/Client/LootLockerResponse.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class LootLockerResponseFactory
102102
return new T()
103103
{
104104
success = true,
105-
text = responseBody,
105+
text = LootLockerConfig.current.prettifyJson ? LootLockerJson.PrettifyJsonString(responseBody) : responseBody,
106106
statusCode = statusCode,
107107
errorData = null,
108108
requestContext = new LootLockerRequestContext(forPlayerWithUlid, requestTime)
@@ -117,7 +117,7 @@ public class LootLockerResponseFactory
117117
return new T()
118118
{
119119
success = false,
120-
text = responseBody,
120+
text = LootLockerConfig.current.prettifyJson ? LootLockerJson.PrettifyJsonString(responseBody) : responseBody,
121121
statusCode = statusCode,
122122
errorData = null,
123123
requestContext = new LootLockerRequestContext(forPlayerWithUlid, requestTime)
@@ -132,7 +132,7 @@ public class LootLockerResponseFactory
132132
return new T()
133133
{
134134
success = failureResponse.success,
135-
text = failureResponse.text,
135+
text = LootLockerConfig.current.prettifyJson ? LootLockerJson.PrettifyJsonString(failureResponse.text) : failureResponse.text,
136136
statusCode = failureResponse.statusCode,
137137
errorData = failureResponse.errorData,
138138
requestContext = failureResponse.requestContext

Runtime/Game/Resources/LootLockerConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ public static bool IsTargetingProductionEnvironment()
274274
[HideInInspector] public string baseUrl = UrlProtocol + GetUrlCore();
275275
[HideInInspector] public float clientSideRequestTimeOut = 180f;
276276
public LootLockerLogger.LogLevel logLevel = LootLockerLogger.LogLevel.Info;
277+
public bool prettifyJson = true;
277278
public bool logErrorsAsWarnings = false;
278279
public bool logInBuilds = false;
279280
public bool allowTokenRefresh = true;

0 commit comments

Comments
 (0)