Ejemplo n.º 1
0
        public Task ExportJson(IJsonSerializerProvider provider, string name = "")
        {
            var dialog = new SaveFileDialog
            {
                Title           = "Save Json export",
                Filter          = "Json File|*.json",
                FileName        = "export.json",
                OverwritePrompt = true
            };

            if (!string.IsNullOrEmpty(name))
            {
                if (!name.EndsWith(".json"))
                {
                    name += ".json";
                }

                dialog.FileName = $"{name}";
            }

            if (dialog.ShowDialog() == true)
            {
                using (var writer = new StreamWriter(dialog.FileName))
                {
                    provider.Serialize(writer, true);
                }
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 返回所有脱敏词汇
        /// </summary>
        /// <returns></returns>
        public async Task <IEnumerable <string> > GetWordsAsync()
        {
            // 读取缓存数据
            var wordsCached = await _distributedCache.GetStringAsync(DISTRIBUTED_KEY);

            if (wordsCached != null)
            {
                return(_jsonSerializerProvider.Deserialize <IEnumerable <string> >(wordsCached));
            }

            // 读取文件内容
            byte[] buffer;
            using (var readStream = _fileProvider.GetFileInfo("sensitive-words.txt").CreateReadStream()) // 暂时不提供配置文件名称
            {
                buffer = new byte[readStream.Length];
                await readStream.ReadAsync(buffer.AsMemory(0, buffer.Length));
            }

            var content = Encoding.UTF8.GetString(buffer);

            // 取换行符分割字符串
            var words = content.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            // 缓存数据
            await _distributedCache.SetStringAsync(DISTRIBUTED_KEY, _jsonSerializerProvider.Serialize(words));

            return(words);
        }
Ejemplo n.º 3
0
        public async Task <Maybe <string> > ExportToJson(IJsonSerializerProvider provider, string fileName = "")
        {
            var exportFileName = "export.json";

            if (!string.IsNullOrEmpty(fileName))
            {
                exportFileName = ArchiveExtensions.EnsureFileNameExtension(fileName, ".json");
            }

            var maybeFileName = await _applicationInteraction.ShowSaveFileDialog("Save Json export", "Json File|*.json", exportFileName);

            if (maybeFileName.HasValue)
            {
                using (var writer = new StreamWriter(maybeFileName.Value))
                {
                    provider.Serialize(writer, true);
                }
            }

            return(maybeFileName);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 实现过滤器方法
        /// </summary>
        /// <param name="model"></param>
        /// <param name="context"></param>
        public void Apply(OpenApiSchema model, SchemaFilterContext context)
        {
            var type = context.Type;

            // 排除其他程序集的枚举
            if (type.IsEnum && App.Assemblies.Contains(type.Assembly))
            {
                model.Enum.Clear();
                var stringBuilder = new StringBuilder();
                stringBuilder.Append($"{model.Description}<br />");

                var enumValues = Enum.GetValues(type);
                foreach (var value in enumValues)
                {
                    // 获取枚举成员特性
                    var fieldinfo            = type.GetField(Enum.GetName(type, value));
                    var descriptionAttribute = fieldinfo.GetCustomAttribute <DescriptionAttribute>(true);
                    model.Enum.Add(OpenApiAnyFactory.CreateFromJson(_serializerProvider.Serialize(value)));

                    stringBuilder.Append($"&nbsp;{descriptionAttribute?.Description} {value} = {value}<br />");
                }
                model.Description = stringBuilder.ToString();
            }
        }
Ejemplo n.º 5
0
 public static string Serialize(object obj, string datetimeFormat = null)
 {
     CheckJsonProviderNotNull();
     return(staticJsonSerializerProvider.Serialize(obj, datetimeFormat));
 }