예제 #1
0
 public PropertyNames()
 {
     _toTest         = new ObjectToSerialize();
     _db             = new ArangoDatabase();
     _docSerializer  = new DocumentSerializer(_db);
     _jsonSerializer = _docSerializer.CreateJsonSerializer();
 }
        public JsonContent(IArangoDatabase db, object value)
        {
            var jw = new JsonTextWriter(new StreamWriter(_Stream));

            var jObject = value as JObject;

            if (jObject != null)
            {
                jObject.WriteTo(jw);
            }
            else
            {
                var docSerializer = new DocumentSerializer(db);
                var serializer    = docSerializer.CreateJsonSerializer();
                serializer.Serialize(jw, value);
            }
            jw.Flush();
            _Stream.Position = 0;
        }
예제 #3
0
 protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
 {
     var jObject = data as JObject;
     if (jObject != null)
     {
         var streamWriter = new StreamWriter(stream);
         await streamWriter.WriteAsync(jObject.ToString(Formatting.None)).ConfigureAwait(false);
         await streamWriter.FlushAsync().ConfigureAwait(false);
     }
     else
     {
         var docSerializer = new DocumentSerializer(db);
         var streamWriter = new StreamWriter(stream);
         var jsonWriter = new JsonTextWriter(streamWriter);
         var serializer = docSerializer.CreateJsonSerializer();
         serializer.Serialize(jsonWriter, data);
         await streamWriter.FlushAsync().ConfigureAwait(false);
     }
 }
예제 #4
0
        protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            var jObject = data as JObject;

            if (jObject != null)
            {
                var streamWriter = new StreamWriter(stream);
                var jsonWriter   = new JsonTextWriter(streamWriter);
                jObject.WriteTo(jsonWriter);
                await streamWriter.FlushAsync().ConfigureAwait(false);
            }
            else
            {
                var docSerializer = new DocumentSerializer(db);
                var streamWriter  = new StreamWriter(stream);
                var jsonWriter    = new JsonTextWriter(streamWriter);
                var serializer    = docSerializer.CreateJsonSerializer();
                serializer.Serialize(jsonWriter, data);
                await streamWriter.FlushAsync().ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Imports documents
        /// </summary>
        /// <param name="collection">The collection name</param>
        /// <param name="documents">Documents to import</param>
        /// <param name="createCollection">If true, then the collection is created if it does not yet exist</param>
        /// <param name="overwrite">If true, then all data in the collection will be removed prior to the import</param>
        /// <param name="waitForSync">Wait until document has been synced to disk</param>
        /// <param name="onDuplicate">Controls what action is carried out in case of a unique key constraint violation</param>
        /// <param name="complete">If true, then it will make the whole import fail if any error occurs</param>
        /// <param name="details">If true, then the result will include details about documents that could not be imported</param>
        /// <param name="baseResult"></param>
        /// <returns>BulkImportResult</returns>
        public async Task <BulkImportResult> BulkImportAsync(string collection, IEnumerable documents, bool?createCollection = null, bool?overwrite = null
                                                             , bool?waitForSync = null, ImportDuplicatePolicy?onDuplicate = null, bool?complete = null, bool?details = null
                                                             , Action <BaseResult> baseResult = null)
        {
            createCollection = createCollection ?? db.Setting.CreateCollectionOnTheFly;
            waitForSync      = waitForSync ?? db.Setting.WaitForSync;

            var command = new HttpCommand(db)
            {
                Api    = CommandApi.Import,
                Method = HttpMethod.Post,
                Query  = new Dictionary <string, string>()
            };

            command.Query.Add("type", "documents");
            command.Query.Add("collection", collection);
            command.Query.Add("createCollection", createCollection.ToString());
            command.Query.Add("waitForSync", waitForSync.ToString());

            if (overwrite.HasValue)
            {
                command.Query.Add("overwrite", overwrite.ToString());
            }
            if (onDuplicate.HasValue)
            {
                command.Query.Add("onDuplicate", Utility.Utils.ImportDuplicatePolicyToString(onDuplicate.Value));
            }
            if (complete.HasValue)
            {
                command.Query.Add("complete", complete.ToString());
            }
            if (details.HasValue)
            {
                command.Query.Add("details", details.ToString());
            }

            Func <StreamWriter, Task> onStreamReady = async(streamWriter) =>
            {
                var docSerializer = new DocumentSerializer(db);
                var serializer    = docSerializer.CreateJsonSerializer();
                var jsonWriter    = new JsonTextWriter(streamWriter);
                foreach (var d in documents)
                {
                    string json = d as string;
                    if (json != null)
                    {
                        streamWriter.Write($"{json}{Environment.NewLine}");
                        continue;
                    }

                    var jObject = d as JObject;
                    if (jObject != null)
                    {
                        jObject.WriteTo(jsonWriter);
                        streamWriter.Write(Environment.NewLine);
                        continue;
                    }

                    serializer.Serialize(jsonWriter, d);
                    streamWriter.Write(Environment.NewLine);
                }
                await streamWriter.FlushAsync().ConfigureAwait(false);
            };

            var result = await command.RequestDistinctResult <BulkImportResult>(onStreamReady : onStreamReady).ConfigureAwait(false);

            baseResult?.Invoke(result.BaseResult);

            return(result.Result);
        }