public static void Main(string[] args) { Book abook = new Book(); //Here's an instance of our type to serialize abook.setAuthorName("Michael Feathers"); abook.setBookID(1337); abook.setTitle("Working Effectively with Legacy Code"); // // SimplTypesScope book_example_sts = new SimplTypesScope("book_example", typeof(Book)); //Serialize to JSON String jsonResult = SimplTypesScope.Serialize(abook, StringFormat.Json); //Serialize to XML // (Just change the StringFormat parameter!) String xmlResult = SimplTypesScope.Serialize(abook, StringFormat.Xml); Object result1 = book_example_sts.Deserialize(jsonResult, StringFormat.Json); Book r1 = (Book)result1; Object result2 = book_example_sts.Deserialize(xmlResult, StringFormat.Xml); Book r2 = (Book)result2; Console.WriteLine(jsonResult); Console.WriteLine(xmlResult); Console.ReadLine(); }
public void OnMetadataExtracted(object sender, JSCallbackEventArgs args) { if (args.Arguments.Length == 0) { Console.WriteLine("No value returned"); return; } JSValue value = args.Arguments[0]; String metadataJSON = value.ToString(); //Console.WriteLine(metadataJSON); Console.WriteLine("Extraction time : " + DateTime.Now.Subtract(parseStart).TotalMilliseconds); Console.WriteLine("Done getting value. Serializing JSON string to ElementState."); TranslationContext context = new TranslationContext(); context.SetUriContext(_puri); SimplTypesScope metadataTScope = SemanticsSessionScope.MetadataTranslationScope; Document myShinyNewMetadata = (Document)metadataTScope.Deserialize(metadataJSON, context, new MetadataDeserializationHookStrategy(SemanticsSessionScope), StringFormat.Json); Console.WriteLine("Extraction time including page load and deserialization: " + DateTime.Now.Subtract(timeStart).TotalMilliseconds); _webView.LoadCompleted -= WebView_LoadCompleted; SemanticsSessionScope.GlobalDocumentCollection.AddDocument(myShinyNewMetadata, _puri); SemanticsSessionScope.WebBrowserPool.Release(_webView); _requestTimedOut.Stop(); _closure.TaskCompletionSource.TrySetResult(myShinyNewMetadata); }
private void ReceiveMessageWorker() { Console.WriteLine("Entering OODSS Recieve Message loop"); while (_isRunning) { try { Receive(_clientSocket); ReceiveDone.Wait(_cancellationTokenSource.Token); int uidIndex = _response.IndexOf("uid:") + 4; string intString = _response.Substring(uidIndex, _response.IndexOf("\r\n", uidIndex) - uidIndex); int responseUid = int.Parse(intString); ServiceMessage serviceMessage = (ServiceMessage)SimplTypesScope.Deserialize(_response.Substring(_response.IndexOf('<')), StringFormat.Xml); if (serviceMessage == null) { throw new ArgumentNullException(string.Format("Received a null {0} object. Deserialization failed?", "serviceMessage")); } if (serviceMessage is UpdateMessage) { var updateMessage = serviceMessage as UpdateMessage; updateMessage.ProcessUpdate(ObjectRegistry); } else if (serviceMessage is ResponseMessage) { var responseMessage = serviceMessage as ResponseMessage; responseMessage.ProcessResponse(ObjectRegistry); QueueObject queueObject; _pendingRequests.TryGetValue(responseUid, out queueObject); if (queueObject == null) { Console.WriteLine("No pending request with Uid: {0}", responseUid); } else { TaskCompletionSource <ResponseMessage> taskCompletionSource = queueObject.Tcs; if (taskCompletionSource != null) { Console.WriteLine("--- Finished Request : {0}", queueObject.Uid); taskCompletionSource.TrySetResult(responseMessage); } } } } catch (OperationCanceledException e) { Console.WriteLine("The operation was cancelled." + e.TargetSite); } catch (Exception e) { Console.WriteLine("Caught Exception :\n " + e.StackTrace); } } }
public override void Parse() { SimplTypesScope metadataTScope = SemanticsSessionScope.MetadataTranslationScope;; Document parsedDoc = metadataTScope.Deserialize(Simpl.Fundamental.Net.PURLConnection.Stream, Format.Xml) as Document; DocumentClosure.TaskCompletionSource.TrySetResult(parsedDoc); // post parse: regex filtering + field parser }
public void TestMethod1() { Book abook = new Book(); //Here's an instance of our type to serialize abook.initBook(); // // SimplTypesScope book_example_sts = new SimplTypesScope("book_example", typeof(Book)); //Serialize to JSON String jsonResult = SimplTypesScope.Serialize(abook, StringFormat.Json); //Serialize to XML // (Just change the StringFormat parameter!) String xmlResult = SimplTypesScope.Serialize(abook, StringFormat.Xml); Object result1 = book_example_sts.Deserialize(jsonResult, StringFormat.Json); Book r1 = (Book)result1; Object result2 = book_example_sts.Deserialize(xmlResult, StringFormat.Xml); Book r2 = (Book)result2; Assert.IsTrue(result1.GetType().IsAssignableFrom(abook.GetType())); Assert.AreEqual(r1.getAuthor(), abook.getAuthor()); Assert.AreEqual(r1.getBookID(), abook.getBookID()); Assert.AreEqual(r1.getTitle(), abook.getTitle()); Assert.AreEqual(r1.getAuthor(), "Michael Feathers"); Assert.AreEqual(r1.getBookID(), 1337); Assert.AreEqual(r1.getTitle(), "Working Effectively with Legacy Code"); Assert.AreEqual(r2.getAuthor(), abook.getAuthor()); Assert.AreEqual(r2.getBookID(), abook.getBookID()); Assert.AreEqual(r2.getTitle(), abook.getTitle()); }
public void TestSemanticServiceError() { SimplTypesScope repositoryMetadataTranslationScope = RepositoryMetadataTranslationScope.Get(); SimplTypesScope typesScope = SimplTypesScope.Get("MetadataServicesTranslationScope", repositoryMetadataTranslationScope, typeof(MetadataRequest), typeof(MetadataResponse), typeof(SemanticServiceError)); String response = "<semantic_service_error code=\"2001\" error_message=\"Error\" />"; ServiceMessage serviceMessage = (ServiceMessage)typesScope.Deserialize(response, StringFormat.Xml); try { (serviceMessage as SemanticServiceError).Perform(); } catch (Exception ex) { Console.WriteLine(ex); } Console.WriteLine(SimplTypesScope.Serialize(serviceMessage, StringFormat.Xml)); }
/// <summary> /// deseiralizes the data, given the input stream and format. returns object representation of the input data. /// </summary> /// <param name="simplTypesScope">Simpl type scope for deserialization</param> /// <param name="inputStream">Input stream of the serialized representaiton</param> /// <param name="format">Format of the serialization</param> /// <returns>a deserialized object from the type scope</returns> public static Object TestDeserialization(SimplTypesScope simplTypesScope, Stream inputStream, Format format) { Object deserializedObj = simplTypesScope.Deserialize(inputStream, format); return(deserializedObj); }
public void RealRssXml() { /* * Read in RSS feed from URL */ const string url = "http://rss.cnn.com/rss/cnn_us.rss"; WebRequest objRequest = WebRequest.Create(url); Stream responseStream = objRequest.GetResponse().GetResponseStream(); if (responseStream != null) { String rssContent = new StreamReader(responseStream).ReadToEnd(); PrintMessage("Raw RSS Feed:"); PrintXmlData(rssContent); /* * Get the simpl types scope. This references all of the classes that we * are considering for translation. */ SimplTypesScope rssScope = SimplTypesScope.Get("rss", typeof(Rss), typeof(Channel), typeof(Item)); /* * Instantiate Rss by translating the xml to C# objects. Take a * look at Rss, Channel, and Item to see how they are annotated to * facilitate translation. Notice that Rss's class tag is rss. This is * an inherent rule: all classes that subclass ElementState have a class * tag of just the class name. Normally Simpl.Serialization uses a camel-case * translatio. Fields that are translated into attributes * and sub elements use a similar convention for determining identifiers. */ Rss feed = (Rss)rssScope.Deserialize(rssContent, StringFormat.Xml); /* * Notice that, translated back to xml, not all attributes and elements * still remain. If an attribute or element is not annotated in the * corresponding class it is simply ignored. */ PrintMessage("Feed Translated back to XML by Simpl.Serializaion"); TestHelper.TestMethods.TestSerialization(feed, Format.Xml); /* * Create our own item to add to the channel */ Item ecologylabItem = new Item { Title = "The Interface Ecology Lab", Description = "Highlights the cool research going on at the lab.", Author = "Dr. Andruid Kerne", Link = new ParsedUri("http://www.ecologylab.net") }; /* * Add our item to the front of the channel. */ feed.Channel.Items.Insert(0, ecologylabItem); PrintMessage("Feed Translated back to XML with our added item"); TestHelper.TestMethods.TestSerialization(feed, Format.Xml); } }