/// <summary> /// Gets the .NET type from JTokenType. /// </summary> /// <param name="jt">The JTokenType to convert</param> /// <returns></returns> /// <exception cref="System.NotImplementedException">support for + jt + is not implemented.</exception> private static string GetTypeFromJTokenType(JTokenType jt) { switch (jt) { case JTokenType.TimeSpan: case JTokenType.Uri: case JTokenType.Boolean: case JTokenType.Guid: case JTokenType.String: return(jt.ToString()); case JTokenType.Bytes: return("byte[]"); case JTokenType.Date: return("DateTime"); case JTokenType.Float: return("double"); case JTokenType.Integer: return("int"); case JTokenType.Null: return("String"); default: throw new NotImplementedException("support for " + jt + " is not implemented."); } }
private static object ReadValueByType(JTokenType currentType, IEnumerable <JToken> token) { switch (currentType.ToString().ToLowerInvariant()) { case "null": return(null); case "boolean": return(token.Value <bool>()); case "int32": case "int64": case "integer": case "long": return(token.Value <long>()); case "float": case "decimal": case "double": case "number": return(token.Value <decimal>()); case "date": case "datetime": return(token.Value <DateTime>()); default: var value = token.Value <string>(); if (string.IsNullOrEmpty(value)) { value = string.Empty; } return(value); } }
public TypeInfo(JTokenType valueType) { switch (valueType) { case JTokenType.Integer: Type = TypeFlags.Int; break; case JTokenType.Float: Type = TypeFlags.Double; break; case JTokenType.String: Type = TypeFlags.String; break; case JTokenType.Boolean: Type = TypeFlags.Bool; break; case JTokenType.Undefined: case JTokenType.Null: Type = TypeFlags.Null; break; default: Type = TypeFlags.Custom; CustomTypeName = valueType.ToString(); break; } }
private ValidationResult ValidateMultiValue(string path, IMetadataDefinition definition, JToken token, JTokenType expectedType) { if (token.Type == JTokenType.Array) { var array = (JArray)token; int index = 0; foreach (var item in array) { if (item.Type != expectedType) { return ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.UnexpectedItemType, $"Bad metadata: unexpected type '{token.Type.ToString()}' for property {path}[{index}], expected type '{expectedType.ToString()}'.", path); } } return ValidationResult.Success; } else if (token.Type == JTokenType.Null) { if (definition.IsRequired) { return ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.FieldRequired, $"Bad metadata: property {path} is required.", path); } return ValidationResult.Success; } return ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.UnexpectedType, $"Bad metadata: unexpected type '{token.Type.ToString()}' for property {path}, expected type '{nameof(JTokenType.Array)}'.", path); }
private JSchemaType GetJSchemaPropertyType(JTokenType item) { if (Enum.TryParse(item.ToString(), out JSchemaType schemaType)) { if (Enum.IsDefined(typeof(JSchemaType), schemaType)) { return(schemaType); } else { throw new ArgumentOutOfRangeException($"{item.ToString()} is not a value in JschemaType enum"); } } else { throw new ArgumentOutOfRangeException($"{item.ToString()} is not a member of JschemaType enum"); } }
/// <summary> /// Converts JTokenType to a string representing the type as expected in a Manifest. /// </summary> /// <param name="type"> /// The type of the extension in the format used to deserialize Model.Json (JToken) /// </param> /// <returns> /// One of the allowed strings representing a type in a Manifest. /// </returns> private static string ConvertJTokenTypeToExpectedString(JTokenType type) { var stringType = type.ToString().ToLower(); if (stringType.Equals("array")) { stringType = "object"; } return(stringType); }
static void GenericJSONObjCreatorRec(JToken token, List <JsonConverterType> list, String cat) { foreach (JToken item in token.Children()) { String[] splitted = item.Path.Split('.'); String propertyname = splitted[splitted.Length - 1]; JTokenType type = item.Type; if (type.Equals(JTokenType.Object) || type.Equals(JTokenType.Property)) { GenericJSONObjCreatorRec(item, list, propertyname + "."); } list.Add(new JsonConverterType(propertyname, cat + propertyname, type.ToString())); } }
private ValidationResult ValidateSimpleValue(string path, IMetadataDefinition definition, JToken token, JTokenType expectedType) { if (token.Type == expectedType) { return ValidationResult.Success; } if (token.Type == JTokenType.Null) { if (definition.IsRequired) { return ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.FieldRequired, $"Bad metadata: property {path} is required.", path); } return ValidationResult.Success; } return ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.UnexpectedType, $"Bad metadata: unexpected type '{token.Type.ToString()}' for property {path}, expected type '{expectedType.ToString()}'.", path); }
/// <summary> /// Gets the .NET type from JTokenType. /// </summary> /// <param name="jt">The JTokenType to convert</param> /// <returns></returns> /// <exception cref="System.NotImplementedException">support for + jt + is not implemented.</exception> private static string GetTypeFromJTokenType(JTokenType jt) { switch (jt) { case JTokenType.TimeSpan: case JTokenType.Uri: case JTokenType.Boolean: case JTokenType.Guid: case JTokenType.String: return jt.ToString(); case JTokenType.Bytes: return "byte[]"; case JTokenType.Date: return "DateTime"; case JTokenType.Float: return "double"; case JTokenType.Integer: return "int"; case JTokenType.Null: return "String"; default: throw new NotImplementedException("support for " + jt + " is not implemented."); } }
private void WriteValueElement(string elementName, JTokenType type) { _writer.WriteStartElement(elementName); _writer.WriteAttributeString("type", type.ToString()); }
public ValuePanel(JTokenType _type, object _value) { this.AllowDrop = true; this.HorizontalAlignment = HorizontalAlignment.Left; this.VerticalAlignment = VerticalAlignment.Stretch; this.Margin = new Thickness(JsonTreeViewItemSize.MARGIN_TEXTBOX); this.Width = JsonTreeViewItemSize.WIDTH_VALUEPANEL; this.Orientation = Orientation.Horizontal; type = _type; value = _value; switch (type) { case JTokenType.Array: break; case JTokenType.Boolean: //TextBlock tb_boolean = new TextBlock(); //tb_boolean.Text = value.ToString(); //tb_boolean.VerticalAlignment = VerticalAlignment.Center; //tb_boolean.Background = null; //CheckBox cb = new CheckBox(); //cb.IsChecked = (bool)value; //this.Children.Add(cb); //cb.Checked += delegate { tb_boolean.Text = cb.IsChecked.ToString(); this.value = cb.IsChecked; }; //cb.Unchecked += delegate { tb_boolean.Text = cb.IsChecked.ToString(); this.value = cb.IsChecked; }; //this.Children.Add(tb_boolean); ToggleSwitch ts = new ToggleSwitch(); ts.IsChecked = (bool)value; ts.Width = JsonTreeViewItemSize.WIDTH_TEXTBOX; ts.FontSize = 13; ts.OffLabel = "False"; ts.OnLabel = "True"; ts.Style = (Style)App.Current.Resources["MahApps.Metro.Styles.ToggleSwitch.Win10"]; ts.Checked += delegate { this.value = ts.IsChecked; }; ts.Unchecked += delegate { this.value = ts.IsChecked; }; this.Children.Add(ts); break; case JTokenType.Bytes: break; case JTokenType.Comment: break; case JTokenType.Constructor: break; case JTokenType.Date: break; case JTokenType.Float: break; case JTokenType.Guid: break; case JTokenType.Integer: //ValueTextBox tb_integer = new ValueTextBox(value.ToString()); ////tb_integer.Text = value.ToString(); ////tb_integer.Width = this.Width; //tb_integer.TextChanged += TextBox_TextChanged; //this.Children.Add(tb_integer); NumericUpDown tb_integer = new NumericUpDown(); tb_integer.Width = JsonTreeViewItemSize.WIDTH_TEXTBOX; //Console.WriteLine("\t\tval = " + this.value.GetType()); tb_integer.Value = (System.Int64) this.value; tb_integer.HorizontalContentAlignment = HorizontalAlignment.Left; tb_integer.ValueChanged += delegate { this.value = (int)tb_integer.Value; }; this.Children.Add(tb_integer); break; case JTokenType.None: break; case JTokenType.Null: break; case JTokenType.Object: break; case JTokenType.Property: break; case JTokenType.Raw: break; case JTokenType.String: ValueTextBox tb_string = new ValueTextBox(value.ToString()); //ValueTextBox tb_string = new ValueTextBox("\"" + value.ToString() + "\""); //tb_string.Text = "\"" + value.ToString() + "\""; tb_string.Width = JsonTreeViewItemSize.WIDTH_TEXTBOX; tb_string.TextChanged += delegate { this.value = tb_string.Text; }; this.Children.Add(tb_string); break; case JTokenType.TimeSpan: break; case JTokenType.Undefined: break; case JTokenType.Uri: break; } Label label = new Label(); label.VerticalAlignment = VerticalAlignment.Center; label.Foreground = Brushes.Gray; label.FontSize = 10; label.Content = type.ToString(); this.Children.Add(label); }
public static TypeMappingEntry Get(this ITypeMapping typeMapping, JTokenType type, ILanguage to) { return(typeMapping.Get(JsonLanguage.Instance, type.ToString(), to)); }
/// <summary> /// Converts JTokenType to a string representing the type as expected in a Manifest. /// </summary> /// <param name="type"> /// The type of the extension in the format used to deserialize Model.Json (JToken) /// </param> /// <returns> /// One of the allowed strings representing a type in a Manifest. /// </returns> private static string ConvertJTokenTypeToExpectedString(JTokenType type) { return(type.ToString().ToLower()); }
private ValidationResult ValidateSimpleValue(string path, IMetadataDefinition definition, JToken token, JTokenType expectedType) { if (token.Type == expectedType) { return(ValidationResult.Success); } if (token.Type == JTokenType.Null) { if (definition.IsRequired) { return(ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.FieldRequired, $"Bad metadata: property {path} is required.", path)); } return(ValidationResult.Success); } return(ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.UnexpectedType, $"Bad metadata: unexpected type '{token.Type.ToString()}' for property {path}, expected type '{expectedType.ToString()}'.", path)); }
private ValidationResult ValidateMultiValue(string path, IMetadataDefinition definition, JToken token, JTokenType expectedType) { if (token.Type == JTokenType.Array) { var array = (JArray)token; int index = 0; foreach (var item in array) { if (item.Type != expectedType) { return(ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.UnexpectedItemType, $"Bad metadata: unexpected type '{token.Type.ToString()}' for property {path}[{index}], expected type '{expectedType.ToString()}'.", path)); } } return(ValidationResult.Success); } else if (token.Type == JTokenType.Null) { if (definition.IsRequired) { return(ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.FieldRequired, $"Bad metadata: property {path} is required.", path)); } return(ValidationResult.Success); } return(ValidationResult.Fail(ValidationErrorCodes.WellknownMetadata.UnexpectedType, $"Bad metadata: unexpected type '{token.Type.ToString()}' for property {path}, expected type '{nameof(JTokenType.Array)}'.", path)); }
public JToken handle(ref JObject json, string name, JTokenType tokenType, ref string message) { if (json.ContainsKey(name)) { JToken jsonVarible = json.GetValue(name); if (jsonVarible.Type == tokenType) { return(jsonVarible); } else { message = "Json key -> '" + name + "' has incorrect type. Required type -> " + tokenType.ToString() + "."; } } else { message = "Json without key -> '" + name + "'."; } return(null); }