Example #1
0
        protected async Task WriteJsonAsync(JsonTextWriter writer, ISelection selection, IExpressionTree fields)
        {
            var countField = fields.Children.FirstOrDefault(c => c.Node.Name == nameof(ISelection.Count) && typeof(ISelection).IsAssignableFrom(c.Node.ToLambdaExpression().Parameters[0].Type));

            var normalFields = new ExpressionTree
            {
                Node     = fields.Node,
                Children = fields.Children.Where(c => c != countField).ToList()
            };

            await writer.WriteStartObjectAsync();

            if (countField == null || normalFields.Children.Count != 0)
            {
                var items = selection.GetItems();
                await WriteKvpAsync(writer, NamingStrategy.GetPropertyName("items", false), items, normalFields, null);
            }

            if (countField != null)
            {
                await SerializePropertyAsync(writer, selection, countField);
            }

            await writer.WriteEndObjectAsync();
        }