Skip to content

Commit 83290b9

Browse files
committed
Revert "Removed obsolete classes"
This reverts commit ae6a25e.
1 parent ed6234c commit 83290b9

3 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Net;
3+
using Newtonsoft.Json;
4+
5+
namespace Skybrud.Essentials.AspNetCore.Models.Json {
6+
7+
/// <summary>
8+
/// Class representing the body of a JSON response.
9+
/// </summary>
10+
[Obsolete("Use the 'NewtonsoftJsonBody' class instead.")]
11+
public class JsonBody {
12+
13+
/// <summary>
14+
/// Gets or sets the meta data for the response.
15+
/// </summary>
16+
[JsonProperty(PropertyName = "meta")]
17+
public JsonMetaData Meta { get; set; } = new();
18+
19+
/// <summary>
20+
/// Gets or sets the data object.
21+
/// </summary>
22+
[JsonProperty(PropertyName = "data", NullValueHandling = NullValueHandling.Ignore)]
23+
public object Data { get; set; }
24+
25+
#region Constructors
26+
27+
/// <summary>
28+
/// Initializes a new instance with default options.
29+
/// </summary>
30+
public JsonBody() { }
31+
32+
/// <summary>
33+
/// Initializes a new instance based on the specified <paramref name="status"/> and <paramref name="error"/> message.
34+
/// </summary>
35+
/// <param name="status">The HTTP status.</param>
36+
/// <param name="error">The error message.</param>
37+
public JsonBody(HttpStatusCode status, string error) {
38+
Meta.Code = status;
39+
Meta.Error = error;
40+
}
41+
42+
/// <summary>
43+
/// Initializes a new instance based on the specified <paramref name="status"/>, <paramref name="error"/> message and <paramref name="data"/>.
44+
/// </summary>
45+
/// <param name="status">The HTTP status.</param>
46+
/// <param name="error">The error message.</param>
47+
/// <param name="data">The data.</param>
48+
public JsonBody(HttpStatusCode status, string error, object data) {
49+
Meta.Code = status;
50+
Meta.Error = error;
51+
Data = data;
52+
}
53+
54+
#endregion
55+
56+
}
57+
58+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Net;
3+
using Newtonsoft.Json;
4+
5+
namespace Skybrud.Essentials.AspNetCore.Models.Json {
6+
7+
/// <summary>
8+
/// Class representing the meta data of a JSON response.
9+
/// </summary>
10+
[Obsolete("Use the 'NewtonsoftJsonMetaData' class instead.")]
11+
public class JsonMetaData {
12+
13+
/// <summary>
14+
/// Gets or sets the status code.
15+
/// </summary>
16+
[JsonProperty(PropertyName = "code")]
17+
public HttpStatusCode Code { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the error message. If the error message is <c>NULL</c>, the property will not be a part of the JSON response.
21+
/// </summary>
22+
[JsonProperty(PropertyName = "error", NullValueHandling = NullValueHandling.Ignore)]
23+
public string Error { get; set; }
24+
25+
}
26+
27+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Net;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Newtonsoft.Json;
5+
6+
namespace Skybrud.Essentials.AspNetCore.Models.Json {
7+
8+
/// <summary>
9+
/// Class representing a JOSN based result serialized using <strong>Newtonsoft.Json</strong>.
10+
/// </summary>
11+
[Obsolete("Use the 'NewtonsoftJsonNetResult' class instead.")]
12+
public class JsonNetResult : ContentResult {
13+
14+
#region Constructors
15+
16+
/// <summary>
17+
/// Initializes a new <strong>200 OK</strong> result wrapping the specified <paramref name="value"/>.
18+
/// </summary>
19+
/// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
20+
public JsonNetResult(object value) : this(HttpStatusCode.OK, value) { }
21+
22+
/// <summary>
23+
/// Initializes a new result with <paramref name="status"/> wrapping the specified <paramref name="value"/>.
24+
/// </summary>
25+
/// <param name="status">The HTTP status code.</param>
26+
/// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
27+
public JsonNetResult(HttpStatusCode status, object value) {
28+
29+
// Serialize the data to a JSON string using JSON.net
30+
string json = JsonConvert.SerializeObject(value, Formatting.None);
31+
32+
StatusCode = (int) status;
33+
ContentType = "application/json";
34+
Content = json;
35+
36+
}
37+
38+
#endregion
39+
40+
#region Static methods
41+
42+
/// <summary>
43+
/// Returns a new <strong>200 OK</strong> result with the specified <paramref name="value"/>.
44+
/// </summary>
45+
/// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
46+
/// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
47+
public static JsonNetResult Ok(object value) {
48+
return new JsonNetResult(HttpStatusCode.OK, value);
49+
}
50+
51+
/// <summary>
52+
/// Returns a new <strong>201 Created</strong> result with the specified <paramref name="value"/>.
53+
/// </summary>
54+
/// <param name="value">The value/body of the result. The value will be serialized to JSON before being returned to the client.</param>
55+
/// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
56+
public static JsonNetResult Created(object value) {
57+
return new JsonNetResult(HttpStatusCode.Created, value);
58+
}
59+
60+
/// <summary>
61+
/// Returns a new <strong>400 Bad Request</strong> result with the specified <paramref name="error"/> message.
62+
/// </summary>
63+
/// <param name="error">A message describing the error.</param>
64+
/// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
65+
public static JsonNetResult BadRequest(string error) {
66+
JsonBody body = new(HttpStatusCode.BadRequest, error, null);
67+
return new JsonNetResult(HttpStatusCode.BadRequest, body);
68+
}
69+
70+
/// <summary>
71+
/// Returns a new <strong>401 Unauthorized</strong> result with the specified <paramref name="error"/> message.
72+
/// </summary>
73+
/// <param name="error">A message describing the error.</param>
74+
/// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
75+
public static JsonNetResult Unauthorized(string error) {
76+
JsonBody body = new(HttpStatusCode.Unauthorized, error, null);
77+
return new JsonNetResult(HttpStatusCode.Unauthorized, body);
78+
}
79+
80+
/// <summary>
81+
/// Returns a new <strong>403 Forbidden</strong> result with the specified <paramref name="error"/> message.
82+
/// </summary>
83+
/// <param name="error">A message describing the error.</param>
84+
/// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
85+
public static JsonNetResult Forbidden(string error) {
86+
JsonBody body = new(HttpStatusCode.Forbidden, error, null);
87+
return new JsonNetResult(HttpStatusCode.Forbidden, body);
88+
}
89+
90+
/// <summary>
91+
/// Returns a new <strong>404 Not Found</strong> result with the specified <paramref name="error"/> message.
92+
/// </summary>
93+
/// <param name="error">A message describing the error.</param>
94+
/// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
95+
public static JsonNetResult NotFound(string error) {
96+
JsonBody body = new(HttpStatusCode.NotFound, error, null);
97+
return new JsonNetResult(HttpStatusCode.NotFound, body);
98+
}
99+
100+
/// <summary>
101+
/// Returns a new <strong>500 Internal Server Error</strong> result with the specified <paramref name="error"/> message.
102+
/// </summary>
103+
/// <param name="error">A message describing the error.</param>
104+
/// <returns>An instance of <see cref="JsonNetResult"/>.</returns>
105+
public static JsonNetResult InternalError(string error) {
106+
JsonBody body = new(HttpStatusCode.InternalServerError, error, null);
107+
return new JsonNetResult(HttpStatusCode.InternalServerError, body);
108+
}
109+
110+
#endregion
111+
112+
}
113+
114+
}

0 commit comments

Comments
 (0)