public JsonWriter(Stream stream, FormattingData.Encoding encoding, bool prettyPrint = false)
 {
     _wroteListItem = false;
     _prettyPrint   = prettyPrint;
     _indent        = 0;
     _pool          = new ManagedBufferPool <byte>(2048);
     _formatter     = new StreamFormatter(stream, encoding == FormattingData.Encoding.Utf16 ? FormattingData.InvariantUtf16 : FormattingData.InvariantUtf8, _pool);
 }
Beispiel #2
0
        public void CustomTypeToString()
        {
            ManagedBufferPool <byte> pool = new ManagedBufferPool <byte>(1024);
            var sb = new StringFormatter(pool);

            sb.Append(new Age(56));
            sb.Append(new Age(14, inMonths: true));
            Assert.Equal(sb.ToString(), "56y14m");
        }
Beispiel #3
0
        public void CompositeFormattingFormatStrings()
        {
            var pool      = new ManagedBufferPool <byte>(1024);
            var formatter = new StringFormatter(pool);

            formatter.Format("Hello{0:x}{1:X}{2:G}", 10, 255, 3);

            Assert.Equal("HelloaFF3", formatter.ToString());
        }
Beispiel #4
0
        public void CompositeFormattingBasics()
        {
            var time      = DateTime.UtcNow;
            var pool      = new ManagedBufferPool <byte>(1024);
            var formatter = new StringFormatter(pool);

            formatter.Format("{2} - Error {0}. File {1} not found.", 404, "index.html", time);

            Assert.Equal(time.ToString("G") + " - Error 404. File index.html not found.", formatter.ToString());
        }
Beispiel #5
0
        public void CustomCulture()
        {
            ManagedBufferPool <byte> pool = new ManagedBufferPool <byte>(1024);
            var sb = new StringFormatter(pool);

            sb.FormattingData = Culture5;

            sb.Append(-1234567890);
            Assert.Equal("_?BBBBBCCCCCDDDDDEEEEEFFFFFGGGGGHHHHHIIIIIJJJJJAAAAA", sb.ToString());
        }
        public void CustomCulture()
        {
            var pool = new ManagedBufferPool <byte>(1024);
            var sb   = new StringFormatter(pool);

            sb.FormattingData = FormattingDataProvider.CreateFormattingData("pl-PL");

            sb.Append(-10000, Format.Parse('N'));
            Assert.Equal("-10\u00A0000,00", sb.ToString()); // \u00A0 is a space group separator
        }
Beispiel #7
0
 public BufferFormatter(int capacity, FormattingData formattingData, ManagedBufferPool <byte> pool = null)
 {
     _formattingData = formattingData;
     _count          = 0;
     _pool           = pool;
     if (_pool == null)
     {
         _pool = new ManagedBufferPool <byte>(capacity);
     }
     _buffer = _pool.RentBuffer(capacity);
 }
Beispiel #8
0
 public StreamFormatter(Stream stream, FormattingData formattingData, ManagedBufferPool <byte> pool, int bufferSize = 256)
 {
     _pool   = pool;
     _buffer = null;
     if (bufferSize > 0)
     {
         _buffer = _pool.RentBuffer(bufferSize);
     }
     _formattingData = formattingData;
     _stream         = stream;
 }
Beispiel #9
0
        public void CustomTypeToStreamUtf8()
        {
            byte[]                   buffer = new byte[1024];
            MemoryStream             stream = new MemoryStream(buffer);
            ManagedBufferPool <byte> pool   = new ManagedBufferPool <byte>(1024);

            using (var writer = new StreamFormatter(stream, FormattingData.InvariantUtf8, pool)) {
                writer.Append(new Age(56));
                writer.Append(new Age(14, inMonths: true));
                var writtenText = Encoding.UTF8.GetString(buffer, 0, (int)stream.Position);
                Assert.Equal(writtenText, "56y14m");
            }
        }
        private void InvariantFormatStruct()
        {
            ManagedBufferPool <byte> pool = new ManagedBufferPool <byte>(1024);

            timer.Restart();
            for (int itteration = 0; itteration < itterationsInvariant; itteration++)
            {
                StringFormatter sb = new StringFormatter(numbersToWrite * 2, pool);
                for (int i = 0; i < numbersToWrite; i++)
                {
                    sb.Append(new Age(i % 10));
                }
                var text = sb.ToString();
                if (text.Length != numbersToWrite * 2)
                {
                    throw new Exception("test failed");
                }
            }
            PrintTime();
        }
        private void InvariantFormatIntHex()
        {
            ManagedBufferPool <byte> pool = new ManagedBufferPool <byte>(1024);

            timer.Restart();
            for (int itteration = 0; itteration < itterationsInvariant; itteration++)
            {
                StringFormatter sb = new StringFormatter(numbersToWrite, pool);
                for (int i = 0; i < numbersToWrite; i++)
                {
                    sb.Append(((int)(i % 10)), Format.Parsed.HexUppercase);
                }
                var text = sb.ToString();
                if (text.Length != numbersToWrite)
                {
                    throw new Exception("test failed");
                }
            }
            PrintTime();
        }
        private void EncodeStringToUtf8()
        {
            string text                        = "Hello World!";
            int    stringsToWrite              = 2000;
            int    size                        = stringsToWrite * text.Length + stringsToWrite;
            ManagedBufferPool <byte> pool      = new ManagedBufferPool <byte>(100000);
            BufferFormatter          formatter = new BufferFormatter(size, FormattingData.InvariantUtf8, pool);

            timer.Restart();
            for (int itteration = 0; itteration < itterationsInvariant; itteration++)
            {
                formatter.Clear();
                for (int i = 0; i < stringsToWrite; i++)
                {
                    formatter.Append(text);
                    formatter.Append(1);
                }
                Assert.Equal(size, formatter.CommitedByteCount);
            }
            PrintTime();
        }
        private void FormatGuid()
        {
            ManagedBufferPool <byte> pool = new ManagedBufferPool <byte>(1024);
            var guid         = Guid.NewGuid();
            var guidsToWrite = numbersToWrite / 10;

            timer.Restart();
            for (int itteration = 0; itteration < itterationsInvariant; itteration++)
            {
                StringFormatter sb = new StringFormatter(guidsToWrite * 36, pool);
                for (int i = 0; i < guidsToWrite; i++)
                {
                    sb.Append(guid);
                }
                var text = sb.ToString();
                if (text.Length != guidsToWrite * 36)
                {
                    throw new Exception("test failed");
                }
            }
            PrintTime();
        }
        private void CustomCultureFormat()
        {
            ManagedBufferPool <byte> pool = new ManagedBufferPool <byte>(1024);
            StringFormatter          sb   = new StringFormatter(numbersToWrite * 3, pool);

            sb.FormattingData = CreateCustomCulture();

            timer.Restart();
            for (int itteration = 0; itteration < itterationsCulture; itteration++)
            {
                sb.Clear();
                for (int i = 0; i < numbersToWrite; i++)
                {
                    var next = (i % 128) + 101;
                    sb.Append(next);
                }
                var text = sb.ToString();
                if (text.Length != numbersToWrite * 3)
                {
                    throw new Exception("test failed");
                }
            }
            PrintTime();
        }
 public StringFormatter(int capacity, ManagedBufferPool <byte> pool)
 {
     _pool   = pool;
     _buffer = _pool.RentBuffer(capacity * 2);
 }
 public StringFormatter(ManagedBufferPool <byte> pool) : this(64, pool)
 {
     Clear();
 }
Beispiel #17
0
 public StreamFormatter(Stream stream, ManagedBufferPool <byte> pool) : this(stream, FormattingData.InvariantUtf16, pool)
 {
 }