Esempio n. 1
0
        // create the same JSON encoding used by TouchDB
        // this lets us test comparisons as they would be encoded
        public virtual string Encode(object obj)
        {
            ObjectWriter mapper = new ObjectWriter();

            try
            {
                byte[] bytes  = mapper.WriteValueAsBytes(obj);
                string result = Sharpen.Runtime.GetStringForBytes(bytes);
                return(result);
            }
            catch (Exception e)
            {
                Log.E(Tag, "Error encoding JSON", e);
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Returns a serialized JSON byte enumerable object containing the properties
        /// of this object in human readable form.
        /// </summary>
        /// <returns>JSON bytes</returns>
        public IEnumerable <Byte> AsPrettyJson()
        {
            object properties = AsObject();

            if (properties != null)
            {
                ObjectWriter writer = Manager.GetObjectMapper().WriterWithDefaultPrettyPrinter();

                try {
                    _json = writer.WriteValueAsBytes(properties);
                } catch (IOException e) {
                    throw new InvalidDataException("The array or dictionary stored is corrupt", e);
                }
            }

            return(AsJson());
        }
Esempio n. 3
0
        /// <summary>
        /// Returns a serialized JSON byte enumerable object containing the properties
        /// of this object in human readable form.
        /// </summary>
        /// <returns>JSON bytes</returns>
        public IEnumerable <Byte> AsPrettyJson()
        {
            object properties = AsObject();

            if (properties != null)
            {
                ObjectWriter writer = Manager.GetObjectMapper().WriterWithDefaultPrettyPrinter();

                try {
                    _json = writer.WriteValueAsBytes(properties).ToArray();
                } catch (CouchbaseLiteException) {
                    Log.To.NoDomain.E(Tag, "Error writing body as pretty JSON, rethrowing...");
                } catch (Exception e) {
                    throw Misc.CreateExceptionAndLog(Log.To.NoDomain, e, Tag,
                                                     "Error writing body as pretty JSON");
                }
            }

            return(AsJson());
        }
Esempio n. 4
0
 protected internal virtual URLConnection SendRequest(string method, string path,
                                                      IDictionary <string, string> headers, object bodyObj)
 {
     try
     {
         Uri           url  = new Uri("cblite://" + path);
         URLConnection conn = (URLConnection)url.OpenConnection();
         conn.SetDoOutput(true);
         conn.SetRequestMethod(method);
         if (headers != null)
         {
             foreach (string header in headers.Keys)
             {
                 conn.SetRequestProperty(header, headers.Get(header));
             }
         }
         IDictionary <string, IList <string> > allProperties = conn.GetRequestProperties();
         if (bodyObj != null)
         {
             conn.SetDoInput(true);
             ByteArrayInputStream bais = new ByteArrayInputStream(mapper.WriteValueAsBytes(bodyObj
                                                                                           ));
             conn.SetRequestInputStream(bais);
         }
         Couchbase.Lite.Router.Router router = new Couchbase.Lite.Router.Router(manager, conn
                                                                                );
         router.Start();
         return(conn);
     }
     catch (UriFormatException)
     {
         Fail();
     }
     catch (IOException)
     {
         Fail();
     }
     return(null);
 }
Esempio n. 5
0
        protected internal virtual HttpURLConnection SendRequest(string method, string path,
                                                                 IDictionary <string, string> headers, IDictionary <string, object> bodyObj)
        {
            try
            {
                var url  = new Uri(new Uri((string)bodyObj["remote_url"]), path);
                var conn = url.OpenConnection();
                conn.SetDoOutput(true);
                conn.SetRequestMethod(method);
                if (headers != null)
                {
                    foreach (string header in headers.Keys)
                    {
                        conn.SetRequestProperty(header, headers[header]);
                    }
                }
                var allProperties = conn.GetRequestProperties();
                if (bodyObj != null)
                {
                    //conn.SetDoInput(true);
                    var bais = mapper.WriteValueAsBytes(bodyObj);
                    conn.SetRequestInputStream(bais);
                }

/*                var router = new Couchbase.Lite.Router.Router(manager, conn);
 *                              router.Start();
 */             return(conn);
            }
            catch (UriFormatException)
            {
                Assert.Fail();
            }
            catch (IOException)
            {
                Assert.Fail();
            }
            return(null);
        }