WriteJson() public static method

public static WriteJson ( ICanJson doc ) : string
doc ICanJson
return string
Beispiel #1
0
        /// <summary>
        /// This is a bulk key request, not to be confused with requests using complex keys, see Key().
        /// </summary>
        public CouchQuery Keys(IList <object> keys)
        {
            var bulk = new CouchBulkKeys(keys.ToArray());

            Data(CouchDocument.WriteJson(bulk));
            return(this);
        }
Beispiel #2
0
        /// <summary>
        /// Create or update a list of ICouchDocuments in CouchDB. Uses POST and CouchDB will
        /// allocate new ids if the documents lack them.
        /// </summary>
        /// <param name="documents">List of documents to store.</param>
        /// <remarks>POST may be problematic in some environments.</remarks>
        public void SaveDocuments(IEnumerable <ICouchDocument> documents, bool allOrNothing)
        {
            var bulk = new CouchBulkDocuments(documents);

            try
            {
                var result = Request("_bulk_docs")
                             .Data(CouchDocument.WriteJson(bulk))
                             .Query("?all_or_nothing=" + allOrNothing.ToString().ToLower())
                             .PostJson()
                             .Parse <JArray>();

                int index = 0;
                foreach (var document in documents)
                {
                    document.Id  = (result[index])["id"].Value <string>();
                    document.Rev = (result[index])["rev"].Value <string>();
                    ++index;
                }
            }
            catch (WebException e)
            {
                throw CouchException.Create("Failed to create bulk documents", e);
            }
        }
Beispiel #3
0
        /// <summary>
        /// This is a bulk key request, not to be confused with requests using complex keys, see Key().
        /// </summary>
        public CouchQuery Keys(object[] keys)
        {
            var bulk = new CouchBulkKeys(keys);

            Data(CouchDocument.WriteJson(bulk));
            return(this);
        }
Beispiel #4
0
 /// <summary>
 /// Create a given ICouchDocument in CouchDB. Uses POST and CouchDB will allocate a new id and overwrite any existing id.
 /// </summary>
 /// <param name="document">Document to store.</param>
 /// <returns>Document with Id and Rev set.</returns>
 /// <remarks>POST which may be problematic in some environments.</remarks>
 public ICouchDocument CreateDocument(ICouchDocument document)
 {
     try
     {
         JObject result = Request().Data(CouchDocument.WriteJson(document)).PostJson().Check("Failed to create document").Result();
         document.Id  = result["id"].Value <string>();
         document.Rev = result["rev"].Value <string>();
         return(document);
     }
     catch (WebException e)
     {
         throw CouchException.Create("Failed to create document", e);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Write a CouchDocument or ICouchDocument, it may already exist in db and will then be overwritten.
        /// </summary>
        /// <param name="document">Couch document</param>
        /// <param name="batch">True if we don't want to wait for flush (commit).</param>
        /// <returns>Couch Document with new Rev set.</returns>
        /// <remarks>This relies on the document to already have an id.</remarks>
        public ICouchDocument WriteDocument(ICouchDocument document, bool batch)
        {
            if (document.Id == null)
            {
                throw CouchException.Create(
                          "Failed to write document using PUT because it lacks an id, use CreateDocument instead to let CouchDB generate an id");
            }
            JObject result =
                Request(document.Id).Query(batch ? "?batch=ok" : null).Data(CouchDocument.WriteJson(document)).Put().Check("Failed to write document").Result();

            document.Id  = result["id"].Value <string>(); // Not really needed
            document.Rev = result["rev"].Value <string>();

            return(document);
        }
Beispiel #6
0
 /// <summary>
 /// Delete documents in bulk fashion.
 /// </summary>
 public void DeleteDocuments(ICanJson bulk)
 {
     try
     {
         var result = Request("_bulk_docs").Data(CouchDocument.WriteJson(bulk)).PostJson().Parse <JArray>();
         for (int i = 0; i < result.Count(); i++)
         {
             //documents[i].id = (result[i])["id"].Value<string>();
             //documents[i].rev = (result[i])["rev"].Value<string>();
             if ((result[i])["error"] != null)
             {
                 throw CouchException.Create(string.Format(CultureInfo.InvariantCulture,
                                                           "Document with id {0} was not deleted: {1}: {2}",
                                                           (result[i])["id"].Value <string>(), (result[i])["error"], (result[i])["reason"]));
             }
         }
     }
     catch (WebException e)
     {
         throw CouchException.Create("Failed to bulk delete documents", e);
     }
 }
Beispiel #7
0
        public IEnumerable <T> GetArbitraryDocuments <T>(IEnumerable <string> documentIds, Func <T> ctor)
        {
            var bulk = new CouchBulkKeys(documentIds.Cast <object>());

            return(QueryAllDocuments().Data(CouchDocument.WriteJson(bulk)).IncludeDocuments().GetResult().ArbitraryDocuments(ctor));
        }
Beispiel #8
0
        public IEnumerable <T> GetDocuments <T>(IEnumerable <string> documentIds) where T : ICouchDocument, new()
        {
            var bulk = new CouchBulkKeys(documentIds.Cast <object>());

            return(QueryAllDocuments().Data(CouchDocument.WriteJson(bulk)).IncludeDocuments().GetResult().Documents <T>());
        }