Exemplo n.º 1
0
 public void SerializeBoolTest ()
 {
     Serializer ser = new Serializer (true);
     Assert.AreEqual ("true", ser.Serialize ());
     ser.SetInput (false);
     Assert.AreEqual ("false", ser.Serialize ());
 }
Exemplo n.º 2
0
 public void SerializeIntTest ()
 {
     Serializer ser = new Serializer (12);
     Assert.AreEqual ("12", ser.Serialize ());
     ser.SetInput (-658);
     Assert.AreEqual ("-658", ser.Serialize ());
     ser.SetInput (0);
     Assert.AreEqual ("0", ser.Serialize ());
 }
Exemplo n.º 3
0
 public void SerializeDoubleTest ()
 {
     Serializer ser = new Serializer (12.5);
     Assert.AreEqual ("12.5", ser.Serialize ());
     ser.SetInput (-658.1);
     Assert.AreEqual ("-658.1", ser.Serialize ());
     ser.SetInput (0.0);
     Assert.AreEqual ("0", ser.Serialize ());
     ser.SetInput (0.1);
     Assert.AreEqual ("0.1", ser.Serialize ());
 }
        Dictionary<string, object> DoServiceCall(string method, params object[] parameters)
        {
            var callId = Interlocked.Increment (ref this.callId);

            var callObject = new Dictionary<string, object> {
                {"id", callId},
                {"method", method},
                {"params", parameters}
            };

            #if DEBUG
            Console.Error.Write ("Serializing JSON-RPC call object...");
            Stopwatch s = new Stopwatch ();
            s.Start ();
            #endif
            string jsonPayload = new Serializer (callObject).Serialize ();
            #if DEBUG
            s.Stop ();
            Console.Error.WriteLine ("done in {0}.", s.Elapsed);
            Console.Error.WriteLine ("Serialized payload: {0}", jsonPayload);
            #endif

            var serviceClient = new CookieClient ();

            if (cookies != null)
                serviceClient.Cookies = cookies;

            serviceClient.Headers.Add (HttpRequestHeader.ContentType, "application/json");

            #if DEBUG
            Console.Error.Write ("Making request...");
            s.Reset ();
            s.Start ();
            #endif

            string responseJson = serviceClient.UploadString (ServiceUri, jsonPayload);

            #if DEBUG
            s.Stop ();
            Console.Error.WriteLine ("done in {0}.", s.Elapsed);
            Console.Error.WriteLine ("Response: {0}", responseJson);
            #endif

            cookies = serviceClient.Cookies;

            #if DEBUG
            s.Reset ();
            Console.Error.Write ("Deserializing result...");
            s.Start ();
            #endif
            var response = new Deserializer (responseJson).Deserialize () as Dictionary<string, object>;
            #if DEBUG
            s.Stop ();
            Console.Error.WriteLine ("done in {0}.", s.Elapsed);
            #endif

            if ((long)response ["id"] != callId)
                throw new ApplicationException (string.Format ("Response ID and original call ID don't match. Expected {0}, received {1}.", callId, response ["id"]));

            if (response ["error"] != null)
                throw new ApplicationException (string.Format ("Received error message from Bugzilla. Message: {0}", ((JsonObject)response ["error"]) ["message"]));

            return response;
        }
        public Dictionary<string, object> DoServiceCall(string method, params object[] parameters)
        {
            var callId = Interlocked.Increment (ref this.callId);

            var callObject = new Dictionary<string, object> {
                {"id", callId},
                {"method", method},
                {"params", parameters}
            };

            #if DEBUG
            Console.Error.Write ("Serializing JSON-RPC call object...");
            Stopwatch s = new Stopwatch ();
            s.Start ();
            #endif
            string jsonPayload = new Serializer (callObject).Serialize ();
            #if DEBUG
            s.Stop ();
            Console.Error.WriteLine ("done in {0}.", s.Elapsed);
            #endif

            var serviceClient = new CookieClient ();

            if (cookies != null) serviceClient.Cookies = cookies;

            #if DEBUG
            Console.Error.Write ("Making request...");
            s.Reset ();
            s.Start ();
            #endif

            byte[] returnData = serviceClient.UploadData (ServiceUri, Encoding.UTF8.GetBytes (jsonPayload));

            #if DEBUG
            s.Stop ();
            Console.Error.WriteLine ("done in {0}.", s.Elapsed);
            #endif

            cookies = serviceClient.Cookies;

            #if DEBUG
            s.Reset ();
            Console.Error.Write ("Decompressing result...");
            s.Start ();
            #endif
            // All this because deluge always returns gzip data, despite what you send for Accept-Encoding.
            var responseJson = new StreamReader (new GZipStream (new MemoryStream (returnData), CompressionMode.Decompress), Encoding.UTF8).ReadToEnd ();
            #if DEBUG
            s.Stop ();
            Console.Error.WriteLine ("done in {0}.", s.Elapsed);
            #endif

            #if DEBUG
            s.Reset ();
            Console.Error.Write ("Deserializing result...");
            s.Start ();
            #endif
            var response = new Deserializer (responseJson).Deserialize () as Dictionary<string, object>;
            #if DEBUG
            s.Stop ();
            Console.Error.WriteLine ("done in {0}.", s.Elapsed);
            #endif

            if ((long) response["id"] != callId)
                throw new ApplicationException (string.Format ("Response ID and original call ID don't match. Expected {0}, received {1}.", callId, response["id"]));

            if (response["error"] != null)
                throw new ApplicationException (string.Format ("Received error message from Deluge. Message: {0}", ((JsonObject) response["error"])["message"]));

            return response;
        }
Exemplo n.º 6
0
 private void VerifyString (string original, string expectedSerialized)
 {
     Serializer ser = new Serializer (original);
     string output = ser.Serialize ();
     Assert.AreEqual (expectedSerialized, output, "Serialized Output");
     Assert.AreEqual (original, new Deserializer (output).Deserialize (),
                      "Input should be identical after serialized then deserialized back to string");
 }
Exemplo n.º 7
0
        public void SerializeObjectTest ()
        {
            JsonObject obj1 = new JsonObject ();
            obj1 ["intfield"] = 1;
            obj1 ["text field"] = "text";
            obj1 ["double-field"] = 0.1;
            obj1 ["Boolean, Field"] = true;
            obj1 ["\"Null\"\nfield"] = null;

            Serializer ser = new Serializer (obj1);
            // Test object equality, since order not guaranteed
            string output = ser.Serialize ();
            JsonObject reconstitutedObj1 = (JsonObject) new Deserializer (output).Deserialize ();
            AssertJsonObjectsEqual (obj1, reconstitutedObj1);

            JsonObject obj2 = new JsonObject ();
            obj2 ["double-field"] = 0.1;
            obj2 ["\"Null\"\nfield"] = null;
            ser.SetInput (obj2);
            output = ser.Serialize ();
            Assert.IsTrue ((output == "{\"double-field\":0.1,\"\\\"Null\\\"\\nfield\":null}") ||
                           (output == "{\"\\\"Null\\\"\\nfield\":null,\"double-field\":0.1}"),
                           "Serialized output format");
        }
Exemplo n.º 8
0
        public void SerializeArrayTest ()
        {
            JsonArray simpleArray = new JsonArray ();
            simpleArray.Add (1);
            simpleArray.Add ("text");
            simpleArray.Add (0.1);
            simpleArray.Add ("5");
            simpleArray.Add (false);
            simpleArray.Add (null);

            Serializer ser = new Serializer (simpleArray);
            Assert.AreEqual ("[1,\"text\",0.1,\"5\",false,null]",
                             ser.Serialize ());

            JsonArray emptyArray = new JsonArray ();
            ser.SetInput (emptyArray);
            Assert.AreEqual ("[]", ser.Serialize ());
        }
Exemplo n.º 9
0
 public void SerializeNullTest ()
 {
     Serializer ser = new Serializer (null);
     Assert.AreEqual ("null", ser.Serialize ());
 }