Пример #1
0
 public JsonWriter(Stream stream, FormattingData.Encoding encoding, bool prettyPrint = false)
 {
     _wroteListItem = false;
     _prettyPrint   = prettyPrint;
     _indent        = 0;
     _formatter     = new StreamFormatter(stream, encoding == FormattingData.Encoding.Utf16 ? FormattingData.InvariantUtf16 : FormattingData.InvariantUtf8);
 }
Пример #2
0
 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);
 }
Пример #3
0
        public static bool TryParse(ReadOnlySpan <byte> text, FormattingData.Encoding encoding, out uint value, out int bytesConsumed)
        {
            Precondition.Require(text.Length > 0);
            Precondition.Require(encoding == FormattingData.Encoding.Utf8 || text.Length > 1);

            value         = 0;
            bytesConsumed = 0;

            if (text[0] == '0')
            {
                if (encoding == FormattingData.Encoding.Utf16)
                {
                    bytesConsumed = 2;
                    return(text[1] == 0);
                }
                bytesConsumed = 1;
                return(true);
            }

            for (int byteIndex = 0; byteIndex < text.Length; byteIndex++)
            {
                byte nextByte = text[byteIndex];
                if (nextByte < '0' || nextByte > '9')
                {
                    if (bytesConsumed == 0)
                    {
                        value = default(uint);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                uint candidate = value * 10;
                candidate += (uint)nextByte - '0';
                if (candidate > value)
                {
                    value = candidate;
                }
                else
                {
                    return(true);
                }
                bytesConsumed++;
                if (encoding == FormattingData.Encoding.Utf16)
                {
                    byteIndex++;
                    if (byteIndex >= text.Length || text[byteIndex] != 0)
                    {
                        return(false);
                    }
                    bytesConsumed++;
                }
            }

            return(true);
        }