Example #1
0
File: RSC.cs Project: dmsl/rayzit
        private static string CheckResponse(HttpResponseMessage resp)
        {
            if (resp.StatusCode != HttpStatusCode.OK)
            {
                if (resp.StatusCode == HttpStatusCode.ServiceUnavailable)
                {
                    var writer = new JSonWriter(true);

                    writer.WriteObjectBegin();
                    writer.WriteMember("status", "error");
                    writer.WriteMember("message", ErrorMessages.ServiceUnavailable);
                    writer.WriteObjectEnd();

                    return(writer.ToString());
                }

                var writer2 = new JSonWriter(true);

                writer2.WriteObjectBegin();
                writer2.WriteMember("status", "error");
                writer2.WriteMember("message", ErrorMessages.ConnectionProblem);
                writer2.WriteObjectEnd();

                return(writer2.ToString());
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Gets the JSON string representation of this request.
        /// </summary>
        public override string ToString()
        {
            JSonWriter output = new JSonWriter(FormattedOutput);

            Write(output);
            return(output.ToString());
        }
 /// <summary>
 /// Converts <see cref="BayeuxRequest"/> to string containing its JSON representation.
 /// </summary>
 protected string SerializeRequest(BayeuxRequest request)
 {
     _writerCache.Remove(0, _writerCache.Length);
     _jsonWriter.WriteArrayBegin();
     request.Write(_jsonWriter);
     _jsonWriter.WriteArrayEnd();
     return(_jsonWriter.ToString());
 }
Example #4
0
        /// <summary>
        /// Creates a new RayzReply
        /// </summary>
        /// <param name="rayzId"> Rayz ID </param>
        /// <param name="rayzReply"> The RayzReply message </param>
        /// <param name="attachments"> The RayzReply Attachments </param>
        public async Task <IJSonObject> NewRayzReply(String rayzId, String rayzReply, ObservableCollection <RayzItAttachment> attachments)
        {
            try
            {
                var content = new MultipartFormDataContent();
                var writer  = new JSonWriter(true);
                var reader  = new JSonReader();

                // PLAY PROBLEM
                foreach (var param in content.Headers.ContentType.Parameters.Where(param => param.Name.Equals("boundary")))
                {
                    param.Value = param.Value.Replace("\"", String.Empty);
                }

                writer.WriteObjectBegin();
                writer.WriteMember("userId", _deviceId);
                writer.WriteMember("rayzId", rayzId);
                writer.WriteMember("rayzReplyMessage", rayzReply);
                writer.WriteObjectEnd();

                var json = new StringContent(writer.ToString());
                content.Add(json, "\"json\"");

                foreach (var a in attachments)
                {
                    var fileContent = new StreamContent(new MemoryStream(a.FileBody));
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name     = "\"attachment\"",
                        FileName = "\"attachment.file\""
                    };
                    fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(a.ContType);
                    content.Add(fileContent);
                }

                var response = await _client.PostAsync(new Uri(ServerBaseUri, "/rayz/reply"), content);

                var cr = CheckResponse(response);
                if (cr != null)
                {
                    var creply = reader.ReadAsJSonObject(cr);
                    return(creply);
                }

                var r = await response.Content.ReadAsStringAsync();

                var reply = reader.ReadAsJSonObject(r);
                return(reply);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #5
0
        public string ToString(string format)
        {
            bool indent = JSonObjectConverter.GetIndentAndVerifyToStringFormat(format);

            using (var writer = new JSonWriter(indent))
            {
                writer.CompactEnumerables = format == JSonObjectConverter.CompactEnumerables;

                writer.Write(_data);
                return(writer.ToString());
            }
        }
Example #6
0
        public void TestReadWriteAbstractArrays()
        {
            JSonWriter writer = new JSonWriter();

            string[] data1 = { "123456", "78910" };
            string[] data2 = { "A", "B", "C" };

            var container = new ArrayContainingClass(data1, data2);

            writer.Write(container);

            Console.WriteLine("JSON:");
            Console.WriteLine(writer.ToString());

            var reader = new JSonReader(writer.ToString());

            var readContainer = reader.ReadAsJSonObject().ToObjectValue <ArrayContainingClass>();

            Assert.IsNotNull(readContainer);
            Assert.IsNotNull(readContainer.Data1);
            Assert.IsNotNull(readContainer.Data2);
            Assert.AreEqual(2, readContainer.Data1.Length);
            Assert.AreEqual(3, readContainer.Data2.Length);
        }
Example #7
0
        public async Task <IJSonObject> CheckRayzReplies(string rayzid, IEnumerable <string> rayzsList)
        {
            try
            {
                var writer = new JSonWriter(true);
                var reader = new JSonReader();

                writer.WriteObjectBegin();
                writer.WriteMember("userId", _deviceId);
                writer.WriteMember("rayzId", rayzid);

                writer.WriteMember("rayzReplyIds");
                writer.WriteArrayBegin();

                //Group up the records in the collection
                foreach (var record in rayzsList)
                {
                    writer.WriteValue(record);
                }

                writer.WriteArrayEnd();


                writer.WriteObjectEnd();

                var json = new StringContent(writer.ToString());
                json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                var response = await _client.PostAsync(new Uri(ServerBaseUri, "/rayz/reply/check"), json);

                var cr = CheckResponse(response);
                if (cr != null)
                {
                    var creply = reader.ReadAsJSonObject(cr);
                    return(creply);
                }

                var r = await response.Content.ReadAsStringAsync();

                var reply = reader.ReadAsJSonObject(r);
                return(reply);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        public void ParseSerializedCurrentDateAsJavaScript()
        {
            var writer = new JSonWriter();
            var now    = DateTime.Now;

            // remove miliseconds:
            now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, 0, now.Kind);

            using (writer.WriteObject())
                writer.WriteMember("date", now, JSonWriterDateFormat.JavaScript);

            var reader = new JSonReader(writer.ToString());
            var item   = reader.ReadAsJSonObject();
            var date   = item["date"].DateTimeValue;

            Assert.AreEqual(now, date);
        }
        public void ParseSerializedCurrentDateAsEpochSeconds()
        {
            var writer = new JSonWriter();
            var now    = DateTime.Now;

            using (writer.WriteObject())
                writer.WriteMember("date", now, JSonWriterDateFormat.UnixEpochSeconds);

            // remove below seconds:
            now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, 0, now.Kind);

            var reader = new JSonReader(writer.ToString());
            var item   = reader.ReadAsJSonObject();
            var date   = item["date"].ToDateTimeValue(JSonDateTimeKind.UnixEpochSeconds);

            Assert.AreNotEqual(0, item["date"].Int64Value);
            Assert.AreEqual(now, date);
        }
Example #10
0
        /// <summary>
        /// Makes a Re-Rayz Request
        /// </summary>
        /// <param name="rayzId"> Rayz ID </param>
        /// <param name="latitude"> Latitude Coordinate </param>
        /// <param name="longitude"> Longitude Coordinate </param>
        /// <param name="accuracy"> Accuracy of the user's position in meters </param>
        /// <param name="maxDistance"> Max Rayz Distance - NOT USED </param>
        public async Task <IJSonObject> ReRayz(String rayzId, String latitude, String longitude, String accuracy, String maxDistance)
        {
            try
            {
                var writer = new JSonWriter(true);
                var reader = new JSonReader();

                // Make sure that lat and long are dot separated
                latitude  = latitude.Replace(@",", @".");
                longitude = longitude.Replace(@",", @".");

                writer.WriteObjectBegin();
                writer.WriteMember("userId", _deviceId);
                writer.WriteMember("rayzId", rayzId);
                writer.WriteMember("latitude", latitude);
                writer.WriteMember("longitude", longitude);
                writer.WriteMember("accuracy", accuracy);
                writer.WriteMember("maxDistance", maxDistance);
                writer.WriteObjectEnd();

                var json = new StringContent(writer.ToString());
                json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                var response = await _client.PostAsync(new Uri(ServerBaseUri, "/rayz/rerayz"), json);

                var cr = CheckResponse(response);
                if (cr != null)
                {
                    var creply = reader.ReadAsJSonObject(cr);
                    return(creply);
                }

                var r = await response.Content.ReadAsStringAsync();

                var reply = reader.ReadAsJSonObject(r);
                return(reply);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #11
0
        public void ParseAdvancedStructure()
        {
            var jsonText    = LoadTestInputFile("advanced.json");
            var watchReader = Stopwatch.StartNew();
            var reader      = new JSonReader(jsonText);
            var result      = reader.ReadAsJSonObject();

            watchReader.Stop();

            var watchWriter = Stopwatch.StartNew();
            var writer      = new JSonWriter(true);

            writer.Write(result);
            watchWriter.Stop();
            Console.WriteLine("Parsing taken: {0}", watchReader.Elapsed);
            Console.WriteLine("Serializing taken: {0}", watchWriter.Elapsed);
            Console.WriteLine("{0}", writer.ToString());

            Assert.IsNotNull(result);
        }
 public void TestClean()
 {
     Console.WriteLine("Serialized JSON object:");
     Console.WriteLine(writer.ToString());
     writer.Close();
 }
Example #13
0
 public void SetValue(DateTime value, JSonDateTimeKind kind)
 {
     Data = JSonWriter.ToString(value);
 }
Example #14
0
 void IJSonMutableObject.SetValue(DateTime value)
 {
     Data = JSonWriter.ToString(value);
 }
Example #15
0
 void IJSonMutableObject.SetValue(DateTime value)
 {
     Data = DateTimeHelper.ToNumber(value, JSonDateTimeKind.Default);
     StringRepresentation = JSonWriter.ToString(value);
 }
Example #16
0
 public void SetValue(DateTime value, JSonDateTimeKind kind)
 {
     Data = DateTimeHelper.ToNumber(value, kind);
     StringRepresentation = JSonWriter.ToString(value);
 }
Example #17
0
 void IJSonMutableObject.SetValue(TimeSpan value)
 {
     Data = value.Ticks;
     StringRepresentation = JSonWriter.ToString(value);
 }
 /// <summary>
 /// Init constructor.
 /// </summary>
 public JSonDecimalInt64Object(DateTime date, JSonDateTimeKind kind)
 {
     _data = DateTimeHelper.ToNumber(date, kind);
     _stringRepresentation = JSonWriter.ToString(date);
 }
Example #19
0
 void IJSonMutableObject.SetValue(bool value)
 {
     Data = value ? 1L : 0L;
     StringRepresentation = JSonWriter.ToString(value);
 }
 /// <summary>
 /// Init constructor.
 /// </summary>
 public JSonDecimalInt64Object(TimeSpan time)
 {
     _data = time.Ticks;
     _stringRepresentation = JSonWriter.ToString(time);
 }
Example #21
0
 /// <summary>
 /// Init constructor.
 /// </summary>
 public JSonStringObject(Guid guid)
 {
     _data = JSonWriter.ToString(guid);
 }