Skip to content

Commit 1135b53

Browse files
committed
Re-adding deleted classes to avoid breaking changes
Since we have solutions and packages using a mix of different versions of this package, it's not possible to upgrade to alpha007 without some other part breaking. Therefore the deleted classes have been re-added to the package, but marked as obsolete. The classes ought still to be removed before pushing a stable release.
1 parent 83290b9 commit 1135b53

8 files changed

Lines changed: 277 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Skybrud.Essentials.AspNetCore.Filters.NewtonsoftJson;
4+
5+
#pragma warning disable CS1591
6+
7+
namespace Skybrud.Essentials.AspNetCore.Attributes.NewtonsoftJson {
8+
9+
/// <summary>
10+
/// When added to an API controller, the output will be serialized to JSON using <strong>Newtonsoft.Json</strong>.
11+
/// </summary>
12+
[AttributeUsage(AttributeTargets.Class)]
13+
[Obsolete("Use the class in the 'Skybrud.Essentials.AspNetCore.Json.Newtonsoft.Attributes' namespace instead.")]
14+
public class NewtonsoftJsonOnlyConfigurationAttribute : TypeFilterAttribute {
15+
16+
public NewtonsoftJsonOnlyConfigurationAttribute() : base(typeof(NewtonsoftJsonOnlyConfigurationFilter)) {
17+
Order = 1;
18+
}
19+
20+
}
21+
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Buffers;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.AspNetCore.Mvc.Filters;
5+
using Microsoft.AspNetCore.Mvc.Formatters;
6+
using Microsoft.Extensions.Options;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Converters;
9+
using Newtonsoft.Json.Serialization;
10+
11+
#pragma warning disable CS1591
12+
13+
namespace Skybrud.Essentials.AspNetCore.Filters.NewtonsoftJson {
14+
15+
[Obsolete("Use the class in the 'Skybrud.Essentials.AspNetCore.Json.Newtonsoft.Filters' namespace instead.")]
16+
public class NewtonsoftJsonOnlyConfigurationFilter : IResultFilter {
17+
18+
private readonly ArrayPool<char> _arrayPool;
19+
private readonly MvcOptions _options;
20+
21+
public NewtonsoftJsonOnlyConfigurationFilter(ArrayPool<char> arrayPool, IOptionsSnapshot<MvcOptions> options) {
22+
_arrayPool = arrayPool;
23+
_options = options.Value;
24+
}
25+
26+
public void OnResultExecuted(ResultExecutedContext context) { }
27+
28+
public virtual void OnResultExecuting(ResultExecutingContext context) {
29+
30+
if (context.Result is not ObjectResult objectResult) return;
31+
32+
JsonSerializerSettings serializerSettings = new() {
33+
ContractResolver = new DefaultContractResolver(),
34+
Converters = { new VersionConverter() },
35+
};
36+
37+
objectResult.Formatters.Clear();
38+
objectResult.Formatters.Add(new NewtonsoftJsonOutputFormatter(serializerSettings, _arrayPool, _options));
39+
40+
}
41+
42+
}
43+
44+
}

src/Skybrud.Essentials.AspNetCore/Models/Json/JsonBody.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Net;
33
using Newtonsoft.Json;
44

5+
#pragma warning disable CS8618
6+
57
namespace Skybrud.Essentials.AspNetCore.Models.Json {
68

79
/// <summary>

src/Skybrud.Essentials.AspNetCore/Models/Json/JsonMetaData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Net;
33
using Newtonsoft.Json;
44

5+
#pragma warning disable CS8618
6+
57
namespace Skybrud.Essentials.AspNetCore.Models.Json {
68

79
/// <summary>

src/Skybrud.Essentials.AspNetCore/Models/Json/JsonNetResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.AspNetCore.Mvc;
44
using Newtonsoft.Json;
55

6+
#pragma warning disable CS8625
7+
68
namespace Skybrud.Essentials.AspNetCore.Models.Json {
79

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

0 commit comments

Comments
 (0)