コード例 #1
0
        /// <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();
        }
コード例 #2
0
        /// <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));
        }
コード例 #3
0
 /// <summary>
 /// swagger的值类型自动Required标记过滤器
 /// </summary>
 /// <param name="camelCasePropertyNames"></param>
 public SwaggerRequiredSchemaFilter(bool camelCasePropertyNames)
 {
     if (camelCasePropertyNames == true)
     {
         this.camelCaseContractResolver = JsonNamingPolicy.CamelCase;
     }
 }
コード例 #4
0
        // 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);
        }
コード例 #5
0
        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;
            }
        }
コード例 #6
0
 private static IList <SearchField> BuildForTypeRecursive(
     Type modelType,
     ObjectInfo info,
     JsonNamingPolicy namingPolicy,
     Stack <Type> processedTypes)
 {
     SearchField BuildField(PropertyInfo prop)
     {
コード例 #7
0
        /// <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));
        }
コード例 #8
0
 public static string Serialize(object obj, bool indent, JsonNamingPolicy jsonNamingPolicy)
 {
     return(JsonSerializer.Serialize(obj, new JsonSerializerOptions
     {
         PropertyNamingPolicy = jsonNamingPolicy,
         WriteIndented = indent
     }));
 }
コード例 #9
0
 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));
 }
コード例 #10
0
        /// <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));
        }
コード例 #11
0
        /// <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));
        }
コード例 #12
0
    public SystemTextJsonValidationMetadataProvider(JsonNamingPolicy namingPolicy)
    {
        if (namingPolicy == null)
        {
            throw new ArgumentNullException(nameof(namingPolicy));
        }

        _jsonNamingPolicy = namingPolicy;
    }
コード例 #13
0
 public ArangoJsonSerializer(JsonNamingPolicy policy)
 {
     Options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
     {
         PropertyNamingPolicy = policy,
         DictionaryKeyPolicy  = policy,
         IgnoreNullValues     = false
     };
 }
コード例 #14
0
 public static JsonSerializerOptions GetSerializerOptions(JsonNamingPolicy jsonNamingPolicy, bool ignoreNullValues = true)
 => new JsonSerializerOptions
 {
     IgnoreNullValues            = ignoreNullValues,
     PropertyNameCaseInsensitive = true,
     PropertyNamingPolicy        = jsonNamingPolicy,
     DictionaryKeyPolicy         = jsonNamingPolicy,
     WriteIndented = true
 };
コード例 #15
0
        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
                );
        }
コード例 #16
0
 public static JsonSerializerOptions GetOptions(JsonNamingPolicy jsonNamingPolicy)
 {
     return(new JsonSerializerOptions
     {
         PropertyNameCaseInsensitive = true,
         PropertyNamingPolicy = jsonNamingPolicy,
         IgnoreNullValues = true,
         AllowTrailingCommas = true
     });
 }
コード例 #17
0
        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));
        }
コード例 #18
0
 /// <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;
 }
コード例 #19
0
        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));
        }
コード例 #20
0
        /// <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);
            }
        }
コード例 #21
0
        /// <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));
        }
コード例 #22
0
 public JsonValueConverterEnumWithAttributeSupport(EnumConverterOptions options, [CanBeNull] JsonNamingPolicy namingPolicy = null)
 {
     _converterOptions = options;
     if (namingPolicy != null)
     {
         _nameCache = new ConcurrentDictionary <string, string>();
     }
     else
     {
         namingPolicy = new JsonDefaultNamingPolicy();
     }
     _namingPolicy = namingPolicy;
 }
コード例 #23
0
ファイル: EnumConverter.cs プロジェクト: sod-21/runtime
 public EnumConverter(EnumConverterOptions options, JsonNamingPolicy?namingPolicy)
 {
     _converterOptions = options;
     if (namingPolicy != null)
     {
         _nameCache = new ConcurrentDictionary <string, string>();
     }
     else
     {
         namingPolicy = JsonNamingPolicy.Default;
     }
     _namingPolicy = namingPolicy;
 }
コード例 #24
0
 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;
 }
コード例 #25
0
 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;
 }
コード例 #26
0
        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));
        }
コード例 #27
0
        /// <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));
        }
コード例 #28
0
ファイル: MetadataHelper.cs プロジェクト: koralium/Koralium
        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);
        }
コード例 #29
0
        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));
        }
コード例 #30
0
 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);
     }
 }