static NServiceBusXmlMessageSerializer() { XmlSerializer = new Lazy <JsonSerializer>(() => JsonSerializer.Create(XmlSerializerSettings)); ByteArrayConverter = new ByteArrayConverter(); MessageDataJsonConverter = new MessageDataJsonConverter(); StringDecimalConverter = new StringDecimalConverter(); DefaultContractResolver serializerContractResolver = new JsonContractResolver(ByteArrayConverter, MessageDataJsonConverter, StringDecimalConverter) { NamingStrategy = new DefaultNamingStrategy() }; SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, ObjectCreationHandling = ObjectCreationHandling.Auto, ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, ContractResolver = serializerContractResolver, TypeNameHandling = TypeNameHandling.None, DateParseHandling = DateParseHandling.None, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind }; _serializer = new Lazy <JsonSerializer>(() => JsonSerializer.Create(SerializerSettings)); }
/// <summary> /// 触发评论事件 /// </summary> /// <param name="notification"></param> public void Handler(ReviewWhiperEvent reviewEvent) { try { Tidings tidings = null; string url = "../whisper/whisper.html?id=" + reviewEvent.WhiperId + "&read=1"; if (reviewEvent.Comment.CommentType == CommentType.微语) { Whisper whisper = _whisperRepository.SelectById(reviewEvent.WhiperId); tidings = new Tidings(reviewEvent.Comment.Guid, reviewEvent.Comment.PostUser, reviewEvent.Comment.Content, whisper.Account, false, url, whisper.Content, DateTime.Now); } else//回复评论 { Comment comment = _commentRepository.SelectById(reviewEvent.Comment.AdditionalData);//被评论的数据; tidings = new Tidings(reviewEvent.Comment.Guid, reviewEvent.Comment.PostUser, reviewEvent.Comment.Content , comment.PostUser, false, url, comment.Content, DateTime.Now); } _tidingsRepository.Insert(tidings); JsonSerializerSettings jsonSerializerSettings = new JsonContractResolver().SetJsonSerializerSettings(); List <Whisper> whispers = _cacheClient.ListRange <Whisper>(ConstantKey.CACHE_SQUARE_WHISPER, 0, 5, jsonSerializerSettings).GetAwaiter().GetResult(); IList <Comment> comments = _commentRepository.SelectByIds(reviewEvent.WhisperCommentGuids); int index = 0; Whisper cacheWhisper = null; for (int i = 0; i < whispers.Count; i++) { if (whispers[i].Id != reviewEvent.WhiperId) { continue; } index = i; cacheWhisper = new Whisper( whispers[i].Id, whispers[i].Account, whispers[i].AccountName, whispers[i].Content, whispers[i].IsPassing, string.Join(",", comments.Select(s => s.Guid)), comments, Convert.ToDateTime(whispers[i].CreateDate)); whispers[i] = cacheWhisper; } if (cacheWhisper == null) { return; } _cacheClient.ListInsert(ConstantKey.CACHE_SQUARE_WHISPER, index, cacheWhisper); int count = _tidingsRepository.SelectCountByAccount(reviewEvent.Comment.RevicerUser); Message message = new Message(); message.Data = count; _singalrContent.SendClientMessage(reviewEvent.Comment.RevicerUser, message); //首页微语 message.Data = whispers; _singalrContent.SendAllClientsMessage(message); } catch (AggregateException ex) { new LogUtils().LogError(ex, "Blog.Domain.ReviewWhisperEventHandler", ex.Message, reviewEvent.Comment.PostUser); } }
public void Handler(CreateWhisperCommand command) { int id = _whisperRepository.Insert(command.Whisper); User user = _userRepository.SelectUserByAccount(command.Whisper.Account); Whisper whisper = new Whisper( id, command.Whisper.Account, user.Username, command.Whisper.Content, command.Whisper.IsPassing, command.Whisper.CommentGuids, DateTime.Now); long length = _cacheClient.AddListTop(ConstantKey.CACHE_SQUARE_WHISPER, whisper).Result; int listLength = Convert.ToInt32(length); if (listLength > 12) { _cacheClient.listPop(ConstantKey.CACHE_SQUARE_WHISPER); } JsonSerializerSettings jsonSerializerSettings = new JsonContractResolver().SetJsonSerializerSettings(); List <Whisper> whispers = _cacheClient.ListRange <Whisper>(ConstantKey.CACHE_SQUARE_WHISPER, 0, 6, jsonSerializerSettings).GetAwaiter().GetResult(); Message message = new Message(); message.Data = whispers; _singalrContent.SendAllClientsMessage(message); }
public JsonSettings() { m_contractResolver = new JsonContractResolver(); m_contractFactory = new Func <Type, JsonContract>(CreateObjectContract); Converters.Add(new VersionConverter()); }
private void EnsureSerializer(OutputFormatterCanWriteContext context) { if (context.HttpContext.Items[Constants.ContextKeys.JsonMultiCase] is ITextTransform transform) { if (!_resolvers.TryGetValue(transform, out var resolver)) { resolver = new JsonContractResolver(transform, JsonProcessingDirection.Output); _resolvers.Add(transform, resolver); } _serializer.ContractResolver = resolver; } if (context.HttpContext.Items[Constants.ContextKeys.JsonPrettyPrint] is bool prettyPrint && !prettyPrint) { _serializer.Apply(s => s.Formatting = Formatting.None); } if (context.HttpContext.Items[Constants.ContextKeys.JsonTrim] is bool trim && trim) { _serializer.Apply(s => { s.NullValueHandling = NullValueHandling.Ignore; s.DefaultValueHandling = DefaultValueHandling.Ignore; }); } }
public IList <News> Select() { string sql = "SELECT config_value FROM SYS_Config where config_key=@key"; string value = DbConnection.QueryFirst <string>(sql, new { key = "news" }); JsonSerializerSettings jsonSerializerSettings = new JsonContractResolver().SetJsonSerializerSettings(); IList <News> newsList = JsonHelper.DeserializeObject <IList <News> >(value, jsonSerializerSettings); return(newsList); }
public HttpJsonResponseMessage(object data, Formatting formatting, JsonContractResolver contractResolver) { JsonSerializerSettings settings = new JsonSerializerSettings { ContractResolver = contractResolver }; string content = JsonConvert.SerializeObject(data, formatting, settings); Content = new StringContent(content, Encoding.UTF8, MediaTypes.Json); }
private void EnsureSerializer(InputFormatterContext context) { if (context.HttpContext.Items[Constants.ContextKeys.JsonMultiCase] is ITextTransform transform) { if (!_resolvers.TryGetValue(transform, out var resolver)) { resolver = new JsonContractResolver(transform, JsonProcessingDirection.Input); _resolvers.Add(transform, resolver); } _serializer.ContractResolver = resolver; } }
public async Task <IList <WhisperModel> > SelectByPageCache(int pageIndex, int pageSize) { JsonSerializerSettings jsonSerializerSettings = new JsonContractResolver().SetJsonSerializerSettings(); IList <Whisper> whispers = await _cacheClient.ListRange <Whisper>(ConstantKey.CACHE_SQUARE_WHISPER, pageIndex, pageSize, jsonSerializerSettings); if (whispers.Count == 0) { whispers = _whisperRepository.SelectByPage(1, 12).ToList(); foreach (var item in whispers) { await _cacheClient.AddListTail(ConstantKey.CACHE_SQUARE_WHISPER, item); } whispers = whispers.Take(6).ToList(); } return(ConvertToModel(whispers)); }
/// <summary> /// 操作缓存 /// </summary> /// <param name="invocation"></param> private void ProgressCaching(IInvocation invocation, CacheAttribute mapCacheAttribute) { var typeName = invocation.TargetType.Name; var methodName = invocation.Method.Name; Type methodReturnType = invocation.Method.ReturnType; var methodArguments = ParseArgumentsToPartOfCacheKey(invocation.Arguments); var cacheKey = BuildCacheKey(typeName, methodName, methodArguments, mapCacheAttribute.prefixs); var cacheValue = _cacheClient.StringGet(cacheKey); if (cacheValue != null) { //基元类型和string类型不需要序列化 if (methodReturnType.IsPrimitive || methodReturnType.Name == "String") { invocation.ReturnValue = cacheKey; } else { var contractResolver = new JsonContractResolver(); JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() { ContractResolver = contractResolver }; invocation.ReturnValue = JsonConvert.DeserializeObject(cacheValue, methodReturnType, jsonSerializerSettings); } return; } invocation.Proceed(); if (!string.IsNullOrWhiteSpace(cacheKey)) { _cacheClient.StringSet(cacheKey, invocation.ReturnValue); } }