/// <summary> /// Creates a collection of <see cref="SearchField"/> objects corresponding to /// the properties of the type supplied. /// </summary> /// <param name="modelType"> /// The type for which fields will be created, based on its properties. /// </param> /// <param name="namingPolicy"> /// <see cref="JsonNamingPolicy"/> to use. /// Contract resolver that the SearchIndexClient will use. /// This ensures that the field names are generated in a way that is /// consistent with the way the model will be serialized. /// </param> /// <returns>A collection of fields.</returns> /// <exception cref="ArgumentNullException"><paramref name="modelType"/> or <paramref name="namingPolicy"/> is null.</exception> public static IList <SearchField> BuildForType(Type modelType, JsonNamingPolicy namingPolicy) { if (modelType is null) { throw new ArgumentNullException(nameof(modelType)); } if (namingPolicy is null) { throw new ArgumentNullException(nameof(namingPolicy)); } ArgumentException FailOnNonObjectDataType() { string errorMessage = $"Type '{modelType}' does not have properties which map to fields of an Azure Search index. Please use a " + "class or struct with public properties."; throw new ArgumentException(errorMessage, nameof(modelType)); } if (ObjectInfo.TryGet(modelType, out ObjectInfo info)) { if (info.Properties.Length == 0) { throw FailOnNonObjectDataType(); } // Use Stack to avoid a dependency on ImmutableStack for now. return(BuildForTypeRecursive(modelType, info, namingPolicy, new Stack <Type>(new[] { modelType }))); } throw FailOnNonObjectDataType(); }
/// <summary> /// create greater than or equal expression ( operator ">=" or "=ge=" ) /// </summary> /// <param name="parameter"></param> /// <param name="context"></param> /// <param name="jsonNamingPolicy"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static Expression <Func <T, bool> > GetGeExpression <T>(ParameterExpression parameter, RSqlQueryParser.ComparisonContext context, JsonNamingPolicy jsonNamingPolicy = null) { var expressionValue = GetSelector <T>(parameter, context, jsonNamingPolicy); if (!RSqlQueryGetValueHelper.IsLowerOrGreaterComparisonType(expressionValue.Property.PropertyType)) { throw new ComparisonInvalidComparatorSelectionException(context); } var value = GetUniqueValue <T>(parameter, expressionValue, context, jsonNamingPolicy); var expression = value is ExpressionValue valueExp1 ? valueExp1.Expression : Expression.Constant(value, expressionValue.Property.PropertyType); if (value is ExpressionValue valueExp2 && valueExp2.Property.PropertyType != expressionValue.Property.PropertyType) { throw new ComparisonInvalidMatchTypeException(context); } return(Expression.Lambda <Func <T, bool> >(Expression.GreaterThanOrEqual( expressionValue.Expression, expression), parameter)); }
/// <summary> /// swagger的值类型自动Required标记过滤器 /// </summary> /// <param name="camelCasePropertyNames"></param> public SwaggerRequiredSchemaFilter(bool camelCasePropertyNames) { if (camelCasePropertyNames == true) { this.camelCaseContractResolver = JsonNamingPolicy.CamelCase; } }
// Use a helper method since the method is not public. private static string ConvertToCamelCase(string name) { JsonNamingPolicy policy = JsonNamingPolicy.CamelCase; string value = policy.ConvertName(name); return(value); }
private JsonOptions(JsonSerializerDefaults defaults, JsonNamingPolicy namingPolicy = null) { SerializerSettings = new JsonSerializerOptions(defaults) { PropertyNamingPolicy = namingPolicy, Converters = { new JsonStringEnumConverter(namingPolicy), new JsonStringEnumConverter() } }; if (defaults == JsonSerializerDefaults.Web) { SerializerSettings.NumberHandling = JsonNumberHandling.AllowReadingFromString; SerializerSettings.AllowTrailingCommas = true; SerializerSettings.ReadCommentHandling = JsonCommentHandling.Skip; SerializerSettings.PropertyNameCaseInsensitive = true; SerializerSettings.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping; SerializerSettings.WriteIndented = true; SerializerSettings.IgnoreNullValues = true; SerializerSettings.DictionaryKeyPolicy = namingPolicy; SerializerSettings.IgnoreReadOnlyFields = true; SerializerSettings.IgnoreReadOnlyProperties = true; SerializerSettings.IncludeFields = true; } }
private static IList <SearchField> BuildForTypeRecursive( Type modelType, ObjectInfo info, JsonNamingPolicy namingPolicy, Stack <Type> processedTypes) { SearchField BuildField(PropertyInfo prop) {
/// <summary> /// extract the selector /// </summary> /// <param name="parameter"></param> /// <param name="context"></param> /// <param name="jsonNamingPolicy"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> private static ExpressionValue GetSelector <T>(ParameterExpression parameter, RSqlQueryParser.ComparisonContext context, JsonNamingPolicy jsonNamingPolicy) { var result = ExpressionValue.Parse <T>(parameter, context.selector().GetText(), jsonNamingPolicy); return(result ?? throw new ComparisonUnknownSelectorException(context)); }
public static string Serialize(object obj, bool indent, JsonNamingPolicy jsonNamingPolicy) { return(JsonSerializer.Serialize(obj, new JsonSerializerOptions { PropertyNamingPolicy = jsonNamingPolicy, WriteIndented = indent })); }
public ApiTaskResultDtoPropNames(JsonNamingPolicy namingPolicy) { TaskStatePropName = namingPolicy.ConvertName(nameof(ApiTaskResultDto.TaskState)); TaskTypePropName = namingPolicy.ConvertName(nameof(ApiTaskResultDto.TaskType)); IdPropName = namingPolicy.ConvertName(nameof(ApiTaskResultDto.Id)); ApiErrorPropName = namingPolicy.ConvertName(nameof(ApiTaskResultDto.ApiError)); TaskResultPropName = namingPolicy.ConvertName(nameof(ApiTaskResultDto.TaskResult)); }
/// <summary> /// Creates a collection of <see cref="SearchField"/> objects corresponding to /// the properties of the type supplied. /// </summary> /// <param name="modelType"> /// The type for which fields will be created, based on its properties. /// </param> /// <returns>A collection of fields.</returns> public static IList <SearchField> BuildForType(Type modelType) { bool useCamelCase = SerializePropertyNamesAsCamelCaseAttribute.IsDefinedOnType(modelType); JsonNamingPolicy namingPolicy = useCamelCase ? CamelCaseResolver : DefaultResolver; return(BuildForType(modelType, namingPolicy)); }
/// <summary> /// create not in expression ( operator "=out=" ) /// </summary> /// <returns></returns> public static Expression <Func <T, bool> > GetOutExpression <T>(ParameterExpression parameter, RSqlQueryParser.ComparisonContext context, JsonNamingPolicy namingStrategy = null) { var expression = GetInExpression <T>(parameter, context, namingStrategy); var body = Expression.Not(expression.Body); return(Expression.Lambda <Func <T, bool> >(body, parameter)); }
public SystemTextJsonValidationMetadataProvider(JsonNamingPolicy namingPolicy) { if (namingPolicy == null) { throw new ArgumentNullException(nameof(namingPolicy)); } _jsonNamingPolicy = namingPolicy; }
public ArangoJsonSerializer(JsonNamingPolicy policy) { Options = new JsonSerializerOptions(JsonSerializerDefaults.Web) { PropertyNamingPolicy = policy, DictionaryKeyPolicy = policy, IgnoreNullValues = false }; }
public static JsonSerializerOptions GetSerializerOptions(JsonNamingPolicy jsonNamingPolicy, bool ignoreNullValues = true) => new JsonSerializerOptions { IgnoreNullValues = ignoreNullValues, PropertyNameCaseInsensitive = true, PropertyNamingPolicy = jsonNamingPolicy, DictionaryKeyPolicy = jsonNamingPolicy, WriteIndented = true };
private JitILCompiler(Type type, JsonSerializerOptions options, TypeBuilder tb) { _rootType = type; _options = options; _dictionaryKeyPolicy = options.DictionaryKeyPolicy; _tb = tb; _followUp = null; _writeStack = new List <FieldBuilder>(); _converterCounter = -1; _converterCache = new Dictionary <JsonConverter, FieldBuilder>(); _propertyNameCounter = -1; _propertyNameCache = new Dictionary <string, FieldBuilder>(); _enumerableContextFields = new List <FieldBuilder>(); // Don't care in constructor _ilg = null; _generatingChunkMethod = false; _jumpTable = null; _jumpTableCount = 0; _enumerableContextFieldCount = 0; // Define a static field for JsonSerializerOptions string optionsFieldName = @"Options"; _optionsField = _tb.DefineField( fieldName: optionsFieldName, type: typeof(JsonSerializerOptions), attributes: FieldAttributes.Public | FieldAttributes.Static ); _followUp += type => { FieldInfo field = type.GetField(optionsFieldName); field.SetValue(null, options); }; // Define a static field for DictionaryKeyPolicy string dictionaryKeyPolicyFieldName = @"DictionaryKeyPolicy"; _dictionaryKeyPolicyField = _tb.DefineField( fieldName: dictionaryKeyPolicyFieldName, type: typeof(JsonNamingPolicy), attributes: FieldAttributes.Public | FieldAttributes.Static ); _followUp += type => { FieldInfo field = type.GetField(dictionaryKeyPolicyFieldName); field.SetValue(null, options.DictionaryKeyPolicy); }; // Define a field for jump table progress _jumpTableProgressField = _tb.DefineField( fieldName: @"_progress", type: typeof(int), attributes: FieldAttributes.Private ); }
public static JsonSerializerOptions GetOptions(JsonNamingPolicy jsonNamingPolicy) { return(new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = jsonNamingPolicy, IgnoreNullValues = true, AllowTrailingCommas = true }); }
public static object GetTemporalValue <T>(ParameterExpression parameter, ExpressionValue expressionValue, RSqlQueryParser.ComparisonContext context, JsonNamingPolicy jsonNamingPolicy = null) { if (IsDateTimeOffset(expressionValue.Property.PropertyType)) { return(GetDateTimeOffset <T>(parameter, context.arguments().value()[0], jsonNamingPolicy)); } return(GetDateTime <T>(parameter, context.arguments().value()[0], jsonNamingPolicy)); }
/// <summary>Initializes a new instance of the <see cref="OasXMsEnumExtension" /> class.</summary> /// <param name="enumType">Type of the enum.</param> /// <param name="commentDocs">The comment docs.</param> /// <param name="namingPolicy">The naming policy.</param> /// <param name="enumModeling">The enum modeling.</param> public OasXMsEnumExtension( Type enumType, IList <XDocument> commentDocs, JsonNamingPolicy namingPolicy, OasEnumModeling enumModeling = OasEnumModeling.AsString) { this.enumType = enumType; this.commentDocs = commentDocs; this.enumModeling = enumModeling; this.namingPolicy = namingPolicy; }
public static object GetAlphabeticValue <T>(ParameterExpression parameter, ExpressionValue expressionValue, RSqlQueryParser.ComparisonContext context, JsonNamingPolicy jsonNamingPolicy = null) { if (IsChar(expressionValue.Property.PropertyType)) { return(GetChar <T>(parameter, context.arguments().value()[0], jsonNamingPolicy)); } return(GetString <T>(parameter, context.arguments().value()[0], jsonNamingPolicy)); }
/// <summary> /// Initializes a new instance of the <see cref="StringEnumListConverter{TEnum}"/> class. /// </summary> /// <param name="namingPolicy">The naming policy to be used to translate JSON values into enum values, and vice-versa.</param> public StringEnumListConverter(JsonNamingPolicy namingPolicy) { _enumValuesByJsonValue = new(); _jsonValuesByEnumValue = new(); foreach (var enumValue in Enum.GetValues(typeof(TEnum)).Cast <TEnum>()) { var jsonValue = namingPolicy.ConvertName(enumValue.ToString()); _enumValuesByJsonValue.Add(jsonValue, enumValue); _jsonValuesByEnumValue.Add(enumValue, jsonValue); } }
/// <summary> /// extract the unique value /// </summary> /// <param name="parameter"></param> /// <param name="expressionValue"></param> /// <param name="context"></param> /// <param name="jsonNamingPolicy"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="ComparisonNotEnoughArgumentException"></exception> /// <exception cref="ComparisonTooManyArgumentException"></exception> private static object GetUniqueValue <T>(ParameterExpression parameter, ExpressionValue expressionValue, RSqlQueryParser.ComparisonContext context, JsonNamingPolicy jsonNamingPolicy = null) { var value = context.arguments().value(); if (value.Length > 1) { throw new ComparisonTooManyArgumentException(context); } return(RSqlQueryGetValueHelper.GetValue <T>(parameter, expressionValue, context, jsonNamingPolicy)); }
public JsonValueConverterEnumWithAttributeSupport(EnumConverterOptions options, [CanBeNull] JsonNamingPolicy namingPolicy = null) { _converterOptions = options; if (namingPolicy != null) { _nameCache = new ConcurrentDictionary <string, string>(); } else { namingPolicy = new JsonDefaultNamingPolicy(); } _namingPolicy = namingPolicy; }
public EnumConverter(EnumConverterOptions options, JsonNamingPolicy?namingPolicy) { _converterOptions = options; if (namingPolicy != null) { _nameCache = new ConcurrentDictionary <string, string>(); } else { namingPolicy = JsonNamingPolicy.Default; } _namingPolicy = namingPolicy; }
public JsonRpcSwaggerProvider( ISchemaGenerator schemaGenerator, IXmlDocumentationService xmlDocumentationService, IOptions <SwaggerConfiguration> swaggerOptions, IServiceScopeFactory scopeFactory ) { this.schemaGenerator = schemaGenerator; this.swagerOptions = swaggerOptions.Value; this.namePolicy = swaggerOptions.Value.NamingPolicy; this.scopeFactory = scopeFactory; this.xmlDocumentationService = xmlDocumentationService; }
public JsonRpcSwaggerProvider( ISchemaGenerator schemaGenerator, IRpcMethodProvider methodProvider, IXmlDocumentationService xmlDocumentationService, IOptions <SwaggerConfiguration> swaggerOptions ) { this.schemaGenerator = schemaGenerator; this.swagerOptions = swaggerOptions.Value; this.namePolicy = swaggerOptions.Value.NamingPolicy; this.methodProvider = methodProvider; this.xmlDocumentationService = xmlDocumentationService; }
private static string GetJsonPropertyName(MemberInfo propertyInfo, JsonNamingPolicy jsonNamingPolicy = null) { var propertyName = propertyInfo.Name; var attribute = propertyInfo.GetCustomAttribute <JsonPropertyAttribute>(); if (attribute != null) { propertyName = attribute.PropertyName; } return(jsonNamingPolicy == null ? propertyName : jsonNamingPolicy.ConvertName(propertyName)); }
/// <summary> /// create in expression ( operator "=in=" ) /// </summary> /// <returns></returns> public static Expression <Func <T, bool> > GetInExpression <T>(ParameterExpression parameter, RSqlQueryParser.ComparisonContext context, JsonNamingPolicy jsonNamingPolicy = null) { var expressionValue = GetSelector <T>(parameter, context, jsonNamingPolicy); var values = GetMultipleValue(expressionValue, context); var methodContainsInfo = QueryReflectionHelper.GetOrRegistryContainsMethodInfo(expressionValue.Property.PropertyType); return(Expression.Lambda <Func <T, bool> >( Expression.Call(Expression.Constant(methodContainsInfo.Convert(values)), methodContainsInfo.ContainsMethod, expressionValue.Expression), parameter)); }
private static string GetColumnName(PropertyInfo propertyInfo, JsonNamingPolicy namingPolicy) { var attribute = propertyInfo.GetCustomAttribute <ColumnNameAttribute>(); if (attribute != null) { return(attribute.Name); } if (namingPolicy != null) { return(namingPolicy.ConvertName(propertyInfo.Name)); } return(propertyInfo.Name); }
private static object GetBoolean <T>(ParameterExpression parameter, RSqlQueryParser.ValueContext valueContext, JsonNamingPolicy jsonNamingPolicy = null) { if (bool.TryParse(valueContext.GetText(), out var result)) { return(result); } if (ExpressionValue.TryParse <T>(parameter, valueContext.GetText(), jsonNamingPolicy, out var value)) { return(value); } throw new InvalidConversionException(valueContext, typeof(bool)); }
public static bool TryParse <T>(ParameterExpression parameter, string selector, JsonNamingPolicy jsonNamingPolicy, out ExpressionValue result ) { result = null; try { result = Parse <T>(parameter, selector, jsonNamingPolicy); return(result != null); } catch { return(false); } }