While setting up applications to communicate between various different trade partners and our WMS software I often have to parse very similar information from various different file formats. In this specific case i have to retrieve the tracking data from different files in csv, xml or json format. I've set up one template class for each file format to map to the respective fields. Each template derives from VersandInformationenVorlageTrackingInformationTemplate class. Now I have come up with this code to select the specific template i need for the parsing.
using CommandLine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace RBHelper
{
[Verb("versanddatenimport")]
class VersanddatenImportCommandTrackinDataImportCommand : ICommand
{
[Option('i',"input", Required = true)]
public string FileName { get; set; }
[Option('v',"vorlage")]
public string Vorlage Template{ get; set; }
private Dictionary<DateiEndung, Func<List<VersandInformationen>>>Func<List<TrackingInformations>>> _convertOperations;
public VersanddatenImportCommandTrackinDataImportCommand()
{
_convertOperations = new Dictionary<DateiEndungDictionary<FileExtension, Func<List<VersandInformationen>>>Func<List<TrackingInformations>>>()
{
{DateiEndungFileExtension.csv, ()=> FromCsv()},
{DateiEndungFileExtension.json, ()=> FromJson()},
{DateiEndungFileExtension.xml, ()=> FromXml()}
};
}
public void Execute()
{
// Versanddatenload ladenTrackinData
List<VersandInformationen>List<TrakingInformations> vis = ParseVis();
// TODO: Working with loaded data
}
private List<VersandInformationen>List<TrackingInformations> ParseVis()
{
DateiEndungFileExtension ext = (DateiEndungFileExtension)Enum.Parse(typeof(DateiEndungFileExtension), Path.GetExtension(FileName).Substring(1));
if(!_convertOperations.Keys.Contains(ext))
throw new NotSupportedException($"{ext} wird nichtnot unterstütztsupported.");
return _convertOperations[ext]();
}
private List<VersandInformationen>List<TrackingInformations> FromCsv()
{
CsvVersandInformationenVorlage vorlageCsvtrackingInformationTemplate =template= CsvVersandInformationenVorlageCsvTrackingInformationTemplate.Default;
if (!string.IsNullOrEmpty(VorlageTemplate)) vorlage =template= VersandInformationenVorlageTrackingInformationTemplate.Load<CsvVersandInformationenVorlage>Load<CsvTrackingInformationTemplate>(VorlageTemplate);
return vorlagetemplate.DeserializeFile(FileName);
}
private List<VersandInformationen>List<TrackingInformations> FromJson()
{
JsonVersandInformationenVorlageJsonTrackingInformationTemplate vorlagetemplate = JsonVersandInformationenVorlageJsonTrackingInformationTemplate.Default;
if (!string.IsNullOrEmpty(VorlageTemplate)) vorlagetemplate = VersandInformationenVorlageTrackingInformationTemplate.Load<JsonVersandInformationenVorlage>Load<JsonTrackingInformatioTemplate>(VorlageTemplate);
return vorlagetemplate.DeserializeFile(FileName);
}
private List<VersandInformationen>List<TrackingInformations> FromXml()
{
XmlVersandInformationenVorlageXmlTrackingInformationTemplate vorlagetemplate = XmlVersandInformationenVorlageXmlTrackingInformationTemplate.Default;
if (!string.IsNullOrEmpty(VorlageTemlate)) vorlagetemplate = VersandInformationenVorlageTrackingInformationTemplate.Load<XmlVersandInformationenVorlage>Load<XmlTrackingInformationTemplate>(VorlageTemplate);
return vorlagetemplate.DeserializeFile(FileName);
}
}
}
[JsonConverter(typeof(StringEnumConverter))]
public enum DateiEndungFileExtension
{
csv,
xml,
json
}
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
namespace RBHelper
{
abstract class VersandInformationenVorlageTrackingInformationTemplate
{
public DateiEndungFileExtension DateiEndungFileExtension { get; private set; }
public int kUser;
public string EigeneVersandinfo;CustomInfo;
public string EigeneRoute;CustomRoute;
public VersandInformationenVorlageTrackingInformationTemplate(DateiEndungFileExtension dateiEndungFileExtension) => DateiEndungFileExtension = DateiEndung;FileExtension;
public static T Load<T>(string fileName) where T : VersandInformationenVorlage =>TrackingInformationTemplate=> JsonConvert.DeserializeObject<T>(File.ReadAllText(fileName));
public void Save(string fileName) => File.WriteAllText(fileName, JsonConvert.SerializeObject(this, Formatting.Indented));
public abstract List<VersandInformationen>List<TrackingInformations> DeserializeFile(string fileName);
protected List<VersandInformationen>List<TrackingInformations> Deserialize(List<VersandInformationen>List<TrackingInformations> input)
{
List<VersandInformationen>List<TrackingInformations> output = new List<VersandInformationen>List<TrackingInformations>();
foreach (VersandInformationenTrackingInformations v in input)
if (!output.Contains(v)) output.Add(v);
return output;
}
protected void Serialize(string serialized, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(serialized);
}
}
}
}
And here the JsonVersandInformationenVorlageJsonTrackingInformationTemplate class
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace RBHelper
{
class JsonVersandInformationenVorlage JsonTrackingInformationTemplate: VersandInformationenVorlageTrackingInformationTemplate
{
public JsonVersandInformationenVorlageJsonTrackingInformationTemplate() : base( DateiEndung.json) { }
public string RootNode;
public string Versanddatum;TrackingDate;
public string Id;
public string Versandinfo;Info;
public string TrackingId;
public string Route;
public static JsonVersandInformationenVorlageJsonTrackingInformationTemplate Default => new JsonVersandInformationenVorlageJsonTrackingInformationTemplate()
{
kUser = 1,
RootNode = "[*]",
VersanddatumTrackingDate = "Versanddatum",
Id = "Id",
VersandinfoInfo = "Versandinfo",
TrackingId = "TrackingId",
Route = "Route"
};
public override List<VersandInformationen>List<TrackingInformations> DeserializeFile(string fileName)
{
JToken doc = JToken.Parse(File.ReadAllText(fileName));
return doc.SelectTokens(RootNode).Select(a => DeserializeRecord(a)).ToList();
}
public VersandInformationenTrackingInformations DeserializeRecord(JToken token)
{
string versandInfoinfo = string.IsNullOrEmpty(EigeneVersandinfoCustomInfo) ? token[Versandinfo]token[Info].Value<string>() : EigeneVersandinfo;CustomInfo;
string route = string.IsNullOrEmpty(EigeneRouteCustomRoute) ? token[Route].Value<string>() : EigeneRoute;CustomRoute;
return new VersandInformationenTrackingInformations()
{
VersanddatumTrackingDate = DateTime.Parse(token[Versanddatum]token[TrackingDate].Value<string>()),
Id = token[Id].Value<string>(),
VersandinfoInfo = versandInfoinfo,
TrackingId = routetoken[TrackingId].Value<string>(),
Route = token[Route].Value<string>()route
};
}
}
}