public void Should_read_serialized_object_from_stream_using_date_handler()
        {
            var formatter = new ServiceStackTextFormatter(JsonDateHandler.DCJSCompatible);
            var value = GetTestObject();
            var utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

            JsConfig.DateHandler = JsonDateHandler.DCJSCompatible;
            byte[] data = utf8Encoding.GetBytes(value.ToJson());
            JsConfig.Reset();

            var memoryStream = new MemoryStream(data);

            var contentHeader = new StringContent(string.Empty).Headers;
            contentHeader.Clear();

            var resultTask = formatter.ReadFromStreamAsync(typeof(RootClass), memoryStream, contentHeader, new FormatterContext(StandardMediaTypeHeaderValues.ApplicationJson, isRead: false));

            resultTask.Wait();

            resultTask.Result.ShouldBeType<RootClass>();

            var result = (RootClass)resultTask.Result;

            result.StringProperty.ShouldEqual(value.StringProperty);
            result.DateProperty.ShouldEqual(value.DateProperty);
            result.Child.BooleanProperty.ShouldEqual(value.Child.BooleanProperty);
            result.Child.DecimalProperty.ShouldEqual(value.Child.DecimalProperty);
            result.Child.DoubleProperty.ShouldEqual(value.Child.DoubleProperty);
            result.Child.IntegerProperty.ShouldEqual(value.Child.IntegerProperty);
            result.Child.StringProperty.ShouldEqual(value.Child.StringProperty);
        }
        public void Should_read_serialized_object_from_stream()
        {
            var formatter = new ServiceStackTextFormatter();
            var value = GetTestObject();
            var utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

            //Media Type Formatter uses ISO8601 date formatting by default;
            using (var scope = JsConfig.BeginScope())
            {
                scope.DateHandler = JsonDateHandler.ISO8601;
                byte[] data = utf8Encoding.GetBytes(value.ToJson());
                var memoryStream = new MemoryStream(data);

                var content = new StringContent(string.Empty);

                var resultTask = formatter.ReadFromStreamAsync(typeof (RootClass), memoryStream, content, null);

                resultTask.Wait();

                resultTask.Result.ShouldBeType<RootClass>();

                var result = (RootClass) resultTask.Result;

                result.StringProperty.ShouldEqual(value.StringProperty);
                result.DateProperty.ShouldEqual(value.DateProperty);
                result.Child.BooleanProperty.ShouldEqual(value.Child.BooleanProperty);
                result.Child.DecimalProperty.ShouldEqual(value.Child.DecimalProperty);
                result.Child.DoubleProperty.ShouldEqual(value.Child.DoubleProperty);
                result.Child.IntegerProperty.ShouldEqual(value.Child.IntegerProperty);
                result.Child.StringProperty.ShouldEqual(value.Child.StringProperty);
            }
        }
        public void Should_support_only_json_media_type()
        {
            var formatter = new ServiceStackTextFormatter();

            formatter.SupportedMediaTypes.Count.ShouldEqual(1);
            formatter.SupportedMediaTypes.ShouldContain(new MediaTypeHeaderValue("application/json"));
        }
        public void Should_write_serialized_object_to_stream_using_date_handler()
        {
            var formatter = new ServiceStackTextFormatter(JsonDateHandler.TimestampOffset);
            var value = GetTestObject();

            var content = new StringContent(string.Empty);
            var memoryStream = new MemoryStream();

            var resultTask = formatter.WriteToStreamAsync(typeof(RootClass), value, memoryStream, content, transportContext: null);

            resultTask.Wait();

            memoryStream.Position = 0;
            string serializedString = new StreamReader(memoryStream).ReadToEnd();

            JsConfig.DateHandler = JsonDateHandler.TimestampOffset;
            var expected = value.ToJson();
            JsConfig.Reset();
            serializedString.ShouldEqual(expected);
        }
        public void Should_write_serialized_object_to_stream()
        {
            var formatter = new ServiceStackTextFormatter();
            var value = GetTestObject();

            var content = new StringContent(string.Empty);
            var memoryStream = new MemoryStream();

            var resultTask = formatter.WriteToStreamAsync(typeof(RootClass), value, memoryStream, content, transportContext: null);

            resultTask.Wait();

            memoryStream.Position = 0;
            string serializedString = new StreamReader(memoryStream).ReadToEnd();

            // Formatter uses ISO8601 dates by default
            using (var scope = JsConfig.BeginScope())
            {
                scope.DateHandler = JsonDateHandler.ISO8601;
                var expextedResult = value.ToJson();
                serializedString.ShouldEqual(expextedResult);    
            }
        }
        public void Should_write_serialized_object_to_stream()
        {
            var formatter = new ServiceStackTextFormatter();
            var value = GetTestObject();

            var contentHeader = new StringContent(string.Empty).Headers;
            contentHeader.Clear();
            var memoryStream = new MemoryStream();

            var resultTask = formatter.WriteToStreamAsync(typeof(RootClass), value, memoryStream, contentHeader, new FormatterContext(StandardMediaTypeHeaderValues.ApplicationJson, isRead: false), transportContext: null);

            resultTask.Wait();

            memoryStream.Position = 0;
            string serializedString = new StreamReader(memoryStream).ReadToEnd();

            // Formatter uses ISO8601 dates by default
            JsConfig.DateHandler = JsonDateHandler.ISO8601;
            var expextedResult = value.ToJson();
            JsConfig.Reset();
            serializedString.ShouldEqual(expextedResult);
        }