11using System ;
2- using System . Text ;
32using System . Threading . Tasks ;
4- using JsonApiDotNetCore . Internal ;
5- using JsonApiDotNetCore . Models ;
6- using JsonApiDotNetCore . Serialization ;
7- using JsonApiDotNetCore . Services ;
83using Microsoft . AspNetCore . Mvc . Formatters ;
94using Microsoft . Extensions . DependencyInjection ;
10- using Microsoft . Extensions . Logging ;
11- using Newtonsoft . Json ;
125
136namespace JsonApiDotNetCore . Formatters
147{
@@ -26,81 +19,8 @@ public bool CanWriteResult(OutputFormatterCanWriteContext context)
2619
2720 public async Task WriteAsync ( OutputFormatterWriteContext context )
2821 {
29- if ( context == null )
30- throw new ArgumentNullException ( nameof ( context ) ) ;
31-
32- var logger = GetService < ILoggerFactory > ( context ) ?
33- . CreateLogger < JsonApiOutputFormatter > ( ) ;
34-
35- var requestMeta = GetService < IRequestMeta > ( context ) ;
36-
37- logger ? . LogInformation ( "Formatting response as JSONAPI" ) ;
38-
39- var response = context . HttpContext . Response ;
40- using ( var writer = context . WriterFactory ( response . Body , Encoding . UTF8 ) )
41- {
42- var jsonApiContext = GetService < IJsonApiContext > ( context ) ;
43-
44- response . ContentType = "application/vnd.api+json" ;
45- string responseContent ;
46- try
47- {
48- responseContent = GetResponseBody ( context . Object , jsonApiContext , requestMeta , logger ) ;
49- }
50- catch ( Exception e )
51- {
52- logger ? . LogError ( new EventId ( ) , e , "An error ocurred while formatting the response" ) ;
53- var errors = new ErrorCollection ( ) ;
54- errors . Add ( new Error ( "400" , e . Message ) ) ;
55- responseContent = errors . GetJson ( ) ;
56- response . StatusCode = 400 ;
57- }
58-
59- await writer . WriteAsync ( responseContent ) ;
60- await writer . FlushAsync ( ) ;
61- }
62- }
63-
64- private T GetService < T > ( OutputFormatterWriteContext context )
65- {
66- return context . HttpContext . RequestServices . GetService < T > ( ) ;
67- }
68-
69- private string GetResponseBody ( object responseObject ,
70- IJsonApiContext jsonApiContext ,
71- IRequestMeta requestMeta ,
72- ILogger logger )
73- {
74- if ( responseObject == null )
75- return GetNullDataResponse ( ) ;
76-
77- if ( responseObject . GetType ( ) == typeof ( Error ) || jsonApiContext . RequestEntity == null )
78- return GetErrorJson ( responseObject , logger ) ;
79-
80- return JsonApiSerializer . Serialize ( responseObject , jsonApiContext , requestMeta ) ;
81- }
82-
83- private string GetNullDataResponse ( )
84- {
85- return JsonConvert . SerializeObject ( new Document
86- {
87- Data = null
88- } ) ;
89- }
90-
91- private string GetErrorJson ( object responseObject , ILogger logger )
92- {
93- if ( responseObject . GetType ( ) == typeof ( Error ) )
94- {
95- var errors = new ErrorCollection ( ) ;
96- errors . Add ( ( Error ) responseObject ) ;
97- return errors . GetJson ( ) ;
98- }
99- else
100- {
101- logger ? . LogInformation ( "Response was not a JSONAPI entity. Serializing as plain JSON." ) ;
102- return JsonConvert . SerializeObject ( responseObject ) ;
103- }
22+ var writer = context . HttpContext . RequestServices . GetService < IJsonApiWriter > ( ) ;
23+ await writer . WriteAsync ( context ) ;
10424 }
10525 }
10626}
0 commit comments