protected override JsonWriter CreateJsonWriter(TextWriter writer) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } var jsonWriter = new ClaimsJsonTextWriter(writer) { ArrayPool = pool, CloseOutput = false, AutoCompleteOnClose = false }; return(jsonWriter); }
private MemoryStream WriteData(NotifyMessageItem[] list, User user) { bool haveValue = false; var stream = new MemoryStream(); using (var streamWriter = new StreamWriter(stream, Encoding.UTF8, 80 * 1024, true)) using (var writer = new ClaimsJsonTextWriter(streamWriter) { User = user, IncludeReferences = false, IncludeReferencing = false, CloseOutput = false }) { var jsonSerializer = JsonSerializer.Create(jsonSettings); writer.WriteStartArray(); Type itemType = null; object id = null; foreach (var item in list) { if (item.Table.ItemType.Type != itemType) { if (itemType != null) { writer.WriteEndArray(); writer.WriteEndObject(); } itemType = item.Table.ItemType.Type; writer.WriteStartObject(); writer.WritePropertyName("Type"); writer.WriteValue(itemType.Name); writer.WritePropertyName("Items"); writer.WriteStartArray(); } if (!item.ItemId.Equals(id) && (item.UserId != user.Id || item.Type == DBLogType.Delete)) { id = item.ItemId; writer.WriteStartObject(); writer.WritePropertyName("Diff"); writer.WriteValue((int)item.Type); writer.WritePropertyName("User"); writer.WriteValue(item.UserId); writer.WritePropertyName("Id"); writer.WriteValue(item.ItemId.ToString()); if (item.Type != DBLogType.Delete) { var value = item.Table.LoadItemById(item.ItemId); if (value != null && (value.Access?.GetFlag(AccessType.Read, user) ?? false) && value.PrimaryId != null) { writer.WritePropertyName("Value"); jsonSerializer.Serialize(writer, value, value?.GetType()); haveValue = true; } } else { haveValue = true; } writer.WriteEndObject(); } } writer.WriteEndArray(); writer.WriteEndObject(); writer.WriteEndArray(); writer.Flush(); } if (!haveValue) { stream.Dispose(); stream = null; } else { stream.Position = 0; } return(stream); }