public DataTable Analyze(string jsonData) { DataTable dataTable = new DataTable(); dataTable.Columns.Add(); dataTable.Columns.Add(); // Parsing the Json data and adding data to the DataTable two columns at a time. JToken json = JToken.Parse(jsonData); IEnumerable <KeyValuePair <string, JValue> > fields = new JsonFieldsCollector(json).GetAllFields(); if (fields.Count() > 0) { foreach (var field in fields) { dataTable.Rows.Add(field.Key, field.Value); } } else { dataTable.Rows.Add("No data"); } return(dataTable); }
private void NormalizedReportedProperties(Dictionary <string, JToken> reportedProperties) { var json = JToken.Parse(JsonConvert.SerializeObject(reportedProperties)); var fieldsCollector = new JsonFieldsCollector(json); this.ReportedProperties = fieldsCollector.GetAllFields(); }
private Dictionary <string, JValue> FieldsCollection(Dictionary <string, JToken> reportedProperties) { var json = JToken.Parse(JsonConvert.SerializeObject(reportedProperties)); var fieldsCollector = new JsonFieldsCollector(json); return(fieldsCollector.GetAllFields()); }
public async Task <List <DeviceDeploymentStatusServiceModel> > GetDeploymentStatusReport(string id, bool isLatest = true) { List <DeviceDeploymentStatusServiceModel> deviceDeploymentStatuses = new List <DeviceDeploymentStatusServiceModel>(); var deploymentDetails = await this.GetAsync(id, true, isLatest); if (deploymentDetails != null && deploymentDetails.DeploymentMetrics != null && deploymentDetails.DeploymentMetrics.DeviceStatuses != null && deploymentDetails.DeploymentMetrics.DeviceStatuses.Keys.Count > 0) { string deviceQuery = @"deviceId IN [{0}]"; var deviceIdsQuery = string.Join(",", deploymentDetails.DeploymentMetrics.DeviceStatuses.Keys.Select(d => $"'{d}'")); var query = string.Format(deviceQuery, deviceIdsQuery); DeviceServiceListModel devices = await this.GetDeviceListAsync(id, query, isLatest); foreach (var item in deploymentDetails.DeploymentMetrics.DeviceStatuses) { var reportedProperties = devices.Items.First(x => x.Id == item.Key).Twin.ReportedProperties; var json = JToken.Parse(JsonConvert.SerializeObject(reportedProperties)); var fieldsCollector = new JsonFieldsCollector(json); var fields = fieldsCollector.GetAllFields(); deviceDeploymentStatuses.Add(new DeviceDeploymentStatusServiceModel(item.Key, item.Value, fields)); } } return(deviceDeploymentStatuses); }
private static string SetConfigurationWithPhrases(string configJson) { var phrases = Regex.Matches(configJson, "{{\\w+}}", RegexOptions.CultureInvariant) .OfType <Match>().Select(match => match.Groups.FirstOrDefault()?.Value).ToList(); var fields = new JsonFieldsCollector(((JToken)configJson).Values().First()).GetAllFields().ToList(); var token = (JToken)configJson; Parallel.ForEach(phrases, new ParallelOptions() { MaxDegreeOfParallelism = 3 }, s => { s = s?.TrimStart('{'); s = s?.TrimEnd('}'); foreach (var(key, value) in fields.ToList()) { if (!(value?.Value is null) && value.Value.ToString().Equals(s)) { token[key] = FindParentWithField(token[key] !.Parent !, value.ToString(CultureInfo.InvariantCulture)); } } });
public static async Task Run([EventHubTrigger(eventHubName: "twin-change", Connection = "TwinChangeEventHubConnectionString", ConsumerGroup = "%DeviceDeploymentHistoryConsumerGroup%")] EventData[] events, ILogger log) { bool exceptionOccurred = false; List <Task> list = new List <Task>(); foreach (EventData message in events) { try { message.Properties.TryGetValue("tenant", out object tenant); if (tenant != null) { string eventData = Encoding.UTF8.GetString(message.Body.Array); Twin twin = JsonConvert.DeserializeObject <Twin>(eventData); TwinServiceModel twinServiceModel = new TwinServiceModel(twin); Dictionary <string, JToken> reportedProperties = twinServiceModel.ReportedProperties; var json = JToken.Parse(JsonConvert.SerializeObject(reportedProperties)); var fieldsCollector = new JsonFieldsCollector(json); var fields = fieldsCollector.GetAllFields(); bool isFirmWareUpdate = false; foreach (var field in fields) { isFirmWareUpdate = field.Key.Contains(FIRMWARE, StringComparison.OrdinalIgnoreCase); if (isFirmWareUpdate) { break; } } if (isFirmWareUpdate) { message.SystemProperties.TryGetValue("iothub-connection-device-id", out object deviceId); var newTwin = await TenantConnectionHelper.GetRegistry(Convert.ToString(tenant)).GetTwinAsync(deviceId.ToString()); var appliedConfigurations = newTwin.Configurations.Where(c => c.Value.Status.Equals(ConfigurationStatus.Applied)); if (appliedConfigurations != null && appliedConfigurations.Count() > 0) { DeploymentSyncService service = new DeploymentSyncService(); var appliedDeploymentFromStorage = service.GetDeploymentsByIdFromStorage(Convert.ToString(tenant), appliedConfigurations.Select(ac => ac.Key).ToArray()).Result.FirstOrDefault(); await service.SaveDeploymentHistory(Convert.ToString(tenant), appliedDeploymentFromStorage, newTwin); } } } } catch (Exception ex) { log.LogError($"Error occurrred : {ex.Message} StackTrace: {ex.StackTrace} Inner Exception: {(string.IsNullOrEmpty(ex.StackTrace) ? string.Empty : ex.StackTrace)}"); exceptionOccurred = true; } } if (exceptionOccurred) { throw new Exception("Function Failed with exception"); } }
private static void WriteLines(string file) { var text = File.ReadAllText(file); var json = JToken.Parse(text); var fieldsCollector = new JsonFieldsCollector(json, AccessModifier.Public); var fields = fieldsCollector.GetAllFields(); var fileName = Path.GetFileName(file); Console.WriteLine($"Filename: {fileName}"); Console.WriteLine(); foreach (var field in fields) { Console.WriteLine($"{field.Key}: '{field.Value}'"); } Console.WriteLine(); }
public SearchResultsViewModel Search(string searchString, bool full = false) { var res = new SearchResultsViewModel { Attachments = Mapper.Map <List <Attachment>, List <AttachmentViewModel> >(_unitOfWork.Attachments.GetBy(x => x.Title.ToLower().Contains(searchString.ToLower())).Take(full ? Int32.MaxValue : 3).ToList()), Users = Mapper.Map <List <User>, List <UserViewModel> >(_unitOfWork.Users.GetBy(x => x.Name.ToLower().Contains(searchString.ToLower())).ToList()), Projects = Mapper.Map <List <Project>, List <ProjectViewModel> >(_unitOfWork.Projects.GetBy(x => x.Name.ToLower().Contains(searchString.ToLower()) || x.Description.ToLower().Contains(searchString.ToLower())).Take(full ? Int32.MaxValue : 3).ToList()), Ticktes = Mapper.Map <List <Ticket>, List <TicketViewModel> >(_unitOfWork.Tickets.GetBy(x => x.Title.ToLower().Contains(searchString.ToLower()) || x.Description.ToLower().Contains(searchString.ToLower())).Take(full ? Int32.MaxValue : 3).ToList()), }; var jsonSer = new JavaScriptSerializer(); res.Templates = new List <TemplateSearchViewModel>(); foreach (var id in _unitOfWork.Tickets.GetAll().Select(x => x.Id).ToList()) { var templates = _templateService.GetTemplates(id); //var tempres = new List<TemplateViewModel>(); foreach (var item in templates) { var json = JToken.Parse(jsonSer.Serialize(item)); var fieldsCollector = new JsonFieldsCollector(json); if (fieldsCollector.GetAllFields().Any(x => x.Value.ToString().ToLower().Contains(searchString.ToLower()))) { res.Templates.Add(new TemplateSearchViewModel { Template = item, Ticket = Mapper.Map <Ticket, TicketViewModel>(_unitOfWork.Tickets.GetById(id)) }); } } //var tempres = templates.Where(x => x.GetType().GetProperties().Where(pi => pi.PropertyType == typeof(string) && pi.GetValue(x, null)!=null).Any(y => y.GetValue(x, null).ToString().Contains(searchString))); //foreach (var item in tempres) //{ // res.Templates.Add(new TemplateSearchViewModel // { // Template = item, // Ticket = Mapper.Map<Ticket, TicketViewModel>(_unitOfWork.Tickets.GetById(id)) // }); //} } return(res); }
public async Task <ActionResult <JToken> > Get(Data data) { string jsonString = ""; Translate translator = new Translate(); Stream stream = new MemoryStream(data.jsonFile.Buffer); StreamReader jsonReader = new StreamReader(stream); jsonString = await jsonReader.ReadToEndAsync(); JsonFieldsCollector dataCollector = new JsonFieldsCollector(jsonString); foreach (var field in data.fields) { dataCollector.jsonObject[field] = translator.TranslateText(dataCollector.jsonObject[field].ToString()); } jsonObj = dataCollector.jsonObject; dataColl = dataCollector; return(jsonObj); }
public async Task <ActionResult> Valid(IFormFile jsonFile) { if (jsonFile != null) { string jsonString = ""; StreamReader streamReader = new StreamReader(jsonFile.OpenReadStream()); jsonString = await streamReader.ReadToEndAsync(); JsonFieldsCollector fieldsCollector = new JsonFieldsCollector(jsonString); ViewData["name"] = jsonFile.FileName; ViewData["fields"] = fieldsCollector; return(View("Privacy")); } else { return(View("Index")); } }
private static void UseJsonFieldsCollector() { var example = @"Examples\Example1.JSON"; var example1 = @"c:\em\test2.json"; var files = new List <string> { //example, example1 }; foreach (var file in files) { var text = File.ReadAllText(file); var json = JToken.Parse(text); var fieldsCollector = new JsonFieldsCollector(json, AccessModifier.Public); var fields = fieldsCollector.GetAllFields(); foreach (var field in fields) { Console.WriteLine($"{field.Key}: '{field.Value}'"); } } }