private void Tvic_Selected(object sender, RoutedEventArgs e) { TreeViewItem selected = (TreeViewItem)sender; REST.Command cmd = (REST.Command)selected.Tag; var obj = Activator.CreateInstance(cmd.JSONType); RestAction = cmd.Action; if (ResourcePanel != null) { StkResource.Children.Remove(ResourcePanel); } ResourcePanel = new Resource_StackPanel((string)selected.Header, ResourcePanel_StatusChanged); StkResource.Children.Add(ResourcePanel); BtnSend.Content = Enum.GetName(typeof(REST.Actions), RestAction); TxtErrorResponse.Text = string.Empty; TxtErrorResponse.Visibility = Visibility.Collapsed; if (RestAction == REST.Actions.POST || RestAction == REST.Actions.PUT) { BtnSend.IsEnabled = true; ScvJSONSchema.Visibility = Visibility.Collapsed; TxtJSONSchema.Text = string.Empty; BtnPasteJSON.Visibility = Visibility.Visible; ScvJSONData.Visibility = Visibility.Visible; StkJsonData.Children.Clear(); StkJsonData.Children.Add(new TextBlock() { Text = "{", Margin = new Thickness(0) }); int i = 1; int len = cmd.JSONType.GetProperties().Length; foreach (PropertyInfo prop in cmd.JSONType.GetProperties()) { if (prop.Name.Equals("details")) { StkJsonData.Children.Add(new TextBlock() { Text = " \"details\": [", Margin = new Thickness(0) }); StkJsonData.Children.Add(new TextBlock() { Text = " {", Margin = new Thickness(0) }); i = 1; len = typeof(JobRequestDetail_POST).GetProperties().Length; foreach (PropertyInfo prop1 in typeof(JobRequestDetail_POST).GetProperties()) { JSONEditor_StackPanel stk1 = new JSONEditor_StackPanel(prop1, !(++i <= len)); StkJsonData.Children.Add(stk1); } StkJsonData.Children.Add(new TextBlock() { Text = " },", Margin = new Thickness(0) }); StkJsonData.Children.Add(new TextBlock() { Text = " {", Margin = new Thickness(0) }); i = 1; foreach (PropertyInfo prop1 in typeof(JobRequestDetail_POST).GetProperties()) { JSONEditor_StackPanel stk1 = new JSONEditor_StackPanel(prop1, !(++i <= len)); StkJsonData.Children.Add(stk1); } StkJsonData.Children.Add(new TextBlock() { Text = " }]", Margin = new Thickness(0) }); continue; } JSONEditor_StackPanel stk = new JSONEditor_StackPanel(prop, !(++i <= len)); StkJsonData.Children.Add(stk); } StkJsonData.Children.Add(new TextBlock() { Text = "}" }); Dispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() => { ((JSONEditor_StackPanel)StkJsonData.Children[1]).SetFocus(); })); } else { ScvJSONData.Visibility = Visibility.Collapsed; StkJsonData.Children.Clear(); BtnPasteJSON.Visibility = Visibility.Collapsed; ScvJSONSchema.Visibility = Visibility.Visible; TxtJSONSchema.Text = JsonConvert.SerializeObject(obj, cmd.JSONType, Formatting.Indented, new JsonSerializerSettings() { ObjectCreationHandling = ObjectCreationHandling.Reuse }).Replace("null", "\"\""); if (RestAction == REST.Actions.STREAM) { BtnSend.IsEnabled = true; } //if(RestAction == REST.Actions.GET) //{ // Dispatcher.BeginInvoke(DispatcherPriority.Render, // (Action)(() => // { // Keyboard.Focus(TxtResourceValue); // })); //} //else //{ // TxtResourceValue.Visibility = Visibility.Collapsed; // TxtResourceValue.Text = string.Empty; //} } }
private void DeserializeJSONtoDataGrid(REST.Command cmd, string json) { DataTable datatable = new DataTable(); foreach (PropertyInfo prop in cmd.JSONType.GetProperties()) { if (prop.Name.Equals("AdditionalProperties")) { continue; } if (prop.Name.Equals("Details")) { continue; } DataColumn column; if (prop.PropertyType == typeof(Timestamp)) { column = new DataColumn { DataType = typeof(DateTime), ColumnName = prop.Name, ReadOnly = true }; } else if (prop.PropertyType.IsEnum) { column = new DataColumn { DataType = typeof(string), ColumnName = prop.Name, ReadOnly = true }; } else { column = new DataColumn { DataType = prop.PropertyType, ColumnName = prop.Name, ReadOnly = true }; } datatable.Columns.Add(column); } JArray lst; if (json.StartsWith("[")) { lst = JsonConvert.DeserializeObject <JArray>(json); } else { lst = JsonConvert.DeserializeObject <JArray>($"[{json}]"); } foreach (JObject elem in lst) { DataRow dr = datatable.NewRow(); foreach (PropertyInfo prop in cmd.JSONType.GetProperties()) { if (prop.Name.Equals("AdditionalProperties")) { continue; } if (prop.Name.Equals("Details")) { continue; } if (prop.PropertyType == typeof(Timestamp)) { dr[prop.Name] = DateTimeOffset.FromUnixTimeMilliseconds((long)elem.Property($"{char.ToLower(prop.Name[0])}{prop.Name.Substring(1)}").Value["millis"]).DateTime; } else { JProperty prop1; if ((prop1 = elem.Property($"{char.ToLower(prop.Name[0])}{prop.Name.Substring(1)}")) != null) { dr[prop.Name] = prop1.Value; } } } datatable.Rows.Add(dr); } DgvReturnedJSON.ItemsSource = null; DgvReturnedJSON.ItemsSource = datatable.DefaultView; if (datatable.Columns.Contains("upd")) { DgvReturnedJSON.Items.SortDescriptions.Add(new SortDescription("upd", ListSortDirection.Descending)); } }