예제 #1
0
        public virtual async Task WriteArrayAsync(JsonTextWriter jsonWriter, IEnumerable <TObjectType> objects, CancellationToken cancellationToken = default)
        {
            CheckJsonTextWriter(jsonWriter);
            var objectJsonWriter = JsonFactoryContainer.CreateObjectWriter <TObjectType>();
            var writerTasks      = new List <Task>();
            await jsonWriter.WriteStartArrayAsync(cancellationToken).ConfigureAwait(false);

            foreach (TObjectType obj in objects)
            {
                writerTasks.Add(objectJsonWriter.WriteObjectAsync(jsonWriter, obj, cancellationToken));
            }

            await Task.WhenAll(writerTasks).ConfigureAwait(false);

            await jsonWriter.WriteEndArrayAsync(cancellationToken).ConfigureAwait(false);
        }
        public virtual async Task <IEnumerable <TReturnType> > ReadArrayAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
        {
            CheckJsonTextReader(jsonReader);

            if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartArray)
            {
                var         objectReader = JsonFactoryContainer.CreateObjectReader <TReturnType>();
                var         objects      = new List <TReturnType>();
                TReturnType obj          = await objectReader.ReadObjectAsync(jsonReader, cancellationToken);

                while (obj != null)
                {
                    objects.Add(obj);
                    obj = await objectReader.ReadObjectAsync(jsonReader, cancellationToken);
                }

                return(objects);
            }

            return(await Task.FromResult(default(IEnumerable <TReturnType>)));
        }