Deserialize() public méthode

public Deserialize ( ) : object
Résultat object
Exemple #1
0
        public void MetricsCollection ()
        {
            string id = "myuniqueid";
            var metrics = new MetricsCollection (id, new MemorySampleStore ());
            Assert.AreEqual ("myuniqueid", metrics.AnonymousUserId);

            metrics.AddDefaults ();
            Assert.IsTrue (metrics.Count > 0);

            string metrics_str = metrics.ToJsonString ();
            Assert.IsTrue (metrics_str.Contains ("\"ID\":\"myuniqueid\""));

            // tests/Makefile.am runs the tests with Locale=it_IT
            Assert.IsTrue (metrics_str.Contains ("it-IT"));

            // Make sure DateTime samples are saved as invariant strings
            var now = DateTime.Now;
            var time_metric = metrics.Add ("Foo", now);
            Assert.AreEqual (Hyena.DateTimeUtil.ToInvariantString (now), metrics.Store.GetFor (time_metric).First ().Value);

            // Make sure we can read the JSON back in
            var ds = new Json.Deserializer ();
            ds.SetInput (metrics.ToJsonString ());
            var json_obj = ds.Deserialize () as JsonObject;
            Assert.AreEqual (id, json_obj["ID"]);
            Assert.IsTrue (json_obj["Metrics"] is JsonObject);
        }
		public void ParseTest ()
		{
			Deserializer deserializer = new Deserializer ();
			
			string refJson =
				"{ \"api-ref\" : \"http://domain/api/1.0/sally/notes\", \"href\" : \"http://domain/sally/notes\"}";
			deserializer.SetInput (refJson);
			JsonObject refObj = (JsonObject) deserializer.Deserialize ();
			ResourceReference resourceRef = ResourceReference.ParseJson (refObj);

			Assert.AreEqual ("http://domain/api/1.0/sally/notes",
			                 resourceRef.ApiRef,
			                 "ApiRef");
			Assert.AreEqual ("http://domain/sally/notes",
			                 resourceRef.Href,
			                 "Href");

			refJson =
				"{ \"api-ref\" : \"http://domain/api/1.0/sally/notes\"}";
			deserializer.SetInput (refJson);
			refObj = (JsonObject) deserializer.Deserialize ();
			resourceRef = ResourceReference.ParseJson (refObj);

			Assert.AreEqual ("http://domain/api/1.0/sally/notes",
			                 resourceRef.ApiRef,
			                 "ApiRef");
			Assert.IsNull (resourceRef.Href, "Href should be null when none specified");

			refJson =
				"{ \"href\" : \"http://domain/sally/notes\"}";
			deserializer.SetInput (refJson);
			refObj = (JsonObject) deserializer.Deserialize ();
			resourceRef = ResourceReference.ParseJson (refObj);

			Assert.AreEqual ("http://domain/sally/notes",
			                 resourceRef.Href,
			                 "Href");
			Assert.IsNull (resourceRef.ApiRef, "ApiRef should be null when none specified");
		}
		public void ParseTest ()
		{
			Deserializer deserializer = new Deserializer ();

			string noteJson = "{\"guid\": \"002e91a2-2e34-4e2d-bf88-21def49a7705\"," +
				"\"title\" :\"New Note 6\"," +
				"\"note-content\" :\"New Note 6\\nDescribe youre note <b>here</b>.\"," +
				"\"note-content-version\" : 0.2," +
				"\"last-change-date\" : \"2009-04-19T21:29:23.2197340-07:00\"," +
				"\"last-metadata-change-date\" : \"2009-04-19T21:29:23.2197340-07:00\"," +
				"\"create-date\" : \"2008-03-06T13:44:46.4342680-08:00\"," +
				"\"last-sync-revision\" : 57," +
				"\"open-on-startup\" : false," +
				"\"tags\" : [\"tag1\",\"tag2\"]" +
				"}";
			deserializer.SetInput (noteJson);
			JsonObject noteObj = (JsonObject) deserializer.Deserialize ();
			//Assert.AreEqual (57, (int)noteObj["last-sync-revision"]);
			NoteInfo note = NoteInfo.ParseJson (noteObj);
			Assert.AreEqual ("002e91a2-2e34-4e2d-bf88-21def49a7705", note.Guid, "GUID");
			Assert.AreEqual ("New Note 6\nDescribe youre note <b>here</b>.", note.NoteContent, "NoteContent");
			Assert.AreEqual (0.2, note.NoteContentVersion, "NoteContentVersion");
			Assert.AreEqual ("New Note 6", note.Title, "Title");
			Assert.AreEqual (DateTime.Parse ("2009-04-19T21:29:23.2197340-07:00"),
					note.LastChangeDate, "LastChangeDate");
			Assert.AreEqual (DateTime.Parse ("2009-04-19T21:29:23.2197340-07:00"),
					note.LastMetadataChangeDate, "LastMetadataChangeDate");
			Assert.AreEqual (DateTime.Parse ("2008-03-06T13:44:46.4342680-08:00"),
					note.CreateDate, "CreateDate");
			Assert.AreEqual (57, note.LastSyncRevision, "LastSyncRevision");

			Assert.IsTrue (note.OpenOnStartup.HasValue, "OpenOnStartup should have value");
			Assert.IsFalse (note.OpenOnStartup.Value, "OpenOnStartup value");
			
			Assert.IsNotNull (note.Tags, "Tags should not be null");

			// TODO: Test resourceref,command,lack of guid,nullables,etc,
			//       ApplicationException when missing comma in string, wrong type, etc
		}
        public StationError GetError ()
        {
            StationError error = StationError.None;

            string response;
            using (StreamReader sr = new StreamReader (response_stream)) {
                response = sr.ReadToEnd ();
            }

            if (response.Contains ("<lfm status=\"failed\">")) {
                // XML reply indicates an error
                Match match = Regex.Match (response, "<error code=\"(\\d+)\">");
                if (match.Success) {
                    error = (StationError) Int32.Parse (match.Value);
                    Log.WarningFormat ("Lastfm error {0}", error);
                } else {
                    error = StationError.Unknown;
                }
            }
            if (response_format == ResponseFormat.Json && response.Contains ("\"error\":")) {
                // JSON reply indicates an error
                Deserializer deserializer = new Deserializer (response);
                JsonObject json = deserializer.Deserialize () as JsonObject;
                if (json != null && json.ContainsKey ("error")) {
                    error = (StationError) json["error"];
                    Log.WarningFormat ("Lastfm error {0} : {1}", error, (string)json["message"]);
                }
            }

            return error;
        }
        public JsonObject GetResponseObject ()
        {
            if (response_stream == null)
                return null;

            Deserializer deserializer = new Deserializer (response_stream);
            object obj = deserializer.Deserialize ();
            JsonObject json_obj = obj as Hyena.Json.JsonObject;

            if (json_obj == null) {
                throw new ApplicationException ("Lastfm invalid response : not a JSON object");
            }

            return json_obj;
        }
		public void ExceptionTest ()
		{
			bool expectedExceptionRaised = false;
			try {
				ResourceReference.ParseJson (null);
			} catch (ArgumentNullException) {
				expectedExceptionRaised = true;
			}
			Assert.IsTrue (expectedExceptionRaised,
			               "ArgumentNullException expected on null input");

			expectedExceptionRaised = false;
			
			Deserializer deserializer = new Deserializer ();
			string invalidRefJson =
				"{ \"api-ref\" : 5, \"href\" : \"http://domain/sally/notes\"}";
			deserializer.SetInput (invalidRefJson);
			JsonObject refObj = (JsonObject) deserializer.Deserialize ();
			try {
				ResourceReference.ParseJson (refObj);
			} catch (InvalidCastException) {
				expectedExceptionRaised = true;
			}
			Assert.IsTrue (expectedExceptionRaised,
			               "InvalidCastException expected on invalid input");
		}
        public static JsonArray ServerRequest(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if(response.StatusCode != HttpStatusCode.OK) {
                        string s = String.Format("Server error(HTTP {0}: {1}).",
                                                response.StatusCode, response.StatusDescription);
                        throw new Exception(s);
                    }

                    Stream			receiveStream = response.GetResponseStream();
                    Deserializer	d = new Deserializer(receiveStream);
                    object			jd = d.Deserialize();
                    JsonArray		ja = jd as JsonArray;

                    response.Close();
                    receiveStream.Close();
                    return ja;
                }
            }
            catch(Exception e)
            {
                SC.log(e.Message);
                return null;
            }
        }