Example #1
0
        public static bool TryReadUInt16(ref ReadOnlySpan <byte> remaining, out ushort result, char standardFormat)
        {
            result = default;

            if (standardFormat != '\0')
            {
                if (!TryReadUtf8Bytes(ref remaining, out var bytes))
                {
                    return(false);
                }
                return(Utf8Reader.TryReadUInt16(ref bytes, out result, standardFormat));
            }

            result = default;
            if (!TryReadUInt64(ref remaining, out ulong longResult, standardFormat))
            {
                return(false);
            }
            if (longResult > ushort.MaxValue)
            {
                return(false);
            }
            result = (ushort)longResult;
            return(true);
        }
        public static bool TryReadUInt16(ref ReadOnlySpan <byte> remaining, out ushort result, char standardFormat)
        {
            result = default;

            switch (GetTokenType(ref remaining))
            {
            case JsonTokenType.Number:
                if (!Utf8Reader.TryReadUInt16(ref remaining, out result, JsonSerializer.IntFormat.Symbol))
                {
                    return(false);
                }
                return(true);

            case JsonTokenType.String:
                remaining = remaining.Slice(1);
                if (!Utf8Reader.TryReadUInt16(ref remaining, out result, standardFormat))
                {
                    return(false);
                }
                if (remaining.Length == 0 || remaining[0] != '"')
                {
                    return(false);
                }
                remaining = remaining.Slice(1);
                return(true);
            }
            return(false);
        }