public IActionResult Download() { // Read cache var cacheKey = "bookings"; _memoryCache.TryGetValue(cacheKey, out bookings); // Write enum content as string var stringEnumConverter = new JsonStringEnumConverter(); JsonSerializerOptions opts = new JsonSerializerOptions() { WriteIndented = true }; opts.Converters.Add(stringEnumConverter); // Serialize var data = JsonSerializer.Serialize(bookings, opts); byte[] bytes = Encoding.UTF8.GetBytes(data); // Export var output = new FileContentResult(bytes, "application/octet-stream"); output.FileDownloadName = "bookings.json"; return(output); }
public DataController(IMemoryCache memoryCache) { this.memoryCache = memoryCache; var Enumtranslation = new JsonStringEnumConverter(JsonNamingPolicy.CamelCase); opt.Converters.Add(Enumtranslation); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services .AddControllers() .AddJsonOptions(options => { var enumConverter = new JsonStringEnumConverter(); options.JsonSerializerOptions.Converters.Add(enumConverter); }); }
public EnumNamingPolicyCache(JsonNamingPolicy?defaultWriteNamingPolicy, bool allowIntegerValues) { var enumType = typeof(T); var enumNameCache = new EnumNameCache(enumType, defaultWriteNamingPolicy); ReadNamingPolicy = new EnumReadNamingPolicy(enumNameCache); var writeNamingPolicy = new EnumWriteNamingPolicy(enumNameCache, defaultWriteNamingPolicy); var defaultJsonConverterFactory = new JsonStringEnumConverter(namingPolicy: writeNamingPolicy, allowIntegerValues: allowIntegerValues); DefaultJsonConverter = (JsonConverter <T>)defaultJsonConverterFactory.CreateConverter(typeToConvert: enumType, options: null); }
#pragma warning disable CA1810 // Initialize reference type static fields inline static SchemaSerializer() #pragma warning restore CA1810 // Initialize reference type static fields inline { var stringEnumConverter = new JsonStringEnumConverter(); DefaultSerializationSettings = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }; DefaultSerializationSettings.Converters.Add(stringEnumConverter); HtmlEscapedSerializationSettings = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, }; HtmlEscapedSerializationSettings.Converters.Add(stringEnumConverter); }
public void ConfigureServices(IServiceCollection services) { SwashbuckleStartup.Init(services, Configuration); DiStartup.Init(services, Configuration); AuthStartup.Init(services, Configuration); CorsStartup.Init(services, Configuration); services .AddControllers() .AddJsonOptions(jsonOptions => { var jsonStringEnumConverter = new JsonStringEnumConverter(); jsonOptions.JsonSerializerOptions.Converters.Add(jsonStringEnumConverter); }); }
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { var query = from field in typeToConvert.GetFields(BindingFlags.Public | BindingFlags.Static) let attr = field.GetCustomAttribute <EnumMemberAttribute>() where attr != null select(field.Name, attr.Value); var dictionary = query.ToDictionary((p) => p.Name, (p) => p.Value); if (dictionary.Count > 0) { var namingPolicy = new DictionaryLookupNamingPolicy(dictionary, _namingPolicy); var converter = new JsonStringEnumConverter(namingPolicy, _allowIntegerValues).CreateConverter(typeToConvert, options); return(converter); } return(_baseConverter.CreateConverter(typeToConvert, options)); }
// This method gets called by the runtime. Use this method to add services to the container. public virtual void ConfigureServices(IServiceCollection services) { services.AddCorsPolicies(Configuration); services.AddBaseConfiguration(Configuration); var metricsOptions = Configuration.GetSection(nameof(MetricsOptions)).Get <MetricsOptions>(); if (metricsOptions.Enabled) { services.AddMetrics(); } services.ConfigureIdentityServices(); services.AddMvc().AddJsonOptions(options => { var converter = new JsonStringEnumConverter(); options.JsonSerializerOptions.Converters.Add(converter); }); services.ConfigureSwagger(); services.RegisterBaseWorkers(Configuration); HealthCheckBuilder = services.ConfigureHealthChecks(Configuration); }
public FileContentResult export(List <EvaluationExportImportViewModel> list) { // Write enum content as string var stringEnumConverter = new JsonStringEnumConverter(); JsonSerializerOptions opts = new JsonSerializerOptions() { WriteIndented = true }; opts.Converters.Add(stringEnumConverter); // Serialize var data = JsonSerializer.Serialize(list, opts); byte[] bytes = Encoding.UTF8.GetBytes(data); // Export var output = new FileContentResult(bytes, "application/octet-stream"); output.FileDownloadName = "Evaluation.json"; return(output); }
/// <summary> /// Creates a new instance /// </summary> public JsonCamelCaseStringEnumConverter() { _converter = new JsonStringEnumConverter(JsonNamingPolicy.CamelCase); }
public JsonEnumMemberConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true) { _namingPolicy = namingPolicy; _allowIntegerValues = allowIntegerValues; _baseConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); }
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { var converterFactory = new JsonStringEnumConverter(new SnakeCaseNamingPolicy(), false); return(converterFactory.CreateConverter(typeToConvert, options)); }
public async void JSONSchemaValidation() { var a = new JSchemaGenerator(); var schemaObject = a.Generate(typeof(TaskModel)); var linkParser = new PropertyParsingRuleModel { NodeSelector = new SelectorModel { Type = SelectorEnum.XPath, MatchExpression = "(.//a[contains(@class,'ellipsis-text')])[1]" }, Type = OutputTypeEnum.Text, OutputFrom = OutputFromEnum.Attribute, OutputFromAttributeName = "href", }; var nameParser = new PropertyParsingRuleModel { NodeSelector = new SelectorModel { Type = SelectorEnum.XPath, MatchExpression = "(.//div[contains(@class,'pi-img-wrapper')])[1]/a[1]" }, Type = OutputTypeEnum.Text, OutputFrom = OutputFromEnum.Attribute, OutputFromAttributeName = "name", }; var priceParser = new PropertyParsingRuleModel { NodeSelector = new SelectorModel { Type = SelectorEnum.XPath, MatchExpression = "(.//div[contains(@class,'pi-price')])[1]" }, Type = OutputTypeEnum.Text, OutputFrom = OutputFromEnum.InnerText }; var productParser = new PropertyParsingRuleModel { Type = OutputTypeEnum.Array, NodeSelector = new SelectorModel { Type = SelectorEnum.CSS, MatchExpression = "div.product-list div.product-item" }, PropertyParsingRules = new Dictionary <string, PropertyParsingRuleModel> { { "Link", linkParser }, { "Name", nameParser }, { "Price", priceParser } } }; var taskModel = new TaskModel { Uri = "http://www.exdoll.com/productlist.ac", RequestMethod = RequestMethodEnum.Get, TaskId = 0, RequestParameter = new RequestParameterModel { Headers = new Dictionary <string, string> { { "cookie", "JSESSIONID=912BD825760319675E9DE1E1C1E2D701" } }, Body = null }, PropertyParsingRules = new Dictionary <string, PropertyParsingRuleModel> { { "Products", productParser } }, }; var stringEnumConverter = new JsonStringEnumConverter(); JsonSerializerOptions opts = new JsonSerializerOptions(); opts.IgnoreNullValues = true; opts.WriteIndented = true; //opts.Converters.Add(stringEnumConverter); var taskString = JsonSerializer.Serialize(taskModel); JObject taskModel1 = JObject.Parse(taskString); JSchema schema = JSchema.Parse(schemaObject.ToString()); var valid = taskModel1.IsValid(schema); }
public async void ParserTest() { /* * 构思 * 请求引擎:默认,WebDriver * 内容类型:Text,JSON,HTML * 内容字符集:UTF8,ASNII -restSharp有 不需要, * * 使用QuartZ.NET启动一个Job 定时 批量 拉取MQ中的Task * 拉取后要将该Task状态置为执行中,如果达到超时阈值没有执行完毕,该状态重置 * 在执行完毕后,将该Task的Response丢入MQ中并将该Task设置为执行完毕 * */ var linkParser = new PropertyParsingRuleModel { NodeSelector = new SelectorModel { Type = SelectorEnum.XPath, MatchExpression = "(.//a[contains(@class,'ellipsis-text')])[1]" }, Type = OutputTypeEnum.Text, OutputFrom = OutputFromEnum.Attribute, OutputFromAttributeName = "href", }; var nameParser = new PropertyParsingRuleModel { NodeSelector = new SelectorModel { Type = SelectorEnum.XPath, MatchExpression = "(.//div[contains(@class,'pi-img-wrapper')])[1]/a[1]" }, Type = OutputTypeEnum.Text, OutputFrom = OutputFromEnum.Attribute, OutputFromAttributeName = "name", }; var priceParser = new PropertyParsingRuleModel { NodeSelector = new SelectorModel { Type = SelectorEnum.XPath, MatchExpression = "(.//div[contains(@class,'pi-price')])[1]" }, Type = OutputTypeEnum.Text, OutputFrom = OutputFromEnum.InnerText }; var productParser = new PropertyParsingRuleModel { Type = OutputTypeEnum.Array, NodeSelector = new SelectorModel { Type = SelectorEnum.CSS, MatchExpression = "div.product-list div.product-item" }, PropertyParsingRules = new Dictionary <string, PropertyParsingRuleModel> { { "Link", linkParser }, { "Name", nameParser }, { "Price", priceParser } } }; var taskModel = new TaskModel { Uri = "http://www.exdoll.com/productlist.ac", RequestMethod = RequestMethodEnum.Get, TaskId = 0, RequestParameter = new RequestParameterModel { Headers = new Dictionary <string, string> { { "cookie", "JSESSIONID=912BD825760319675E9DE1E1C1E2D701" } }, Body = null }, PropertyParsingRules = new Dictionary <string, PropertyParsingRuleModel> { { "Products", productParser } }, }; var stringEnumConverter = new JsonStringEnumConverter(); JsonSerializerOptions opts = new JsonSerializerOptions(); opts.IgnoreNullValues = true; opts.WriteIndented = true; //opts.Converters.Add(stringEnumConverter); var txt = JsonSerializer.Serialize(taskModel, opts); //IRestClient restClient = new RestClient(taskModel.Uri); //IRestRequest request = new RestRequest(Method.GET); //request.AddCookie("JSESSIONID", "4173BE2521D676127C3F9C3F8EA68F67"); //IRestResponse response = await restClient.ExecuteGetAsync(request); var response = await DownloadData(taskModel); var contentText = response.Content; HtmlDocument document = new HtmlDocument(); document.LoadHtml(contentText); var rootNode = document.DocumentNode; var result = await Parse(rootNode, taskModel.PropertyParsingRules.First().Value); var resultT = new { TaskId = taskModel.TaskId, Result = result, ResponseHeaders = response.Headers.ToDictionary(c => c.Name, c => c?.Value?.ToString()), ResponseCookies = response.Cookies.ToDictionary(c => c.Name, c => c.Value) }; var aaa = JsonSerializer.Serialize(resultT, opts); }
public AspireJsonStringEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true) { this.namingPolicy = namingPolicy; this.allowIntegerValues = allowIntegerValues; this.baseConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); }
public CustomJsonStringEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true) { this._namingPolicy = namingPolicy; this._allowIntegerValues = allowIntegerValues; this._baseConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); }
public AbpStringToEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true) { _innerJsonStringEnumConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); }
/// <summary> /// Creates a new instance of a JsonEnumValueConverter, initializes with specified arguments. /// </summary> /// <param name="namingPolicy"></param> /// <param name="allowIntegerValues"></param> public JsonEnumValueConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true) { _converter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); }
public KebabCaseEnumConverter(bool allowIntegerValues = true) { this.allowIntegerValues = allowIntegerValues; this.baseConverter = new JsonStringEnumConverter(null, allowIntegerValues); }