11using System ;
2- using System . Collections . Generic ;
2+ using System . IO ;
3+ using Newtonsoft . Json ;
34
45namespace UnityEngineAnalyzer . CLI . Reporting
56{
67 public class JsonAnalyzerExporter : IAnalyzerExporter
78 {
8- private readonly List < DiagnosticInfo > _diagnostics = new List < DiagnosticInfo > ( ) ;
9+ private JsonTextWriter _jsonWriter ;
10+ private readonly JsonSerializer _jsonSerializer = new JsonSerializer ( ) ;
911
1012 public void AppendDiagnostic ( DiagnosticInfo diagnosticInfo )
1113 {
12- _diagnostics . Add ( diagnosticInfo ) ;
14+ _jsonSerializer . Serialize ( _jsonWriter , diagnosticInfo ) ;
1315 }
1416
1517 public void Finish ( TimeSpan duration )
1618 {
17- Console . WriteLine ( "This is where we write the json file : " + _diagnostics . Count ) ;
19+ _jsonWriter . WriteEndArray ( ) ;
20+ _jsonWriter . WriteEndObject ( ) ;
21+ _jsonWriter . Close ( ) ;
1822 }
1923
20- public void InitializeExporter ( string fileName )
24+ public void InitializeExporter ( FileInfo projectFile )
2125 {
22- throw new NotImplementedException ( ) ;
26+ if ( ! projectFile . Exists )
27+ {
28+ throw new ArgumentException ( "Project file does not exist" ) ;
29+ }
30+
31+
32+ var newFileName = projectFile . Name . Replace ( projectFile . Extension , ".json" ) ;
33+ var jsonFilePath = Path . Combine ( projectFile . DirectoryName , newFileName ) ;
34+ var jsonFile = new FileInfo ( jsonFilePath ) ;
35+
36+ if ( jsonFile . Exists )
37+ {
38+ jsonFile . Delete ( ) ;
39+ }
40+
41+ TextWriter textWriter = new StreamWriter ( jsonFile . FullName ) ;
42+ _jsonWriter = new JsonTextWriter ( textWriter ) ;
43+
44+ _jsonWriter . WriteStartObject ( ) ;
45+ _jsonWriter . WritePropertyName ( "File" ) ;
46+ _jsonWriter . WriteValue ( projectFile . FullName ) ;
47+ _jsonWriter . WritePropertyName ( "Date" ) ;
48+ _jsonWriter . WriteValue ( DateTime . Now ) ;
49+ _jsonWriter . WritePropertyName ( "Diagnostics" ) ;
50+ _jsonWriter . WriteStartArray ( ) ;
51+
52+
53+ _jsonSerializer . Formatting = Formatting . Indented ;
2354 }
2455 }
2556}
0 commit comments