public NativeJsonFileDTO ConvertFile(string Base64File, Dictionary <string, object> FileData) { NativeJsonFile NativeJsonFile = new NativeJsonFile(); NativeJsonFile = GetJsonSerializedFile(Base64File, FileData); return(new NativeJsonFileDTO { Content = NativeJsonFile.Content, Columns = NativeJsonFile.Columns }); }
public NativeJsonFile GetJsonSerializedFile(string Base64File, Dictionary <string, object> FileData) { try { object FileType = new object(); NativeJsonFile NativeJsonFile = new NativeJsonFile(); bool FileTyeFounded = FileData.TryGetValue("FileType", out FileType); if (!FileTyeFounded) { throw new Exception(ServicesConstants.NotTypeFileFound); } var clazz = Type.GetType("Hexagon.Services.ConvertSourceFileToJsonStrategy.Convert" + FileType.ToString() + "ToJsonStrategy"); var Strategy = (IConvertSourceFileToJsonStrategy)Activator.CreateInstance(clazz); return(Strategy.Do(Base64File, FileData)); } catch (Exception Exception) { throw Exception; } }
public NativeJsonFile Do(string Base64File, Dictionary <string, object> FileData) { var Base64EncodedBytes = System.Convert.FromBase64String(Base64File); MemoryStream SourceFile = new MemoryStream(Base64EncodedBytes); object Delimiter = null; FileData.TryGetValue("Delimiter", out Delimiter); if (Delimiter == null) { throw new Exception(ServicesConstants.DelimiterMissing); } object HasTitleInDefinition = null; FileData.TryGetValue("HasTitle", out HasTitleInDefinition); bool HasTitle = HasTitleInDefinition != null && Convert.ToBoolean(HasTitleInDefinition.ToString()); string JsonFileConverted = ""; string [] Columns = null; int Step = 0; List <JObject> JObjectList = new List <JObject>(); using (StreamReader StreamReader = new StreamReader(SourceFile)) { String ActualLine = ""; while ((ActualLine = StreamReader.ReadLine()) != null) { string[] DataInLine = ActualLine.Split(Delimiter.ToString()); int ColumnsQuantity = DataInLine.Length; Step++; if (Step == 1) { if (HasTitle) { Columns = DataInLine; } else { Columns = new string[ColumnsQuantity]; for (int i = 0; i < ColumnsQuantity; i++) { Columns[i] = "Column-" + i.ToString(); } } } dynamic JObjectLine = new JObject(); if (Step > 1 || !HasTitle) { int i = 0; foreach (string Data in DataInLine) { JObjectLine[Columns[i]] = new JValue(TypeFunction.GetTyped(Data));; i++; } JObjectList.Add(JObjectLine); } } } var JSonFileConverted = Newtonsoft.Json.JsonConvert.SerializeObject(JObjectList); NativeJsonFile NativeJsonFile = new NativeJsonFile(); NativeJsonFile.Content = JSonFileConverted; NativeJsonFile.Columns = Columns; return(NativeJsonFile); }