private static string Format(object o)
        {
            ComponentFormatter componentFormatter = new ComponentFormatter();

            CompositeFormatter compositeFormatter = new CompositeFormatter();
            compositeFormatter.AddFormatter(typeof(Car), componentFormatter);
            compositeFormatter.AddFormatter(typeof(Person), componentFormatter);

            JsonTextWriter writer = new JsonTextWriter();
            writer.ValueFormatter = compositeFormatter;

            writer.WriteValue(o);
            return writer.ToString();
        }
 public void WideSelection()
 {
     CompositeFormatter compositeFormatter = new CompositeFormatter();
     StubFormatter formatter = new StubFormatter();
     compositeFormatter.AddFormatter(typeof(IList), formatter, true);
     Assert.IsNull(compositeFormatter.SelectFormatter(typeof(Guid)));
     Assert.AreSame(formatter, compositeFormatter.SelectFormatter(typeof(ArrayList)));
     Assert.AreSame(formatter, compositeFormatter.SelectFormatter(typeof(int[])));
 }
 public void ExactSelection()
 {
     JsonTextWriter writer = new JsonTextWriter();
     CompositeFormatter compositeFormatter = new CompositeFormatter();
     compositeFormatter.AddFormatter(typeof(object), new TestFormatter());
     IJsonFormatter formatter = compositeFormatter.SelectFormatter(typeof(object));
     formatter.Format(new object(), writer);
     Assert.AreEqual("\"(object)\"", writer.ToString());
 }
        private static JsonReader FormatForReading(object o)
        {
            CompositeFormatter compositeFormatter = new CompositeFormatter();
            compositeFormatter.AddFormatter(typeof(Car), new ComponentFormatter());
            compositeFormatter.AddFormatter(typeof(Person), new ComponentFormatter());
            compositeFormatter.AddFormatter(typeof(OwnerCars), new ComponentFormatter());

            JsonTextWriter writer = new JsonTextWriter();
            writer.ValueFormatter = compositeFormatter;

            writer.WriteValue(o);
            return new JsonTextReader(new StringReader(writer.ToString()));
        }
        protected virtual void WriteResponse(object response, TextWriter output)
        {
            if (response == null)
                throw new ArgumentNullException("response");

            if (output == null)
                throw new ArgumentNullException("output");
            
            JsonWriter writer = (JsonWriter) _serviceProvider.GetService(typeof(JsonWriter));
            
            if (writer == null)
            {
                CompositeFormatter formatter = new CompositeFormatter();

                formatter.AddFormatter(typeof(DateTime), new DateTimeFormatter());
                formatter.AddFormatter(typeof(DataSet), new DataSetFormatter(), true);
                formatter.AddFormatter(typeof(DataTable), new DataTableFormatter(), true);
                formatter.AddFormatter(typeof(DataView), new DataViewFormatter(), true);
                formatter.AddFormatter(typeof(DataRowView), new DataRowViewFormatter(), true);
                formatter.AddFormatter(typeof(DataRow), new DataRowFormatter(), true);
                formatter.AddFormatter(typeof(NameValueCollection), new NameValueCollectionFormatter(), true);
                formatter.AddFormatter(typeof(Control), new ControlFormatter(), true);

                writer = new JsonTextWriter(output);
                writer.ValueFormatter = formatter;
            }

            writer.WriteValue(response);
        }
        protected virtual void WriteResponse(object response, TextWriter output)
        {
            if (response == null)
                throw new ArgumentNullException("response");

            if (output == null)
                throw new ArgumentNullException("output");
            
            JsonWriter writer = null;
            IJsonTextService textService = (IJsonTextService) ServiceQuery.FindByType(_serviceProvider, typeof(IJsonTextService));
            
            if (textService != null)
                writer = textService.CreateWriter(output);

            if (writer == null)
            {
                writer = JsonText.CreateWriter(output);

                CompositeFormatter formatter = new CompositeFormatter();

                formatter.AddFormatter(typeof(DateTime), new DateTimeFormatter());
                formatter.AddFormatter(typeof(DataSet), new DataSetFormatter());
                formatter.AddFormatter(typeof(DataTable), new DataTableFormatter());
                formatter.AddFormatter(typeof(DataView), new DataViewFormatter());
                formatter.AddFormatter(typeof(DataRowView), new DataRowViewFormatter());
                formatter.AddFormatter(typeof(DataRow), new DataRowFormatter());
                formatter.AddFormatter(typeof(NameValueCollection), new NameValueCollectionFormatter(), true);
                formatter.AddFormatter(typeof(Control), new ControlFormatter(), true);

                writer.ValueFormatter = formatter;
            }

            writer.WriteValue(response);
        }