From cd11f49b4a2e3f7ccfd39575280ed3b8101d745c Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Mon, 10 Oct 2016 15:08:27 +0200
Subject: [PATCH] merge writing json files

---
 VECTO/Configuration.vb             |   9 +--
 VECTO/JSONWriter.vb                |  53 ---------------
 VECTO/OutputData/JSONFileWriter.vb | 101 +++++++++++++++++------------
 3 files changed, 63 insertions(+), 100 deletions(-)
 delete mode 100644 VECTO/JSONWriter.vb

diff --git a/VECTO/Configuration.vb b/VECTO/Configuration.vb
index 867e09ff04..f4a18ed4ef 100644
--- a/VECTO/Configuration.vb
+++ b/VECTO/Configuration.vb
@@ -73,7 +73,7 @@ Public Class Configuration
 				LogSize = body.GetEx(Of Double)("LogSize")
 				AirDensity = body.GetEx(Of Double)("AirDensity")
 				FuelDens = body.GetEx(Of Double)("FuelDensity")
-				CO2perFC = body.GetEx(Of Double)("CO2perFC")
+				Co2PerFc = body.GetEx(Of Double)("CO2perFC")
 				OpenCmd = body.GetEx(Of String)("OpenCmd")
 				OpenCmdName = body.GetEx(Of String)("OpenCmdName")
 				FirstRun = body.GetEx(Of Boolean)("FirstRun")
@@ -85,7 +85,6 @@ Public Class Configuration
 	End Sub
 
 	Public Sub Save()
-		Dim json As New JSONWriter
 
 		Dim header As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
 		header.Add("CreatedBy", Lic.LicString & " (" & Lic.GUID & ")")
@@ -93,21 +92,19 @@ Public Class Configuration
 		header.Add("AppVersion", VECTOvers)
 		header.Add("FileVersion", FormatVersion)
 
-
 		Dim body As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
 		body.Add("ModOut", ModOut)
 		body.Add("Mod1Hz", Mod1Hz)
 		body.Add("LogSize", LogSize)
 		body.Add("AirDensity", AirDensity)
 		body.Add("FuelDensity", FuelDens)
-		body.Add("CO2perFC", CO2perFC)
+		body.Add("CO2perFC", Co2PerFc)
 		body.Add("OpenCmd", OpenCmd)
 		body.Add("OpenCmdName", OpenCmdName)
 		body.Add("FirstRun", FirstRun)
 		body.Add("DeclMode", DeclMode)
 
-		json.Content = JToken.FromObject(New Dictionary(Of String, Object) From {{"Header", header}, {"Body", body}})
-		json.WriteFile(FilePath)
+		JSONFileWriter.WriteFile(New Dictionary(Of String, Object) From {{"Header", header}, {"Body", body}}, FilePath)
 	End Sub
 End Class
 
diff --git a/VECTO/JSONWriter.vb b/VECTO/JSONWriter.vb
deleted file mode 100644
index e678d8ebb1..0000000000
--- a/VECTO/JSONWriter.vb
+++ /dev/null
@@ -1,53 +0,0 @@
-' Copyright 2014 European Union.
-' Licensed under the EUPL (the 'Licence');
-'
-' * You may not use this work except in compliance with the Licence.
-' * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
-' * Unless required by applicable law or agreed to in writing,
-'   software distributed under the Licence is distributed on an "AS IS" basis,
-'   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-'
-' See the LICENSE.txt for the specific language governing permissions and limitations.
-
-Imports System.IO
-Imports System.Linq
-Imports Newtonsoft.Json
-Imports Newtonsoft.Json.Linq
-
-''' <summary>
-''' uses JSON.NET http://json.codeplex.com/
-''' </summary>
-''' <remarks></remarks>
-Public Class JSONWriter
-	Public Content As JToken 'Dictionary(Of String, Object)
-
-	Public Sub New()
-		'Content = New Dictionary(Of String, Object)
-	End Sub
-
-
-	''' <summary>
-	''' Writes the Content variable into a JSON file.
-	''' </summary>
-	''' <param name="path"></param>
-	''' <returns></returns>
-	''' <remarks></remarks>
-	Public Sub WriteFile(path As String)
-		Dim file As StreamWriter
-		Dim str As String
-
-		If Content.Count = 0 Then
-			Return
-		End If
-
-		Try
-			str = JsonConvert.SerializeObject(Content, Formatting.Indented)
-			file = My.Computer.FileSystem.OpenTextFileWriter(path, False)
-		Catch ex As Exception
-			Throw
-		End Try
-
-		file.Write(str)
-		file.Close()
-	End Sub
-End Class
diff --git a/VECTO/OutputData/JSONFileWriter.vb b/VECTO/OutputData/JSONFileWriter.vb
index d32900443c..020149a33a 100644
--- a/VECTO/OutputData/JSONFileWriter.vb
+++ b/VECTO/OutputData/JSONFileWriter.vb
@@ -1,6 +1,7 @@
 Imports System.Collections.Generic
 Imports System.IO
 Imports System.Linq
+Imports Newtonsoft.Json
 Imports Newtonsoft.Json.Linq
 Imports TUGraz.VectoCommon.InputData
 Imports TUGraz.VectoCommon.Models
@@ -9,13 +10,13 @@ Imports TUGraz.VectoCore.Models.Declaration
 
 Public Class JSONFileWriter
 	Implements IOutputFileWriter
-	Public Const EngineFormatVersion As Short = 3
+	Public Const EngineFormatVersion As Integer = 3
 
-	Public Const GearboxFormatVersion As Short = 6
+	Public Const GearboxFormatVersion As Integer = 6
 
-	Public Const VehicleFormatVersion As Short = 7
+	Public Const VehicleFormatVersion As Integer = 7
 
-	Private Const VectoJobFormatVersion As Short = 3
+	Private Const VectoJobFormatVersion As Integer = 3
 
 	Private Shared _instance As JSONFileWriter
 
@@ -28,14 +29,9 @@ Public Class JSONFileWriter
 
 	Public Sub SaveEngine(eng As IEngineEngineeringInputData, filename As String) _
 		Implements IOutputFileWriter.SaveEngine
-		Dim json As New JSONWriter
 
 		'Header
-		Dim header As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
-		header.Add("CreatedBy", Lic.LicString & " (" & Lic.GUID & ")")
-		header.Add("Date", Now.ToUniversalTime().ToString("o"))
-		header.Add("AppVersion", VECTOvers)
-		header.Add("FileVersion", EngineFormatVersion)
+		Dim header As Dictionary(Of String, Object) = GetHeader(EngineFormatVersion)
 
 		'Body
 		Dim body As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
@@ -57,22 +53,24 @@ Public Class JSONFileWriter
 		body.Add("WHTC-Motorway", eng.WHTCMotorway)
 		body.Add("ColdHotBalancingFactor", eng.ColdHotBalancingFactor)
 
-		json.Content = JToken.FromObject(New Dictionary(Of String, Object) From {{"Header", header}, {"Body", body}})
-
-		json.WriteFile(filename)
+		WriteFile(header, body, filename)
 	End Sub
 
-	Public Sub SaveGearbox(gbx As IGearboxEngineeringInputData, axl As IAxleGearInputData, filename As String) _
-		 Implements IOutputFileWriter.SaveGearbox
-
-		Dim json As New JSONWriter
-
-		'Header
+	Protected Function GetHeader(fileVersion As Integer) As Dictionary(Of String, Object)
 		Dim header As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
+
 		header.Add("CreatedBy", Lic.LicString & " (" & Lic.GUID & ")")
 		header.Add("Date", Now.ToUniversalTime().ToString("o"))
 		header.Add("AppVersion", VECTOvers)
-		header.Add("FileVersion", GearboxFormatVersion)
+		header.Add("FileVersion", fileVersion)
+		Return header
+	End Function
+
+	Public Sub SaveGearbox(gbx As IGearboxEngineeringInputData, axl As IAxleGearInputData, filename As String) _
+		Implements IOutputFileWriter.SaveGearbox
+
+		'Header
+		Dim header As Dictionary(Of String, Object) = GetHeader(GearboxFormatVersion)
 
 
 		'Body
@@ -142,22 +140,16 @@ Public Class JSONFileWriter
 		body.Add("UpshiftAfterDownshiftDelay", gbx.UpshiftAfterDownshiftDelay.Value())
 		body.Add("UpshiftMinAcceleration", gbx.UpshiftMinAcceleration.Value())
 
-		json.Content = JToken.FromObject(New Dictionary(Of String, Object) From {{"Header", header}, {"Body", body}})
-
-		json.WriteFile(filename)
+		WriteFile(header, body, filename)
 	End Sub
 
 	Public Sub SaveVehicle(vehicle As IVehicleEngineeringInputData, retarder As IRetarderInputData,
-								pto As IPTOTransmissionInputData, angledrive As IAngledriveInputData, filename As String) _
+							pto As IPTOTransmissionInputData, angledrive As IAngledriveInputData, filename As String) _
 		Implements IOutputFileWriter.SaveVehicle
 		Dim basePath As String = Path.GetDirectoryName(filename)
-		Dim json As New JSONWriter
+
 		'Header
-		Dim header As Dictionary(Of String, Object) = New Dictionary(Of String, Object) From {
-				{"CreatedBy", Lic.LicString & " (" & Lic.GUID & ")"},
-				{"Date", Now.ToUniversalTime().ToString("o")},
-				{"AppVersion", VECTOvers},
-				{"FileVersion", VehicleFormatVersion}}
+		Dim header As Dictionary(Of String, Object) = GetHeader(VehicleFormatVersion)
 
 		'Body
 
@@ -232,20 +224,14 @@ Public Class JSONFileWriter
 				}
 				}
 
-		json.Content = JToken.FromObject(New Dictionary(Of String, Object) From {{"Header", header}, {"Body", body}})
-		json.WriteFile(filename)
+		WriteFile(header, body, filename)
 	End Sub
 
 	Public Sub SaveJob(input As IEngineeringInputDataProvider, filename As String) _
 		Implements IOutputFileWriter.SaveJob
-		Dim json As New JSONWriter
 		Dim basePath As String = Path.GetDirectoryName(filename)
 		'Header
-		Dim header As Dictionary(Of String, Object) = New Dictionary(Of String, Object) From {
-				{"CreatedBy", Lic.LicString & " (" & Lic.GUID & ")"},
-				{"Date", Now.ToUniversalTime().ToString("o")},
-				{"AppVersion", VECTOvers},
-				{"FileVersion", VectoJobFormatVersion}}
+		Dim header As Dictionary(Of String, Object) = GetHeader(VectoJobFormatVersion)
 
 		'Body
 		Dim body As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
@@ -264,6 +250,7 @@ Public Class JSONFileWriter
 			body.Add("EngineFile", GetRelativePath(input.EngineInputData.Source, basePath))
 			body.Add("Cycles",
 					job.Cycles.Select(Function(x) GetRelativePath(x.CycleData.Source, Path.GetDirectoryName(filename))).ToArray())
+			WriteFile(header, body, filename)
 			Return
 		End If
 
@@ -348,12 +335,44 @@ Public Class JSONFileWriter
 					job.Cycles.Select(Function(x) GetRelativePath(x.CycleData.Source, Path.GetDirectoryName(filename))).ToArray())
 		End If
 
-		json.Content = JToken.FromObject(New Dictionary(Of String, Object) From {{"Header", header}, {"Body", body}})
-		json.WriteFile(filename)
+		WriteFile(header, body, filename)
 	End Sub
 
-	Public Sub ExportJob(input As IEngineeringInputDataProvider, filename As String, separateFiles As Boolean) Implements IOutputFileWriter.ExportJob
+	Public Sub ExportJob(input As IEngineeringInputDataProvider, filename As String, separateFiles As Boolean) _
+		Implements IOutputFileWriter.ExportJob
 		Throw New NotImplementedException
 	End Sub
 
+	''' <summary>
+	''' Writes the Content variable into a JSON file.
+	''' </summary>
+	''' <param name="path"></param>
+	''' <remarks></remarks>
+	Public Shared Sub WriteFile(content As JToken, path As String)
+		Dim file As StreamWriter
+		Dim str As String
+
+		If content.Count = 0 Then
+			Return
+		End If
+
+		Try
+			str = JsonConvert.SerializeObject(content, Formatting.Indented)
+			file = My.Computer.FileSystem.OpenTextFileWriter(path, False)
+		Catch ex As Exception
+			Throw
+		End Try
+
+		file.Write(str)
+		file.Close()
+	End Sub
+
+	Public Shared Sub WriteFile(content As Dictionary(Of String, Object), path As String)
+		WriteFile(JToken.FromObject(content), path)
+	End Sub
+
+	Protected Shared Sub WriteFile(header As Dictionary(Of String, Object), body As Dictionary(Of String, Object),
+									path As String)
+		WriteFile(JToken.FromObject(New Dictionary(Of String, Object) From {{"Header", header}, {"Body", body}}), path)
+	End Sub
 End Class
\ No newline at end of file
-- 
GitLab