private ComponentExporter(Type inputType, PropertyDescriptorCollection properties) : 
            base(inputType)
        {
            Debug.Assert(properties != null);
            
            _properties = properties;

            int index = 0;
            int count = 0;
            IObjectMemberExporter[] exporters = new IObjectMemberExporter[properties.Count];

            foreach (PropertyDescriptor property in properties)
            {
                IServiceProvider sp = property as IServiceProvider;
                
                if (sp == null)
                    continue;
                
                IObjectMemberExporter exporter = (IObjectMemberExporter) sp.GetService(typeof(IObjectMemberExporter));
                
                if (exporter == null)
                    continue;
                
                exporters[index++] = exporter;
                count++;
            }
            
            if (count > 0)
                _exporters = exporters;
        }
        public void PropertyDescriptorCustomizedAsJsonObjectMemberExporter()
        {
            TestPropertyDescriptor    property  = new TestPropertyDescriptor("prop", 42);
            JsonDefaultValueAttribute attribute = new JsonDefaultValueAttribute(42);
            IServiceProvider          sp        = property;

            Assert.IsNull(sp.GetService(typeof(IObjectMemberExporter)));
            IPropertyDescriptorCustomization customization = attribute;

            customization.Apply(property);
            IObjectMemberExporter exporter = (IObjectMemberExporter)sp.GetService(typeof(IObjectMemberExporter));

            Assert.IsNotNull(exporter);
        }
        public void DoesNotExportPropertyValueWhenEqualsSpecfiedDefault()
        {
            IObjectMemberExporter exporter = CreatePropertyExporter("prop", 0, 0);

            ExportContext context = new ExportContext();
            JsonRecorder  writer  = new JsonRecorder();

            writer.WriteStartObject();
            exporter.Export(context, writer, new object());
            writer.WriteEndObject();

            JsonReader reader = writer.CreatePlayer();

            reader.ReadToken(JsonTokenClass.Object);
            reader.ReadToken(JsonTokenClass.EndObject);
        }
        public void ExportsPropertyValueWhenNotEqualsSpecfiedDefault()
        {
            const string          propertyName = "prop";
            IObjectMemberExporter exporter     = CreatePropertyExporter(propertyName, 42, 0);

            ExportContext context = new ExportContext();
            JsonRecorder  writer  = new JsonRecorder();

            writer.WriteStartObject();
            exporter.Export(context, writer, new object());
            writer.WriteEndObject();

            JsonReader reader = writer.CreatePlayer();

            reader.ReadToken(JsonTokenClass.Object);
            Assert.AreEqual(propertyName, reader.ReadMember());
            Assert.AreEqual(42, reader.ReadNumber().ToInt32());
        }
Exemplo n.º 5
0
        private ComponentExporter(Type inputType, PropertyDescriptorCollection properties) :
            base(inputType)
        {
            Debug.Assert(properties != null);

            _properties = properties;

            int count = 0;

            IObjectMemberExporter[] exporters = null;

            for (int i = 0; i < properties.Count; i++)
            {
                IServiceProvider sp = properties[i] as IServiceProvider;

                if (sp == null)
                {
                    continue;
                }

                IObjectMemberExporter exporter = (IObjectMemberExporter)sp.GetService(typeof(IObjectMemberExporter));

                if (exporter == null)
                {
                    continue;
                }

                if (exporters == null) // fault
                {
                    exporters = new IObjectMemberExporter[properties.Count];
                }

                exporters[i] = exporter;
                count++;
            }

            if (count > 0)
            {
                _exporters = exporters;
            }
        }
Exemplo n.º 6
0
 private ComponentExporter(Type inputType, PropertyDescriptorCollection properties) : base(inputType)
 {
     this._properties = properties;
     int num = 0;
     IObjectMemberExporter[] exporterArray = new IObjectMemberExporter[properties.Count];
     for (int i = 0; i < properties.Count; i++)
     {
         IServiceProvider provider = properties[i] as IServiceProvider;
         if (provider != null)
         {
             IObjectMemberExporter service = (IObjectMemberExporter) provider.GetService(typeof(IObjectMemberExporter));
             if (service != null)
             {
                 exporterArray[i] = service;
                 num++;
             }
         }
     }
     if (num > 0)
     {
         this._exporters = exporterArray;
     }
 }
Exemplo n.º 7
0
        protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(value != null);
            Debug.Assert(writer != null);

            if (_properties.Count == 0)
            {
                writer.WriteString(value.ToString());
            }
            else
            {
                ObjectReferenceTracker tracker = null;

                try
                {
                    writer.WriteStartObject();

                    int index = 0;

                    foreach (PropertyDescriptor property in _properties)
                    {
                        IObjectMemberExporter exporter = _exporters != null && index < _exporters.Length ?
                                                         _exporters[index++] : null;

                        if (exporter != null)
                        {
                            exporter.Export(context, writer, value);
                        }
                        else
                        {
                            object propertyValue = property.GetValue(value);

                            if (!JsonNull.LogicallyEquals(propertyValue))
                            {
                                writer.WriteMember(property.Name);

                                if (value.GetType().IsClass&& tracker == null)
                                {
                                    //
                                    // We are about to enter a deeper scope so
                                    // start tracking the current object being
                                    // exported. This will help to detect
                                    // recursive references that may occur
                                    // through this exporter deeper in the tree.
                                    //

                                    tracker = TrackObject(context, value);
                                }

                                context.Export(propertyValue, writer);
                            }
                        }
                    }

                    writer.WriteEndObject();
                }
                finally
                {
                    if (tracker != null)
                    {
                        tracker.Pop(value);
                    }
                }
            }
        }