public void Registration() { ImportContext context = new ImportContext(); ThingImporter importer = new ThingImporter(); context.Register(importer); Assert.AreSame(importer, context.FindImporter(typeof(Thing))); }
public void RegistrationIsPerContext() { ImportContext context = new ImportContext(); ThingImporter exporter = new ThingImporter(); context.Register(exporter); context = new ImportContext(); Assert.AreNotSame(exporter, context.FindImporter(typeof(Thing))); }
private TransportComponent() { JsonExportContext = JsonConvert.CreateExportContext(); JsonExportContext.Register(new TransportMessageExporter()); JsonImportContext = JsonConvert.CreateImportContext(); JsonImportContext.Register(new TransportMessageImporter()); TransportListeners.Add(this); }
public void MemberImportCustomization() { TestTypeDescriptor logicalType = new TestTypeDescriptor(); PropertyDescriptorCollection properties = logicalType.GetProperties(); properties.Add(new TestPropertyDescriptor("prop1", typeof(object), new Hashtable())); ArrayList calls2 = new ArrayList(); TestObjectMemberImporter memberImporter2 = new TestObjectMemberImporter(calls2); Hashtable services2 = new Hashtable(); services2.Add(typeof(IObjectMemberImporter), memberImporter2); properties.Add(new TestPropertyDescriptor("prop2", typeof(object), services2)); // Third property added to exercise issue #27: // http://code.google.com/p/jayrock/issues/detail?id=27 ArrayList calls3 = new ArrayList(); TestObjectMemberImporter memberImporter3 = new TestObjectMemberImporter(calls3); Hashtable services3 = new Hashtable(); services3.Add(typeof(IObjectMemberImporter), memberImporter3); properties.Add(new TestPropertyDescriptor("prop3", typeof(object), services3)); ComponentImporter importer = new ComponentImporter(typeof(Thing), logicalType); ImportContext context = new ImportContext(); context.Register(importer); JsonRecorder writer = new JsonRecorder(); writer.WriteStartObject(); writer.WriteMember("prop1"); writer.WriteString("value1"); writer.WriteMember("prop2"); writer.WriteString("value2"); writer.WriteMember("prop3"); writer.WriteString("value3"); writer.WriteEndObject(); JsonReader reader = writer.CreatePlayer(); Thing thing = (Thing) context.Import(typeof(Thing), reader); Assert.AreEqual(1, calls2.Count); Assert.AreSame(memberImporter2, calls2[0]); Assert.AreEqual(new object[] { context, reader, thing }, memberImporter2.ImportArgs); Assert.AreEqual("value2", memberImporter2.ImportedValue); Assert.AreEqual(1, calls3.Count); Assert.AreSame(memberImporter3, calls3[0]); Assert.AreEqual(new object[] { context, reader, thing }, memberImporter3.ImportArgs); Assert.AreEqual("value3", memberImporter3.ImportedValue); }
/************************************************************************/ /* GET */ /************************************************************************/ #if TEST public PipelineDescriptor GetPipeline(string pipelineName) { PipelineDescriptor pipeline = null; ImportContext impctx = new ImportContext(); impctx.Register(new ListImporter<ModuleDescriptor>()); impctx.Register(new ListImporter<EndpointDescriptor>()); impctx.Register(new ListImporter<ConnectionDescriptor>()); switch (pipelineName) { case "root": pipeline = (PipelineDescriptor)impctx.Import( typeof(PipelineDescriptor), JsonText.CreateReader(test_RootPipelineJSON)); break; case "pipeline1": pipeline = (PipelineDescriptor)impctx.Import( typeof(PipelineDescriptor), JsonText.CreateReader(test_Pipeline1JSON)); break; } return pipeline; }
public void MemberImportCustomization() { ArrayList calls = new ArrayList(); TestTypeDescriptor logicalType = new TestTypeDescriptor(); PropertyDescriptorCollection properties = logicalType.GetProperties(); properties.Add(new TestPropertyDescriptor("prop1", typeof(object), new Hashtable())); TestObjectMemberImporter memberImporter = new TestObjectMemberImporter(calls); Hashtable services = new Hashtable(); services.Add(typeof(IObjectMemberImporter), memberImporter); properties.Add(new TestPropertyDescriptor("prop2", typeof(object), services)); ComponentImporter importer = new ComponentImporter(typeof(Thing), logicalType); ImportContext context = new ImportContext(); context.Register(importer); JsonRecorder writer = new JsonRecorder(); writer.WriteStartObject(); writer.WriteMember("prop1"); writer.WriteString("value1"); writer.WriteMember("prop2"); writer.WriteString("value2"); writer.WriteEndObject(); JsonReader reader = writer.CreatePlayer(); Thing thing = (Thing) context.Import(typeof(Thing), reader); Assert.AreEqual(1, calls.Count); Assert.AreSame(memberImporter, calls[0]); Assert.AreEqual(new object[] { context, reader, thing }, memberImporter.ImportArgs); Assert.AreEqual("value2", memberImporter.ImportedValue); }
//! move out of current pipeline public PipelineDescriptor NavigatePop() { ImportContext impctx = new ImportContext(); impctx.Register(new ListImporter<ModuleDescriptor>()); impctx.Register(new ListImporter<ConnectionDescriptor>()); impctx.Register(new ListImporter<EndpointDescriptor>()); PipelineDescriptor newPipeline = (PipelineDescriptor)impctx.Import( typeof(PipelineDescriptor), JsonText.CreateReader(test_RootPipelineJSON)); NuiState.Instance.level--; currentPipeline = newPipeline; return newPipeline; }
private static void Run() { ImportContext impctx = new ImportContext(); // // Import a strongly-typed collection of integers... // impctx.Register(new ListImporter<int>()); List<int> numbers = (List<int>) impctx.Import(typeof(List<int>), JsonText.CreateReader("[ 1, 2, 3 ]")); numbers.ForEach(Console.WriteLine); Console.WriteLine(); // // Import a Shape object containing a strongly-typed collection of // Point objects. // impctx.Register(new ListImporter<Point>()); Shape shape = (Shape) impctx.Import(typeof(Shape), JsonText.CreateReader(@"{ name: 'square', points: [ { x: 10, y: 10 }, { x: 20, y: 10 }, { x: 20, y: 20 }, { x: 10, y: 20 } ] }")); JsonConvert.Export(shape, CreatePrettyWriter(Console.Out)); Console.WriteLine(); // // Import CookieCollection using duck-typing. In other words, // as long as CookieCollection walks and quacks like a // collection of Cookie elements then it's good enough for // DuckCollectionImporter. DuckCollectionImporter can infer // that CookieCollection contains Cookie elements. // impctx.Register(new DuckCollectionImporter(typeof(CookieCollection))); const string cookiesText = @"[ { name: 'one', value: 1, expires: '2099-01-02' }, { name: 'two', value: 2, expires: '2088-03-04' }, { name: 'three', value: 3, expires: '2077-05-06' } ]"; CookieCollection cookies = (CookieCollection) impctx.Import(typeof(CookieCollection), JsonText.CreateReader(cookiesText)); JsonConvert.Export(cookies, CreatePrettyWriter(Console.Out)); Console.WriteLine(); // // Now repeat, but replace with a new CookieCollection importer // that is identical in behavior but based on generics. Here, // the difference is that DuckCollectionImporter<,> does not // have to guess the element type as it is provided as a type // argument. // impctx.Register(new DuckCollectionImporter<CookieCollection, Cookie>()); cookies = (CookieCollection)impctx.Import(typeof(CookieCollection), JsonText.CreateReader(cookiesText)); JsonConvert.Export(cookies, CreatePrettyWriter(Console.Out)); Console.WriteLine(); // // Those Cookie objects have a lot of properties. Say our JSON // text only needs a subset. Here, we register an exporter that // provides a custom view of the type. We only expose the name, // value and expiration time. What's more, we rename the // Expires property so that it appears as "expiration" in JSON. // ExportContext expctx = new ExportContext(); JsonType. BuildFor(typeof(Cookie)). AddProperty("Name"). AddProperty("Value"). AddProperty("Expires").As("expiration"). Register(expctx); expctx.Export(cookies, CreatePrettyWriter(Console.Out)); Console.WriteLine(); }
public void MemberImportCustomization() { TestObjectMemberImporter memberImporter = new TestObjectMemberImporter(); Hashtable services = new Hashtable(); services.Add(typeof(IObjectMemberImporter), memberImporter); TestTypeDescriptor logicalType = new TestTypeDescriptor(); PropertyDescriptorCollection properties = logicalType.GetProperties(); TestPropertyDescriptor property = new TestPropertyDescriptor("prop", typeof(object), services); properties.Add(property); ComponentImporter importer = new ComponentImporter(typeof(Thing), logicalType); ImportContext context = new ImportContext(); context.Register(importer); JsonRecorder writer = new JsonRecorder(); writer.WriteStartObject(); writer.WriteMember("prop"); writer.WriteString("value"); writer.WriteEndObject(); JsonReader reader = writer.CreatePlayer(); Thing thing = (Thing) context.Import(typeof(Thing), reader); Assert.AreSame(context, memberImporter.ImportContext); Assert.AreSame(reader, memberImporter.ImportReader); Assert.AreSame(thing, memberImporter.ImportTarget); Assert.AreEqual("value", memberImporter.ImportedValue); }
public Builder Register(ImportContext context) { if (context == null) throw new ArgumentNullException("context"); context.Register(new ComponentImporter(_type, ToCustomType())); return this; }
/************************************************************************/ /* NAVIGATE */ /************************************************************************/ //! move into next pipeline #if TEST public PipelineDescriptor NavigatePush(int moduleIdx) { ImportContext impctx = new ImportContext(); impctx.Register(new ListImporter<ModuleDescriptor>()); impctx.Register(new ListImporter<ConnectionDescriptor>()); impctx.Register(new ListImporter<EndpointDescriptor>()); PipelineDescriptor newPipeline = ((Response<PipelineDescriptor>)impctx.Import( typeof(Response<PipelineDescriptor>), JsonText.CreateReader(test_Pipeline1JSON))).GetResponse(); NuiState.Instance.level++; currentPipeline = newPipeline; return newPipeline; }