Skip to content

Commit ba0bd72

Browse files
committed
Added more "ParseJson..." and "TryParseJson..." to the "HttpResponseBase"
1 parent b90b4d7 commit ba0bd72

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

src/Skybrud.Essentials.Http/HttpResponseBase.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,36 @@ protected HttpResponseBase(IHttpResponse response) {
6363

6464
#region Member methods
6565

66+
/// <summary>
67+
/// Parses the specified <paramref name="json"/> string into an instance <see cref="JToken"/>.
68+
/// </summary>
69+
/// <param name="json">The JSON string to be parsed.</param>
70+
/// <returns>An instance of <see cref="JObject"/> parsed from the specified <paramref name="json"/> string.</returns>
71+
protected static JToken ParseJsonToken(string json) {
72+
return JsonUtils.ParseJsonToken(json);
73+
}
74+
75+
/// <summary>
76+
/// Parses the specified <paramref name="json"/> string into an instance <typeparamref name="T"/>.
77+
/// </summary>
78+
/// <param name="json">The JSON string to be parsed.</param>
79+
/// <returns>An instance of <typeparamref name="T"/> parsed from the specified <paramref name="json"/> string.</returns>
80+
protected static T ParseJsonToken<T>(string json) {
81+
return JsonUtils.ParseJsonToken<T>(json);
82+
}
83+
84+
/// <summary>
85+
/// Parses the specified <paramref name="json"/> string into an instance of <typeparamref name="T"/>.
86+
/// </summary>
87+
/// <typeparam name="T">The type to be returned.</typeparam>
88+
/// <param name="json">The JSON string to be parsed.</param>
89+
/// <param name="func">A callback function/method used for converting an instance of <see cref="JToken"/> into
90+
/// an instance of <typeparamref name="T"/>.</param>
91+
/// <returns>An instance of <typeparamref name="T"/> parsed from the specified <paramref name="json"/> string.</returns>
92+
protected static T ParseJsonToken<T>(string json, Func<JToken, T> func) {
93+
return JsonUtils.ParseJsonToken(json, func);
94+
}
95+
6696
/// <summary>
6797
/// Parses the specified <paramref name="json"/> string into an instance of <see cref="JObject"/>.
6898
/// </summary>
@@ -102,6 +132,117 @@ protected static T[] ParseJsonArray<T>(string json, Func<JObject, T> func) {
102132
return JsonUtils.ParseJsonArray(json, func);
103133
}
104134

135+
/// <summary>
136+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <see cref="JToken"/>.
137+
/// </summary>
138+
/// <param name="json">The JSON string to parse.</param>
139+
/// <param name="result">When this method returns, holds the parsed <see cref="JToken"/> if successful; otherwise, <c>null</c>.</param>
140+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
141+
protected static bool TryParseJsonToken(string json, out JToken result) {
142+
return JsonUtils.TryParseJsonToken(json, out result);
143+
}
144+
145+
/// <summary>
146+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <typeparamref name="T"/>.
147+
/// </summary>
148+
/// <typeparam name="T">The type of the output object.</typeparam>
149+
/// <param name="json">The JSON string to parse.</param>
150+
/// <param name="result">When this method returns, holds the parsed <see cref="JToken"/> if successful; otherwise, the default value of <typeparamref name="T"/>.</param>
151+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
152+
protected static bool TryParseJsonToken<T>(string json, out T result) {
153+
return JsonUtils.TryParseJsonToken(json, out result);
154+
}
155+
156+
/// <summary>
157+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <typeparamref name="T"/>.
158+
/// </summary>
159+
/// <typeparam name="T">The type of the output object.</typeparam>
160+
/// <param name="json">The JSON string to parse.</param>
161+
/// <param name="callback">A callback function used for converting a <see cref="JToken"/> into an instance of <typeparamref name="T"/>.</param>
162+
/// <param name="result">When this method returns, holds the parsed <see cref="JToken"/> if successful; otherwise, the default value of <typeparamref name="T"/>.</param>
163+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
164+
protected static bool TryParseJsonToken<T>(string json, Func<JToken, T> callback, out T result) {
165+
return JsonUtils.TryParseJsonToken(json, callback, out result);
166+
}
167+
168+
/// <summary>
169+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <see cref="JObject"/>.
170+
/// </summary>
171+
/// <param name="json">The JSON string to parse.</param>
172+
/// <param name="result">When this method returns, holds the parsed <see cref="JObject"/> if successful; otherwise, <c>null</c>.</param>
173+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
174+
protected static bool TryParseJsonObject(string json, out JObject result) {
175+
return JsonUtils.TryParseJsonObject(json, out result);
176+
}
177+
178+
/// <summary>
179+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <typeparamref name="T"/>.
180+
/// </summary>
181+
/// <typeparam name="T">The type of the output object.</typeparam>
182+
/// <param name="json">The JSON string to parse.</param>
183+
/// <param name="result">When this method returns, holds the parsed <see cref="JObject"/> if successful; otherwise, the default value of <typeparamref name="T"/>.</param>
184+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
185+
protected static bool TryParseJsonObject<T>(string json, out T result) {
186+
return JsonUtils.TryParseJsonObject(json, out result);
187+
}
188+
189+
/// <summary>
190+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <typeparamref name="T"/>.
191+
/// </summary>
192+
/// <typeparam name="T">The type of the output object.</typeparam>
193+
/// <param name="json">The JSON string to parse.</param>
194+
/// <param name="callback">A callback function used for converting a <see cref="JObject"/> into an instance of <typeparamref name="T"/>.</param>
195+
/// <param name="result">When this method returns, holds the parsed <see cref="JObject"/> if successful; otherwise, the default value of <typeparamref name="T"/>.</param>
196+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
197+
protected static bool TryParseJsonObject<T>(string json, Func<JObject, T> callback, out T result) {
198+
return JsonUtils.TryParseJsonObject(json, callback, out result);
199+
}
200+
201+
/// <summary>
202+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <see cref="JArray"/>.
203+
/// </summary>
204+
/// <param name="json">The JSON string to parse.</param>
205+
/// <param name="result">When this method returns, holds the parsed <see cref="JArray"/> if successful; otherwise, <c>null</c>.</param>
206+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
207+
protected static bool TryParseJsonArray(string json, out JArray result) {
208+
return JsonUtils.TryParseJsonArray(json, out result);
209+
}
210+
211+
/// <summary>
212+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <typeparamref name="T"/>.
213+
/// </summary>
214+
/// <typeparam name="T">The type of the output Array.</typeparam>
215+
/// <param name="json">The JSON string to parse.</param>
216+
/// <param name="result">When this method returns, holds the parsed array of <typeparamref name="T"/> if successful; otherwise, <c>null</c>.</param>
217+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
218+
protected static bool TryParseJsonArray<T>(string json, out T[] result) {
219+
return JsonUtils.TryParseJsonArray(json, out result);
220+
}
221+
222+
/// <summary>
223+
/// Attempts to parse the specified <paramref name="json"/> string into an instance of <typeparamref name="T"/>.
224+
/// </summary>
225+
/// <typeparam name="T">The type of the output array.</typeparam>
226+
/// <param name="json">The JSON string to parse.</param>
227+
/// <param name="callback">A callback function used for converting a <see cref="JArray"/> into an instance of <typeparamref name="T"/>.</param>
228+
/// <param name="result">When this method returns, holds the parsed array of <typeparamref name="T"/> if successful; otherwise, <c>null</c>.</param>
229+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
230+
protected static bool TryParseJsonArray<T>(string json, Func<JArray, T[]> callback, out T[] result) {
231+
return JsonUtils.TryParseJsonArray(json, callback, out result);
232+
}
233+
234+
/// <summary>
235+
/// Attempts to parse the specified <paramref name="json"/> string into an array of <typeparamref name="T"/>.
236+
/// </summary>
237+
/// <typeparam name="T">The type of the output array.</typeparam>
238+
/// <param name="json">The JSON string to parse.</param>
239+
/// <param name="callback">A callback function used for converting the individual <see cref="JObject"/> of the parsed array into instances of <typeparamref name="T"/>.</param>
240+
/// <param name="result">When this method returns, holds the parsed array of <typeparamref name="T"/> if successful; otherwise, <c>null</c>.</param>
241+
/// <returns><c>true</c> if the parsing was successful; otherwise, <c>false</c>.</returns>
242+
protected static bool TryParseJsonArray<T>(string json, Func<JObject, T> callback, out T[] result) {
243+
return JsonUtils.TryParseJsonArray(json, callback, out result);
244+
}
245+
105246
/// <summary>
106247
/// Parses the specified <paramref name="xml"/> into an instance of <see cref="XElement"/>.
107248
/// </summary>

0 commit comments

Comments
 (0)