internal static bool TryParseDateTimeOffsetIso(StringReference text, out DateTimeOffset dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default(DateTimeOffset); return(false); } DateTime d = CreateDateTime(dateTimeParser); TimeSpan offset; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: offset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: offset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: offset = TimeZoneInfo.Local.GetUtcOffset(d); break; } long ticks = d.Ticks - offset.Ticks; if (ticks < 0 || ticks > 3155378975999999999) { dt = default(DateTimeOffset); return(false); } dt = new DateTimeOffset(d, offset); return(true); }
public static bool StartsWith(this StringReference s, string text) { if (text.Length > s.Length) { return(false); } char[] chars = s.Chars; for (int i = 0; i < text.Length; i++) { if (text[i] != chars[i + s.StartIndex]) { return(false); } } return(true); }
internal static bool TryParseDateTimeOffset( StringReference s, string?dateFormatString, CultureInfo culture, out DateTimeOffset dt ) { if (s.Length > 0) { int i = s.StartIndex; if (s[i] == '/') { if (s.Length >= 9 && s.StartsWith("/Date(") && s.EndsWith(")/")) { if (TryParseDateTimeOffsetMicrosoft(s, out dt)) { return(true); } } } else if (s.Length >= 19 && s.Length <= 40 && char.IsDigit(s[i]) && s[i + 10] == 'T') { if (TryParseDateTimeOffsetIso(s, out dt)) { return(true); } } if (!StringUtils.IsNullOrEmpty(dateFormatString)) { if ( TryParseDateTimeOffsetExact(s.ToString(), dateFormatString, culture, out dt) ) { return(true); } } } dt = default; return(false); }
// Token: 0x06000D1C RID: 3356 RVA: 0x0004C36C File Offset: 0x0004A56C internal static bool TryParseDateTimeOffsetIso(StringReference text, out DateTimeOffset dt) { DateTimeParser dateTimeParser = default(DateTimeParser); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default(DateTimeOffset); return(false); } DateTime dateTime = DateTimeUtils.CreateDateTime(dateTimeParser); TimeSpan utcOffset; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: utcOffset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: utcOffset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: utcOffset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime); break; } long num = dateTime.Ticks - utcOffset.Ticks; if (num < 0L || num > 3155378975999999999L) { dt = default(DateTimeOffset); return(false); } dt = new DateTimeOffset(dateTime, utcOffset); return(true); }
internal static bool TryParseDateTimeOffsetIso(StringReference text, out DateTimeOffset dt) { TimeSpan utcOffset; DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = new DateTimeOffset(); return(false); } DateTime dateTime = CreateDateTime(dateTimeParser); switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: utcOffset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: utcOffset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: utcOffset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: utcOffset = TimeZoneInfo.Local.GetUtcOffset(dateTime); break; } long num = dateTime.Ticks - utcOffset.Ticks; if ((num < 0L) || (num > 0x2bca2875f4373fffL)) { dt = new DateTimeOffset(); return(false); } dt = new DateTimeOffset(dateTime, utcOffset); return(true); }
private static bool TryReadOffset( StringReference offsetText, int startIndex, out TimeSpan offset ) { bool negative = (offsetText[startIndex] == '-'); if ( ConvertUtils.Int32TryParse(offsetText.Chars, startIndex + 1, 2, out int hours) != ParseResult.Success ) { offset = default; return(false); } int minutes = 0; if (offsetText.Length - startIndex > 5) { if ( ConvertUtils.Int32TryParse(offsetText.Chars, startIndex + 3, 2, out minutes) != ParseResult.Success ) { offset = default; return(false); } } offset = TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(minutes); if (negative) { offset = offset.Negate(); } return(true); }
private static bool TryReadOffset(StringReference offsetText, int startIndex, out TimeSpan offset) { bool flag = offsetText[startIndex] == '-'; if (ConvertUtils.Int32TryParse(offsetText.Chars, startIndex + 1, 2, out int num) != ParseResult.Success) { offset = new TimeSpan(); return(false); } int num2 = 0; if (((offsetText.Length - startIndex) > 5) && (ConvertUtils.Int32TryParse(offsetText.Chars, startIndex + 3, 2, out num2) != ParseResult.Success)) { offset = new TimeSpan(); return(false); } offset = TimeSpan.FromHours((double)num) + TimeSpan.FromMinutes((double)num2); if (flag) { offset = offset.Negate(); } return(true); }
private static bool TryParseMicrosoftDate( StringReference text, out long ticks, out TimeSpan offset, out DateTimeKind kind ) { kind = DateTimeKind.Utc; int index = text.IndexOf('+', 7, text.Length - 8); if (index == -1) { index = text.IndexOf('-', 7, text.Length - 8); } if (index != -1) { kind = DateTimeKind.Local; if (!TryReadOffset(text, index + text.StartIndex, out offset)) { ticks = 0; return(false); } } else { offset = TimeSpan.Zero; index = text.Length - 2; } return( ConvertUtils.Int64TryParse(text.Chars, 6 + text.StartIndex, index - 6, out ticks) == ParseResult.Success ); }
internal static bool TryParseDateTimeOffset(StringReference s, string dateFormatString, CultureInfo culture, out DateTimeOffset dt) { if (s.Length > 0) { int startIndex = s.StartIndex; if (s[startIndex] == '/') { if (((s.Length >= 9) && s.StartsWith("/Date(")) && (s.EndsWith(")/") && TryParseDateTimeOffsetMicrosoft(s, out dt))) { return(true); } } else if ((((s.Length >= 0x13) && (s.Length <= 40)) && (char.IsDigit(s[startIndex]) && (s[startIndex + 10] == 'T'))) && TryParseDateTimeOffsetIso(s, out dt)) { return(true); } if (!string.IsNullOrEmpty(dateFormatString) && TryParseDateTimeOffsetExact(s.ToString(), dateFormatString, culture, out dt)) { return(true); } } dt = new DateTimeOffset(); return(false); }
private static bool TryParseDateTimeMicrosoft(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { if (!TryParseMicrosoftDate(text, out long num, out _, out DateTimeKind kind)) { dt = new DateTime(); return(false); } DateTime time = ConvertJavaScriptTicksToDateTime(num); if (kind == DateTimeKind.Unspecified) { dt = DateTime.SpecifyKind(time.ToLocalTime(), DateTimeKind.Unspecified); } else if (kind == DateTimeKind.Local) { dt = time.ToLocalTime(); } else { dt = time; } dt = EnsureDateTime(dt, dateTimeZoneHandling); return(true); }
internal static bool TryParseDateTime(StringReference s, DateTimeZoneHandling dateTimeZoneHandling, string dateFormatString, CultureInfo culture, out DateTime dt) { if (s.Length > 0) { int startIndex = s.StartIndex; if (s[startIndex] == '/') { if (s.Length >= 9 && s.StartsWith("/Date(") && s.EndsWith(")/") && DateTimeUtils.TryParseDateTimeMicrosoft(s, dateTimeZoneHandling, out dt)) { return(true); } } else if (s.Length >= 19 && s.Length <= 40 && char.IsDigit(s[startIndex]) && s[startIndex + 10] == 'T' && DateTimeUtils.TryParseDateTimeIso(s, dateTimeZoneHandling, out dt)) { return(true); } if (!string.IsNullOrEmpty(dateFormatString) && DateTimeUtils.TryParseDateTimeExact(s.ToString(), dateTimeZoneHandling, dateFormatString, culture, out dt)) { return(true); } } dt = new DateTime(); return(false); }
private void ParseConstructor() { if (!this.MatchValueWithTrailingSeperator("new")) return; this.EatWhitespace(false); int startIndex = this._charPos; char c; while (true) { do { c = this._chars[this._charPos]; if ((int) c == 0) { if (this._charsUsed != this._charPos) goto label_6; } else goto label_7; } while (this.ReadData(true) != 0); break; label_7: if (char.IsLetterOrDigit(c)) ++this._charPos; else goto label_9; } throw JsonReaderException.Create((JsonReader) this, "Unexpected end while parsing constructor."); label_6: int num = this._charPos; ++this._charPos; goto label_18; label_9: if ((int) c == 13) { num = this._charPos; this.ProcessCarriageReturn(true); } else if ((int) c == 10) { num = this._charPos; this.ProcessLineFeed(); } else if (char.IsWhiteSpace(c)) { num = this._charPos; ++this._charPos; } else { if ((int) c != 40) throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("Unexpected character while parsing constructor: {0}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) c)); num = this._charPos; } label_18: this._stringReference = new StringReference(this._chars, startIndex, num - startIndex); string str = this._stringReference.ToString(); this.EatWhitespace(false); if ((int) this._chars[this._charPos] != 40) throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("Unexpected character while parsing constructor: {0}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) this._chars[this._charPos])); ++this._charPos; this.ClearRecentString(); this.SetToken(JsonToken.StartConstructor, (object) str); }
private void ReadStringIntoBuffer(char quote) { int index = this._charPos; int startIndex = this._charPos; int num1 = this._charPos; StringBuffer buffer = (StringBuffer) null; do { char ch1 = this._chars[index++]; if ((int) ch1 <= 13) { if ((int) ch1 != 0) { if ((int) ch1 != 10) { if ((int) ch1 == 13) { this._charPos = index - 1; this.ProcessCarriageReturn(true); index = this._charPos; } } else { this._charPos = index - 1; this.ProcessLineFeed(); index = this._charPos; } } else if (this._charsUsed == index - 1) { --index; if (this.ReadData(true) == 0) { this._charPos = index; throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("Unterminated string. Expected delimiter: {0}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) quote)); } } } else if ((int) ch1 != 34 && (int) ch1 != 39) { if ((int) ch1 == 92) { this._charPos = index; if (!this.EnsureChars(0, true)) { this._charPos = index; throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("Unterminated string. Expected delimiter: {0}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) quote)); } else { int writeToPosition = index - 1; char ch2 = this._chars[index]; char ch3; switch (ch2) { case 'n': ++index; ch3 = '\n'; break; case 'r': ++index; ch3 = '\r'; break; case 't': ++index; ch3 = '\t'; break; case 'u': this._charPos = index + 1; ch3 = this.ParseUnicode(); if (StringUtils.IsLowSurrogate(ch3)) ch3 = '�'; else if (StringUtils.IsHighSurrogate(ch3)) { bool flag; do { flag = false; if (this.EnsureChars(2, true) && (int) this._chars[this._charPos] == 92 && (int) this._chars[this._charPos + 1] == 117) { char writeChar = ch3; this._charPos += 2; ch3 = this.ParseUnicode(); if (!StringUtils.IsLowSurrogate(ch3)) { if (StringUtils.IsHighSurrogate(ch3)) { writeChar = '�'; flag = true; } else writeChar = '�'; } if (buffer == null) buffer = this.GetBuffer(); this.WriteCharToBuffer(buffer, writeChar, num1, writeToPosition); num1 = this._charPos; } else ch3 = '�'; } while (flag); } index = this._charPos; break; case 'b': ++index; ch3 = '\b'; break; case 'f': ++index; ch3 = '\f'; break; case '/': case '"': case '\'': ch3 = ch2; ++index; break; case '\\': ++index; ch3 = '\\'; break; default: this._charPos = index + 1; throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("Bad JSON escape sequence: {0}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) ("\\" + (object) ch2))); } if (buffer == null) buffer = this.GetBuffer(); this.WriteCharToBuffer(buffer, ch3, num1, writeToPosition); num1 = index; } } } } while ((int) this._chars[index - 1] != (int) quote); int num2 = index - 1; if (startIndex == num1) { this._stringReference = new StringReference(this._chars, startIndex, num2 - startIndex); } else { if (buffer == null) buffer = this.GetBuffer(); if (num2 > num1) buffer.Append(this._chars, num1, num2 - num1); this._stringReference = new StringReference(buffer.GetInternalBuffer(), 0, buffer.Position); } this._charPos = num2 + 1; }
private void ParseUnquotedProperty() { int startIndex = this._charPos; do { for (; (int) this._chars[this._charPos] != 0; ++this._charPos) { char c = this._chars[this._charPos]; if (!this.ValidIdentifierChar(c)) { if (!char.IsWhiteSpace(c) && (int) c != 58) throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("Invalid JavaScript property identifier character: {0}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) c)); this._stringReference = new StringReference(this._chars, startIndex, this._charPos - startIndex); return; } } if (this._charsUsed != this._charPos) goto label_5; } while (this.ReadData(true) != 0); throw JsonReaderException.Create((JsonReader) this, "Unexpected end while parsing unquoted property name."); label_5: this._stringReference = new StringReference(this._chars, startIndex, this._charPos - startIndex); }
// Token: 0x06000719 RID: 1817 // RVA: 0x00039FE8 File Offset: 0x000381E8 private void ParseNumber() { this.ShiftBufferIfNeeded(); char c = this._chars[this._charPos]; int charPos = this._charPos; this.ReadNumberIntoBuffer(); base.SetPostValueState(true); this._stringReference = new StringReference(this._chars, charPos, this._charPos - charPos); bool flag = char.IsDigit(c) && this._stringReference.Length == 1; bool flag2 = c == '0' && this._stringReference.Length > 1 && this._stringReference.Chars[this._stringReference.StartIndex + 1] != '.' && this._stringReference.Chars[this._stringReference.StartIndex + 1] != 'e' && this._stringReference.Chars[this._stringReference.StartIndex + 1] != 'E'; object value; JsonToken newToken; if (this._readType == ReadType.ReadAsInt32) { if (flag) { value = (int)(c - '0'); } else { if (flag2) { string text = this._stringReference.ToString(); try { int num = text.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt32(text, 16) : Convert.ToInt32(text, 8); value = num; goto IL_186; } catch (Exception ex) { throw JsonReaderException.Create(this, StringUtils.FormatWith("Input string '{0}' is not a valid integer.", CultureInfo.InvariantCulture, text), ex); } } int num2; ParseResult parseResult = ConvertUtils.Int32TryParse(this._stringReference.Chars, this._stringReference.StartIndex, this._stringReference.Length, out num2); if (parseResult == ParseResult.Success) { value = num2; } else { if (parseResult == ParseResult.Overflow) { throw JsonReaderException.Create(this, StringUtils.FormatWith("JSON integer {0} is too large or small for an Int32.", CultureInfo.InvariantCulture, this._stringReference.ToString())); } throw JsonReaderException.Create(this, StringUtils.FormatWith("Input string '{0}' is not a valid integer.", CultureInfo.InvariantCulture, this._stringReference.ToString())); } } IL_186: newToken = JsonToken.Integer; } else if (this._readType == ReadType.ReadAsDecimal) { if (flag) { value = c - 48m; } else { if (flag2) { string text2 = this._stringReference.ToString(); try { long value2 = text2.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(text2, 16) : Convert.ToInt64(text2, 8); value = Convert.ToDecimal(value2); goto IL_2A3; } catch (Exception ex2) { throw JsonReaderException.Create(this, StringUtils.FormatWith("Input string '{0}' is not a valid decimal.", CultureInfo.InvariantCulture, text2), ex2); } } string s = this._stringReference.ToString(); decimal num3; if (!decimal.TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out num3)) { throw JsonReaderException.Create(this, StringUtils.FormatWith("Input string '{0}' is not a valid decimal.", CultureInfo.InvariantCulture, this._stringReference.ToString())); } value = num3; } IL_2A3: newToken = JsonToken.Float; } else if (flag) { value = (long)((ulong)c - 48uL); newToken = JsonToken.Integer; } else if (flag2) { string text3 = this._stringReference.ToString(); try { value = (text3.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(text3, 16) : Convert.ToInt64(text3, 8)); } catch (Exception ex3) { throw JsonReaderException.Create(this, StringUtils.FormatWith("Input string '{0}' is not a valid number.", CultureInfo.InvariantCulture, text3), ex3); } newToken = JsonToken.Integer; } else { long num4; ParseResult parseResult2 = ConvertUtils.Int64TryParse(this._stringReference.Chars, this._stringReference.StartIndex, this._stringReference.Length, out num4); if (parseResult2 == ParseResult.Success) { value = num4; newToken = JsonToken.Integer; } else { if (parseResult2 == ParseResult.Overflow) { throw JsonReaderException.Create(this, StringUtils.FormatWith("JSON integer {0} is too large or small for an Int64.", CultureInfo.InvariantCulture, this._stringReference.ToString())); } string text4 = this._stringReference.ToString(); if (this._floatParseHandling == FloatParseHandling.Decimal) { decimal num5; if (!decimal.TryParse(text4, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out num5)) { throw JsonReaderException.Create(this, StringUtils.FormatWith("Input string '{0}' is not a valid decimal.", CultureInfo.InvariantCulture, text4)); } value = num5; } else { double num6; if (!double.TryParse(text4, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out num6)) { throw JsonReaderException.Create(this, StringUtils.FormatWith("Input string '{0}' is not a valid number.", CultureInfo.InvariantCulture, text4)); } value = num6; } newToken = JsonToken.Float; } } this.ClearRecentString(); base.SetToken(newToken, value, false); }
private void ParseComment() { ++this._charPos; if (!this.EnsureChars(1, false) || (int) this._chars[this._charPos] != 42) throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("Error parsing comment. Expected: *, got {0}.", (IFormatProvider) CultureInfo.InvariantCulture, (object) this._chars[this._charPos])); ++this._charPos; int startIndex = this._charPos; bool flag = false; while (!flag) { switch (this._chars[this._charPos]) { case '\r': this.ProcessCarriageReturn(true); continue; case '*': ++this._charPos; if (this.EnsureChars(0, true) && (int) this._chars[this._charPos] == 47) { this._stringReference = new StringReference(this._chars, startIndex, this._charPos - startIndex - 1); ++this._charPos; flag = true; continue; } else continue; case char.MinValue: if (this._charsUsed == this._charPos) { if (this.ReadData(true) == 0) throw JsonReaderException.Create((JsonReader) this, "Unexpected end while parsing comment."); else continue; } else { ++this._charPos; continue; } case '\n': this.ProcessLineFeed(); continue; default: ++this._charPos; continue; } } this.SetToken(JsonToken.Comment, (object) this._stringReference.ToString()); this.ClearRecentString(); }
private void ReadStringIntoBuffer(char quote) { int charPos = _charPos; int initialPosition = _charPos; int lastWritePosition = _charPos; StringBuffer buffer = null; while (true) { switch (_chars[charPos++]) { case '\0': if (_charsUsed == charPos - 1) { charPos--; if (ReadData(true) == 0) { _charPos = charPos; throw CreateReaderException(this, "Unterminated string. Expected delimiter: {0}.".FormatWith(CultureInfo.InvariantCulture, quote)); } } break; case '\\': _charPos = charPos; if (!EnsureChars(0, true)) { _charPos = charPos; throw CreateReaderException(this, "Unterminated string. Expected delimiter: {0}.".FormatWith(CultureInfo.InvariantCulture, quote)); } // start of escape sequence int escapeStartPos = charPos - 1; char currentChar = _chars[charPos]; char writeChar; switch (currentChar) { case 'b': charPos++; writeChar = '\b'; break; case 't': charPos++; writeChar = '\t'; break; case 'n': charPos++; writeChar = '\n'; break; case 'f': charPos++; writeChar = '\f'; break; case 'r': charPos++; writeChar = '\r'; break; case '\\': charPos++; writeChar = '\\'; break; case '"': case '\'': case '/': writeChar = currentChar; charPos++; break; case 'u': charPos++; _charPos = charPos; if (EnsureChars(4, true)) { string hexValues = new string(_chars, charPos, 4); char hexChar = Convert.ToChar(int.Parse(hexValues, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo)); writeChar = hexChar; charPos += 4; } else { _charPos = charPos; throw CreateReaderException(this, "Unexpected end while parsing unicode character."); } break; default: charPos++; _charPos = charPos; throw CreateReaderException(this, "Bad JSON escape sequence: {0}.".FormatWith(CultureInfo.InvariantCulture, @"\" + currentChar)); } if (buffer == null) buffer = GetBuffer(); if (escapeStartPos > lastWritePosition) { buffer.Append(_chars, lastWritePosition, escapeStartPos - lastWritePosition); } buffer.Append(writeChar); lastWritePosition = charPos; break; case StringUtils.CarriageReturn: _charPos = charPos - 1; ProcessCarriageReturn(true); charPos = _charPos; break; case StringUtils.LineFeed: _charPos = charPos - 1; ProcessLineFeed(); charPos = _charPos; break; case '"': case '\'': if (_chars[charPos - 1] == quote) { charPos--; if (initialPosition == lastWritePosition) { _stringReference = new StringReference(_chars, initialPosition, charPos - initialPosition); } else { if (buffer == null) buffer = GetBuffer(); if (charPos > lastWritePosition) buffer.Append(_chars, lastWritePosition, charPos - lastWritePosition); _stringReference = new StringReference(buffer.GetInternalBuffer(), 0, buffer.Position); } charPos++; _charPos = charPos; return; } break; } } }
internal static bool TryParseDateTimeIso(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default(DateTime); return false; } DateTime d = CreateDateTime(dateTimeParser); long ticks; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: d = new DateTime(d.Ticks, DateTimeKind.Utc); break; case ParserTimeZone.LocalWestOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks + offset.Ticks; if (ticks <= DateTime.MaxValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks > DateTime.MaxValue.Ticks) { ticks = DateTime.MaxValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } case ParserTimeZone.LocalEastOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks - offset.Ticks; if (ticks >= DateTime.MinValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks < DateTime.MinValue.Ticks) { ticks = DateTime.MinValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } } dt = EnsureDateTime(d, dateTimeZoneHandling); return true; }
private static bool TryReadOffset(StringReference offsetText, int startIndex, out TimeSpan offset) { bool negative = (offsetText[startIndex] == '-'); int hours; if (ConvertUtils.Int32TryParse(offsetText.Chars, startIndex + 1, 2, out hours) != ParseResult.Success) { offset = default(TimeSpan); return false; } int minutes = 0; if (offsetText.Length - startIndex > 5) { if (ConvertUtils.Int32TryParse(offsetText.Chars, startIndex + 3, 2, out minutes) != ParseResult.Success) { offset = default(TimeSpan); return false; } } offset = TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(minutes); if (negative) { offset = offset.Negate(); } return true; }
private void ParseConstructor() { if (MatchValueWithTrailingSeperator("new")) { EatWhitespace(false); int initialPosition = _charPos; int endPosition; while (true) { char currentChar = _chars[_charPos]; if (currentChar == '\0') { if (_charsUsed == _charPos) { if (ReadData(true) == 0) throw JsonReaderException.Create(this, "Unexpected end while parsing constructor."); } else { endPosition = _charPos; _charPos++; break; } } else if (char.IsLetterOrDigit(currentChar)) { _charPos++; } else if (currentChar == StringUtils.CarriageReturn) { endPosition = _charPos; ProcessCarriageReturn(true); break; } else if (currentChar == StringUtils.LineFeed) { endPosition = _charPos; ProcessLineFeed(); break; } else if (char.IsWhiteSpace(currentChar)) { endPosition = _charPos; _charPos++; break; } else if (currentChar == '(') { endPosition = _charPos; break; } else { throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); } } _stringReference = new StringReference(_chars, initialPosition, endPosition - initialPosition); string constructorName = _stringReference.ToString(); EatWhitespace(false); if (_chars[_charPos] != '(') throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); _charPos++; ClearRecentString(); SetToken(JsonToken.StartConstructor, constructorName); } }
internal static bool TryParseDateTimeOffset(StringReference s, string dateFormatString, CultureInfo culture, out DateTimeOffset dt) { if (s.Length > 0) { int i = s.StartIndex; if (s[i] == '/') { if (s.Length >= 9 && s.StartsWith("/Date(") && s.EndsWith(")/")) { if (TryParseDateTimeOffsetMicrosoft(s, out dt)) { return true; } } } else if (s.Length >= 19 && s.Length <= 40 && char.IsDigit(s[i]) && s[i + 10] == 'T') { if (TryParseDateTimeOffsetIso(s, out dt)) { return true; } } if (!string.IsNullOrEmpty(dateFormatString)) { if (TryParseDateTimeOffsetExact(s.ToString(), dateFormatString, culture, out dt)) { return true; } } } dt = default(DateTimeOffset); return false; }
private static bool TryParseDateTimeMicrosoft(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { long ticks; TimeSpan offset; DateTimeKind kind; if (!TryParseMicrosoftDate(text, out ticks, out offset, out kind)) { dt = default(DateTime); return false; } DateTime utcDateTime = ConvertJavaScriptTicksToDateTime(ticks); switch (kind) { case DateTimeKind.Unspecified: dt = DateTime.SpecifyKind(utcDateTime.ToLocalTime(), DateTimeKind.Unspecified); break; case DateTimeKind.Local: dt = utcDateTime.ToLocalTime(); break; default: dt = utcDateTime; break; } dt = EnsureDateTime(dt, dateTimeZoneHandling); return true; }
// Token: 0x0600070E RID: 1806 // RVA: 0x0000A028 File Offset: 0x00008228 private void ClearRecentString() { if (this._buffer != null) { this._buffer.Position = 0; } this._stringReference = default(StringReference); }
// Token: 0x0600071A RID: 1818 // RVA: 0x0003A474 File Offset: 0x00038674 private void ParseComment() { this._charPos++; if (!this.EnsureChars(1, false)) { throw JsonReaderException.Create(this, "Unexpected end while parsing comment."); } bool flag; if (this._chars[this._charPos] == '*') { flag = false; } else { if (this._chars[this._charPos] != '/') { throw JsonReaderException.Create(this, StringUtils.FormatWith("Error parsing comment. Expected: *, got {0}.", CultureInfo.InvariantCulture, this._chars[this._charPos])); } flag = true; } this._charPos++; int charPos = this._charPos; bool flag2 = false; while (!flag2) { char c = this._chars[this._charPos]; if (c <= '\n') { if (c != '\0') { if (c == '\n') { if (flag) { this._stringReference = new StringReference(this._chars, charPos, this._charPos - charPos); flag2 = true; } this.ProcessLineFeed(); continue; } } else { if (this._charsUsed != this._charPos) { this._charPos++; continue; } if (this.ReadData(true) != 0) { continue; } if (flag) { this._stringReference = new StringReference(this._chars, charPos, this._charPos - charPos); flag2 = true; continue; } throw JsonReaderException.Create(this, "Unexpected end while parsing comment."); } } else { if (c == '\r') { if (flag) { this._stringReference = new StringReference(this._chars, charPos, this._charPos - charPos); flag2 = true; } this.ProcessCarriageReturn(true); continue; } if (c == '*') { this._charPos++; if (!flag && this.EnsureChars(0, true) && this._chars[this._charPos] == '/') { this._stringReference = new StringReference(this._chars, charPos, this._charPos - charPos - 1); this._charPos++; flag2 = true; continue; } continue; } } this._charPos++; } base.SetToken(JsonToken.Comment, this._stringReference.ToString()); this.ClearRecentString(); }
// Token: 0x06000718 RID: 1816 // RVA: 0x00039E54 File Offset: 0x00038054 private void ParseConstructor() { if (!this.MatchValueWithTrailingSeparator("new")) { throw JsonReaderException.Create(this, "Unexpected content while parsing JSON."); } this.EatWhitespace(false); int charPos = this._charPos; char c; while (true) { c = this._chars[this._charPos]; if (c == '\0') { if (this._charsUsed != this._charPos) { goto IL_6F; } if (this.ReadData(true) == 0) { break; } } else { if (!char.IsLetterOrDigit(c)) { goto IL_86; } this._charPos++; } } throw JsonReaderException.Create(this, "Unexpected end while parsing constructor."); IL_6F: int charPos2 = this._charPos; this._charPos++; goto IL_DA; IL_86: if (c == '\r') { charPos2 = this._charPos; this.ProcessCarriageReturn(true); } else if (c == '\n') { charPos2 = this._charPos; this.ProcessLineFeed(); } else if (char.IsWhiteSpace(c)) { charPos2 = this._charPos; this._charPos++; } else { if (c != '(') { throw JsonReaderException.Create(this, StringUtils.FormatWith("Unexpected character while parsing constructor: {0}.", CultureInfo.InvariantCulture, c)); } charPos2 = this._charPos; } IL_DA: this._stringReference = new StringReference(this._chars, charPos, charPos2 - charPos); string value = this._stringReference.ToString(); this.EatWhitespace(false); if (this._chars[this._charPos] != '(') { throw JsonReaderException.Create(this, StringUtils.FormatWith("Unexpected character while parsing constructor: {0}.", CultureInfo.InvariantCulture, this._chars[this._charPos])); } this._charPos++; this.ClearRecentString(); base.SetToken(JsonToken.StartConstructor, value); }
private void ClearRecentString() { _stringBuffer.Position = 0; _stringReference = new StringReference(); }
private void ParseComment() { // should have already parsed / character before reaching this method _charPos++; if (!EnsureChars(1, false) || _chars[_charPos] != '*') throw JsonReaderException.Create(this, "Error parsing comment. Expected: *, got {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); else _charPos++; int initialPosition = _charPos; bool commentFinished = false; while (!commentFinished) { switch (_chars[_charPos]) { case '\0': if (_charsUsed == _charPos) { if (ReadData(true) == 0) throw JsonReaderException.Create(this, "Unexpected end while parsing comment."); } else { _charPos++; } break; case '*': _charPos++; if (EnsureChars(0, true)) { if (_chars[_charPos] == '/') { _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition - 1); _charPos++; commentFinished = true; } } break; case StringUtils.CarriageReturn: ProcessCarriageReturn(true); break; case StringUtils.LineFeed: ProcessLineFeed(); break; default: _charPos++; break; } } SetToken(JsonToken.Comment, _stringReference.ToString()); ClearRecentString(); }
private void ParseNumber() { ShiftBufferIfNeeded(); char firstChar = _chars[_charPos]; int initialPosition = _charPos; ReadNumberIntoBuffer(); _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); object numberValue; JsonToken numberType; bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1); bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1 && _stringReference.Chars[_stringReference.StartIndex + 1] != '.' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E'); if (_readType == ReadType.ReadAsInt32) { if (singleDigit) { // digit char values start at 48 numberValue = firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt32(number, 16) : Convert.ToInt32(number, 8); numberValue = integer; } else { numberValue = ConvertUtils.IntParseFast(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length); } numberType = JsonToken.Integer; } else if (_readType == ReadType.ReadAsDecimal) { if (singleDigit) { // digit char values start at 48 numberValue = (decimal)firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberValue = Convert.ToDecimal(integer); } else { string number = _stringReference.ToString(); numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture); } numberType = JsonToken.Float; } else { if (singleDigit) { // digit char values start at 48 numberValue = (long)firstChar - 48; numberType = JsonToken.Integer; } else if (nonBase10) { string number = _stringReference.ToString(); numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberType = JsonToken.Integer; } else { string number = _stringReference.ToString(); // it's faster to do 3 indexof with single characters than an indexofany if (number.IndexOf('.') != -1 || number.IndexOf('E') != -1 || number.IndexOf('e') != -1) { if (_floatParseHandling == FloatParseHandling.Decimal) numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture); else numberValue = Convert.ToDouble(number, CultureInfo.InvariantCulture); numberType = JsonToken.Float; } else { long value; if (long.TryParse(number, out value)) { numberValue = value; } else { #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) numberValue = BigInteger.Parse(number, CultureInfo.InvariantCulture); #else // todo - validate number was a valid integer to make sure overflow was the reason for failure throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, number)); #endif } numberType = JsonToken.Integer; } } } ClearRecentString(); SetToken(numberType, numberValue); }
private void ClearRecentString() { if (_buffer != null) _buffer.Position = 0; _stringReference = new StringReference(); }
internal static bool TryParseDateTimeIso(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default; return(false); } DateTime d = CreateDateTime(dateTimeParser); long ticks; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: d = new DateTime(d.Ticks, DateTimeKind.Utc); break; case ParserTimeZone.LocalWestOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks + offset.Ticks; if (ticks <= DateTime.MaxValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks > DateTime.MaxValue.Ticks) { ticks = DateTime.MaxValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } case ParserTimeZone.LocalEastOfUtc: { TimeSpan offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = d.Ticks - offset.Ticks; if (ticks >= DateTime.MinValue.Ticks) { d = new DateTime(ticks, DateTimeKind.Utc).ToLocalTime(); } else { ticks += d.GetUtcOffset().Ticks; if (ticks < DateTime.MinValue.Ticks) { ticks = DateTime.MinValue.Ticks; } d = new DateTime(ticks, DateTimeKind.Local); } break; } } dt = EnsureDateTime(d, dateTimeZoneHandling); return(true); }
public static int IndexOf(this StringReference s, char c, int startIndex, int length) { int num = Array.IndexOf <char>(s.Chars, c, s.StartIndex + startIndex, length); return(num == -1 ? -1 : num - s.StartIndex); }
internal static bool TryParseDateTimeOffsetIso(StringReference text, out DateTimeOffset dt) { DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = default(DateTimeOffset); return false; } DateTime d = CreateDateTime(dateTimeParser); TimeSpan offset; switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: offset = new TimeSpan(0L); break; case ParserTimeZone.LocalWestOfUtc: offset = new TimeSpan(-dateTimeParser.ZoneHour, -dateTimeParser.ZoneMinute, 0); break; case ParserTimeZone.LocalEastOfUtc: offset = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); break; default: offset = TimeZoneInfo.Local.GetUtcOffset(d); break; } long ticks = d.Ticks - offset.Ticks; if (ticks < 0 || ticks > 3155378975999999999) { dt = default(DateTimeOffset); return false; } dt = new DateTimeOffset(d, offset); return true; }
private void ParseNumber(ReadType readType) { ShiftBufferIfNeeded(); char firstChar = _chars[_charPos]; int initialPosition = _charPos; ReadNumberIntoBuffer(); // set state to PostValue now so that if there is an error parsing the number then the reader can continue SetPostValueState(true); _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); object numberValue; JsonToken numberType; bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1); bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1 && _stringReference.Chars[_stringReference.StartIndex + 1] != '.' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E'); if (readType == ReadType.ReadAsString) { string number = _stringReference.ToString(); // validate that the string is a valid number if (nonBase10) { try { if (number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { Convert.ToInt64(number, 16); } else { Convert.ToInt64(number, 8); } } catch (Exception ex) { throw ThrowReaderError("Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number), ex); } } else { double value; if (!double.TryParse(number, NumberStyles.Float, CultureInfo.InvariantCulture, out value)) { throw ThrowReaderError("Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } } numberType = JsonToken.String; numberValue = number; } else if (readType == ReadType.ReadAsInt32) { if (singleDigit) { // digit char values start at 48 numberValue = firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); try { int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt32(number, 16) : Convert.ToInt32(number, 8); numberValue = integer; } catch (Exception ex) { throw ThrowReaderError("Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, number), ex); } } else { int value; ParseResult parseResult = ConvertUtils.Int32TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value); if (parseResult == ParseResult.Success) { numberValue = value; } else if (parseResult == ParseResult.Overflow) { throw ThrowReaderError("JSON integer {0} is too large or small for an Int32.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } else { throw ThrowReaderError("Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } } numberType = JsonToken.Integer; } else if (readType == ReadType.ReadAsDecimal) { if (singleDigit) { // digit char values start at 48 numberValue = (decimal)firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); try { // decimal.Parse doesn't support parsing hexadecimal values long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberValue = Convert.ToDecimal(integer); } catch (Exception ex) { throw ThrowReaderError("Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number), ex); } } else { string number = _stringReference.ToString(); decimal value; if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out value)) { numberValue = value; } else { throw ThrowReaderError("Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } } numberType = JsonToken.Float; } else if (readType == ReadType.ReadAsDouble) { if (singleDigit) { // digit char values start at 48 numberValue = (double)firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); try { // double.Parse doesn't support parsing hexadecimal values long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberValue = Convert.ToDouble(integer); } catch (Exception ex) { throw ThrowReaderError("Input string '{0}' is not a valid double.".FormatWith(CultureInfo.InvariantCulture, number), ex); } } else { double value; ParseResult parseResult = ConvertUtils.DoubleTryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value); if (parseResult == ParseResult.Success) { numberValue = value; } else { throw ThrowReaderError("Input string '{0}' is not a valid double.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } } numberType = JsonToken.Float; } else { if (singleDigit) { // digit char values start at 48 numberValue = (long)firstChar - 48; numberType = JsonToken.Integer; } else if (nonBase10) { string number = _stringReference.ToString(); try { numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); } catch (Exception ex) { throw ThrowReaderError("Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number), ex); } numberType = JsonToken.Integer; } else { long value; ParseResult parseResult = ConvertUtils.Int64TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value); if (parseResult == ParseResult.Success) { numberValue = value; numberType = JsonToken.Integer; } else if (parseResult == ParseResult.Overflow) { #if !(NET20 || NET35 || PORTABLE40 || PORTABLE) || NETSTANDARD1_1 string number = _stringReference.ToString(); if (number.Length > MaximumJavascriptIntegerCharacterLength) { throw ThrowReaderError("JSON integer {0} is too large to parse.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } numberValue = BigIntegerParse(number, CultureInfo.InvariantCulture); numberType = JsonToken.Integer; #else throw ThrowReaderError("JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); #endif } else { string number = _stringReference.ToString(); if (_floatParseHandling == FloatParseHandling.Decimal) { decimal d; if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out d)) { numberValue = d; } else { throw ThrowReaderError("Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number)); } } else { double d; parseResult = ConvertUtils.DoubleTryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out d); if (parseResult == ParseResult.Success) { numberValue = d; } else { throw ThrowReaderError("Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number)); } } numberType = JsonToken.Float; } } } ClearRecentString(); // index has already been updated SetToken(numberType, numberValue, false); }
private static bool TryParseMicrosoftDate(StringReference text, out long ticks, out TimeSpan offset, out DateTimeKind kind) { kind = DateTimeKind.Utc; int index = text.IndexOf('+', 7, text.Length - 8); if (index == -1) { index = text.IndexOf('-', 7, text.Length - 8); } if (index != -1) { kind = DateTimeKind.Local; if (!TryReadOffset(text, index + text.StartIndex, out offset)) { ticks = 0; return false; } } else { offset = TimeSpan.Zero; index = text.Length - 2; } return (ConvertUtils.Int64TryParse(text.Chars, 6 + text.StartIndex, index - 6, out ticks) == ParseResult.Success); }
internal static bool TryParseDateTimeIso(StringReference text, DateTimeZoneHandling dateTimeZoneHandling, out DateTime dt) { long ticks; TimeSpan utcOffset; DateTimeParser dateTimeParser = new DateTimeParser(); if (!dateTimeParser.Parse(text.Chars, text.StartIndex, text.Length)) { dt = new DateTime(); return(false); } DateTime dateTime = DateTimeUtils.CreateDateTime(dateTimeParser); switch (dateTimeParser.Zone) { case ParserTimeZone.Utc: { dateTime = new DateTime(dateTime.Ticks, DateTimeKind.Utc); break; } case ParserTimeZone.LocalWestOfUtc: { TimeSpan timeSpan = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = dateTime.Ticks + timeSpan.Ticks; if (ticks > DateTime.MaxValue.Ticks) { utcOffset = dateTime.GetUtcOffset(); ticks += utcOffset.Ticks; if (ticks > DateTime.MaxValue.Ticks) { ticks = DateTime.MaxValue.Ticks; } dateTime = new DateTime(ticks, DateTimeKind.Local); break; } else { dateTime = (new DateTime(ticks, DateTimeKind.Utc)).ToLocalTime(); break; } } case ParserTimeZone.LocalEastOfUtc: { TimeSpan timeSpan1 = new TimeSpan(dateTimeParser.ZoneHour, dateTimeParser.ZoneMinute, 0); ticks = dateTime.Ticks - timeSpan1.Ticks; if (ticks < DateTime.MinValue.Ticks) { utcOffset = dateTime.GetUtcOffset(); ticks += utcOffset.Ticks; if (ticks < DateTime.MinValue.Ticks) { ticks = DateTime.MinValue.Ticks; } dateTime = new DateTime(ticks, DateTimeKind.Local); break; } else { dateTime = (new DateTime(ticks, DateTimeKind.Utc)).ToLocalTime(); break; } } } dt = DateTimeUtils.EnsureDateTime(dateTime, dateTimeZoneHandling); return(true); }
private static bool TryParseDateTimeOffsetMicrosoft(StringReference text, out DateTimeOffset dt) { long ticks; TimeSpan offset; DateTimeKind kind; if (!TryParseMicrosoftDate(text, out ticks, out offset, out kind)) { dt = default(DateTime); return false; } DateTime utcDateTime = ConvertJavaScriptTicksToDateTime(ticks); dt = new DateTimeOffset(utcDateTime.Add(offset).Ticks, offset); return true; }
private void ParseNumber() { ShiftBufferIfNeeded(); char firstChar = _chars[_charPos]; int initialPosition = _charPos; ReadNumberIntoBuffer(); _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); object numberValue; JsonToken numberType; bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1); bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1 && _stringReference.Chars[_stringReference.StartIndex + 1] != '.' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E'); if (_readType == ReadType.ReadAsInt32) { if (singleDigit) { // digit char values start at 48 numberValue = firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt32(number, 16) : Convert.ToInt32(number, 8); numberValue = integer; } else { numberValue = ConvertUtils.Int32Parse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length); } numberType = JsonToken.Integer; } else if (_readType == ReadType.ReadAsDecimal) { if (singleDigit) { // digit char values start at 48 numberValue = (decimal)firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberValue = Convert.ToDecimal(integer); } else { string number = _stringReference.ToString(); numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture); } numberType = JsonToken.Float; } else { if (singleDigit) { // digit char values start at 48 numberValue = (long)firstChar - 48; numberType = JsonToken.Integer; } else if (nonBase10) { string number = _stringReference.ToString(); numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberType = JsonToken.Integer; } else { long value; ParseResult parseResult = ConvertUtils.Int64TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value); if (parseResult == ParseResult.Success) { numberValue = value; numberType = JsonToken.Integer; } else if (parseResult == ParseResult.Invalid) { string number = _stringReference.ToString(); if (_floatParseHandling == FloatParseHandling.Decimal) numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture); else numberValue = Convert.ToDouble(number, CultureInfo.InvariantCulture); numberType = JsonToken.Float; } else if (parseResult == ParseResult.Overflow) { string number = _stringReference.ToString(); numberValue = BigInteger.Parse(number, CultureInfo.InvariantCulture); numberType = JsonToken.Integer; } else { throw JsonReaderException.Create(this, "Unknown error parsing integer."); } } } ClearRecentString(); SetToken(numberType, numberValue); }
private void ParseNumber() { ShiftBufferIfNeeded(); char firstChar = _chars[_charPos]; int initialPosition = _charPos; ReadNumberIntoBuffer(); _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); object numberValue; JsonToken numberType; bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1); bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1 && _stringReference.Chars[_stringReference.StartIndex + 1] != '.' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E'); if (_readType == ReadType.ReadAsInt32) { if (singleDigit) { // digit char values start at 48 numberValue = firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt32(number, 16) : Convert.ToInt32(number, 8); numberValue = integer; } else { int value; ParseResult parseResult = ConvertUtils.Int32TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value); if (parseResult == ParseResult.Success) numberValue = value; else if (parseResult == ParseResult.Overflow) throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int32.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); else throw JsonReaderException.Create(this, "Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } numberType = JsonToken.Integer; } else if (_readType == ReadType.ReadAsDecimal) { if (singleDigit) { // digit char values start at 48 numberValue = (decimal)firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberValue = Convert.ToDecimal(integer); } else { string number = _stringReference.ToString(); decimal value; if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out value)) numberValue = value; else throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); } numberType = JsonToken.Float; } else { if (singleDigit) { // digit char values start at 48 numberValue = (long)firstChar - 48; numberType = JsonToken.Integer; } else if (nonBase10) { string number = _stringReference.ToString(); numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberType = JsonToken.Integer; } else { long value; ParseResult parseResult = ConvertUtils.Int64TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value); if (parseResult == ParseResult.Success) { numberValue = value; numberType = JsonToken.Integer; } else if (parseResult == ParseResult.Overflow) { #if !(NET20 || NET35 || PORTABLE40 || PORTABLE) string number = _stringReference.ToString(); numberValue = BigInteger.Parse(number, CultureInfo.InvariantCulture); numberType = JsonToken.Integer; #else throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString())); #endif } else { string number = _stringReference.ToString(); if (_floatParseHandling == FloatParseHandling.Decimal) { decimal d; if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out d)) numberValue = d; else throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number)); } else { double d; if (double.TryParse(number, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out d)) numberValue = d; else throw JsonReaderException.Create(this, "Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number)); } numberType = JsonToken.Float; } } } ClearRecentString(); SetToken(numberType, numberValue); }
private void ReadDollarStringIntoBuffer(char quote) { //_charPos--; //wrong, because _charPos may be 0. var currentPos = _charPos; DlState _state = DlState.FirstDollar; string tag = null; int firstDollarIndex = currentPos; int thirdDollarIndex = currentPos; firstDollarIndex = currentPos - 1; while (true) { char c = _chars[currentPos]; switch (c) { case '\0': if (_charsUsed == currentPos) { currentPos--; if (ReadData(true) == 0) { _charPos = currentPos; throw JsonReaderException.Create(this, "Unterminated string. Expected delimiter: {0}.".FormatWith(CultureInfo.InvariantCulture, quote)); } } break; case '$': case '`': if (c != quote) { currentPos++; continue; } switch (_state) { case DlState.None: firstDollarIndex = currentPos; _state = DlState.FirstDollar; break; case DlState.FirstDollar: tag = new string(_chars, firstDollarIndex + 1, currentPos - firstDollarIndex - 1); _state = DlState.InString; break; case DlState.InString: thirdDollarIndex = currentPos; _state = DlState.ThirdDollar; break; case DlState.ThirdDollar: { bool match = false; if (currentPos - thirdDollarIndex - 1 == tag.Length) { var testTag = new string(_chars, thirdDollarIndex + 1, currentPos - thirdDollarIndex - 1); if (testTag == tag) { match = true; } } if (match) { var start = firstDollarIndex + tag.Length + 2; _stringReference = new StringReference(_chars, start, thirdDollarIndex - start); _state = DlState.Complete; currentPos++; _charPos = currentPos; return; } else { thirdDollarIndex = currentPos; } } break; case DlState.Complete: break; default: break; } break; default: break; }//end switch currentPos++; } }
private void ParseNumber() { ShiftBufferIfNeeded(); char firstChar = _chars[_charPos]; int initialPosition = _charPos; ReadNumberIntoBuffer(); _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); object numberValue; JsonToken numberType; bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1); bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1 && _stringReference.Chars[_stringReference.StartIndex + 1] != '.' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e' && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E'); if (_readType == ReadType.ReadAsInt32) { if (singleDigit) { // digit char values start at 48 numberValue = firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt32(number, 16) : Convert.ToInt32(number, 8); numberValue = integer; } else { string number = _stringReference.ToString(); numberValue = Convert.ToInt32(number, CultureInfo.InvariantCulture); } numberType = JsonToken.Integer; } else if (_readType == ReadType.ReadAsDecimal) { if (singleDigit) { // digit char values start at 48 numberValue = (decimal)firstChar - 48; } else if (nonBase10) { string number = _stringReference.ToString(); // decimal.Parse doesn't support parsing hexadecimal values long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberValue = Convert.ToDecimal(integer); } else { string number = _stringReference.ToString(); numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture); } numberType = JsonToken.Float; } else { if (singleDigit) { // digit char values start at 48 numberValue = (long)firstChar - 48; numberType = JsonToken.Integer; } else if (nonBase10) { string number = _stringReference.ToString(); numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(number, 16) : Convert.ToInt64(number, 8); numberType = JsonToken.Integer; } else { string number = _stringReference.ToString(); // it's faster to do 3 indexof with single characters than an indexofany if (number.IndexOf('.') != -1 || number.IndexOf('E') != -1 || number.IndexOf('e') != -1) { numberValue = Convert.ToDouble(number, CultureInfo.InvariantCulture); numberType = JsonToken.Float; } else { try { numberValue = Convert.ToInt64(number, CultureInfo.InvariantCulture); } catch (OverflowException ex) { throw JsonReaderException.Create((JsonReader)this, "JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, number), ex); } numberType = JsonToken.Integer; } } } ClearRecentString(); SetToken(numberType, numberValue); }
public void ReadOffsetMSDateTimeOffset() { char[] c = @"12345/Date(1418924498000+0800)/12345".ToCharArray(); StringReference reference = new StringReference(c, 5, c.Length - 10); DateTimeOffset d; DateTimeUtils.TryParseDateTimeOffset(reference, null, CultureInfo.InvariantCulture, out d); long initialTicks = DateTimeUtils.ConvertDateTimeToJavaScriptTicks(d.DateTime, d.Offset); Assert.AreEqual(1418924498000, initialTicks); Assert.AreEqual(8, d.Offset.Hours); }
private void ReadStringIntoBuffer(char quote) { int charPos = _charPos; int initialPosition = _charPos; int lastWritePosition = _charPos; StringBuffer buffer = null; while (true) { switch (_chars[charPos++]) { case '\0': if (_charsUsed == charPos - 1) { charPos--; if (ReadData(true) == 0) { _charPos = charPos; throw JsonReaderException.Create(this, "Unterminated string. Expected delimiter: {0}.".FormatWith(CultureInfo.InvariantCulture, quote)); } } break; case '\\': _charPos = charPos; if (!EnsureChars(0, true)) { _charPos = charPos; throw JsonReaderException.Create(this, "Unterminated string. Expected delimiter: {0}.".FormatWith(CultureInfo.InvariantCulture, quote)); } // start of escape sequence int escapeStartPos = charPos - 1; char currentChar = _chars[charPos]; char writeChar; switch (currentChar) { case 'b': charPos++; writeChar = '\b'; break; case 't': charPos++; writeChar = '\t'; break; case 'n': charPos++; writeChar = '\n'; break; case 'f': charPos++; writeChar = '\f'; break; case 'r': charPos++; writeChar = '\r'; break; case '\\': charPos++; writeChar = '\\'; break; case '"': case '\'': case '/': writeChar = currentChar; charPos++; break; case 'u': charPos++; _charPos = charPos; writeChar = ParseUnicode(); if (StringUtils.IsLowSurrogate(writeChar)) { // low surrogate with no preceding high surrogate; this char is replaced writeChar = UnicodeReplacementChar; } else if (StringUtils.IsHighSurrogate(writeChar)) { bool anotherHighSurrogate; // loop for handling situations where there are multiple consecutive high surrogates do { anotherHighSurrogate = false; // potential start of a surrogate pair if (EnsureChars(2, true) && _chars[_charPos] == '\\' && _chars[_charPos + 1] == 'u') { char highSurrogate = writeChar; _charPos += 2; writeChar = ParseUnicode(); if (StringUtils.IsLowSurrogate(writeChar)) { // a valid surrogate pair! } else if (StringUtils.IsHighSurrogate(writeChar)) { // another high surrogate; replace current and start check over highSurrogate = UnicodeReplacementChar; anotherHighSurrogate = true; } else { // high surrogate not followed by low surrogate; original char is replaced highSurrogate = UnicodeReplacementChar; } if (buffer == null) buffer = GetBuffer(); WriteCharToBuffer(buffer, highSurrogate, lastWritePosition, escapeStartPos); lastWritePosition = _charPos; } else { // there are not enough remaining chars for the low surrogate or is not follow by unicode sequence // replace high surrogate and continue on as usual writeChar = UnicodeReplacementChar; } } while (anotherHighSurrogate); } charPos = _charPos; break; default: charPos++; _charPos = charPos; throw JsonReaderException.Create(this, "Bad JSON escape sequence: {0}.".FormatWith(CultureInfo.InvariantCulture, @"\" + currentChar)); } if (buffer == null) buffer = GetBuffer(); WriteCharToBuffer(buffer, writeChar, lastWritePosition, escapeStartPos); lastWritePosition = charPos; break; case StringUtils.CarriageReturn: _charPos = charPos - 1; ProcessCarriageReturn(true); charPos = _charPos; break; case StringUtils.LineFeed: _charPos = charPos - 1; ProcessLineFeed(); charPos = _charPos; break; case '"': case '\'': if (_chars[charPos - 1] == quote) { charPos--; if (initialPosition == lastWritePosition) { _stringReference = new StringReference(_chars, initialPosition, charPos - initialPosition); } else { if (buffer == null) buffer = GetBuffer(); if (charPos > lastWritePosition) buffer.Append(_chars, lastWritePosition, charPos - lastWritePosition); _stringReference = new StringReference(buffer.GetInternalBuffer(), 0, buffer.Position); } charPos++; _charPos = charPos; return; } break; } } }
private void ParseNumber() { this.ShiftBufferIfNeeded(); char c = this._chars[this._charPos]; int startIndex = this._charPos; this.ReadNumberIntoBuffer(); this._stringReference = new StringReference(this._chars, startIndex, this._charPos - startIndex); bool flag1 = char.IsDigit(c) && this._stringReference.Length == 1; bool flag2 = (int) c == 48 && this._stringReference.Length > 1 && ((int) this._stringReference.Chars[this._stringReference.StartIndex + 1] != 46 && (int) this._stringReference.Chars[this._stringReference.StartIndex + 1] != 101) && (int) this._stringReference.Chars[this._stringReference.StartIndex + 1] != 69; object obj; JsonToken newToken; if (this._readType == ReadType.ReadAsInt32) { if (flag1) obj = (object) ((int) c - 48); else if (flag2) { string str = this._stringReference.ToString(); obj = (object) (str.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt32(str, 16) : Convert.ToInt32(str, 8)); } else obj = (object) Convert.ToInt32(this._stringReference.ToString(), (IFormatProvider) CultureInfo.InvariantCulture); newToken = JsonToken.Integer; } else if (this._readType == ReadType.ReadAsDecimal) { if (flag1) obj = (object) ((Decimal) c - new Decimal(48)); else if (flag2) { string str = this._stringReference.ToString(); obj = (object) Convert.ToDecimal(str.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(str, 16) : Convert.ToInt64(str, 8)); } else obj = (object) Decimal.Parse(this._stringReference.ToString(), NumberStyles.Number | NumberStyles.AllowExponent, (IFormatProvider) CultureInfo.InvariantCulture); newToken = JsonToken.Float; } else if (flag1) { obj = (object) ((long) c - 48L); newToken = JsonToken.Integer; } else if (flag2) { string str = this._stringReference.ToString(); obj = (object) (str.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? Convert.ToInt64(str, 16) : Convert.ToInt64(str, 8)); newToken = JsonToken.Integer; } else { string str = this._stringReference.ToString(); if (str.IndexOf('.') == -1 && str.IndexOf('E') == -1) { if (str.IndexOf('e') == -1) { try { obj = (object) Convert.ToInt64(str, (IFormatProvider) CultureInfo.InvariantCulture); } catch (OverflowException ex) { throw JsonReaderException.Create((JsonReader) this, StringUtils.FormatWith("JSON integer {0} is too large or small for an Int64.", (IFormatProvider) CultureInfo.InvariantCulture, (object) str), (Exception) ex); } newToken = JsonToken.Integer; goto label_24; } } obj = (object) Convert.ToDouble(str, (IFormatProvider) CultureInfo.InvariantCulture); newToken = JsonToken.Float; } label_24: this.ClearRecentString(); this.SetToken(newToken, obj); }
private void ParseUnquotedProperty() { int initialPosition = _charPos; // parse unquoted property name until whitespace or colon while (true) { switch (_chars[_charPos]) { case '\0': if (_charsUsed == _charPos) { if (ReadData(true) == 0) throw JsonReaderException.Create(this, "Unexpected end while parsing unquoted property name."); break; } _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); return; default: char currentChar = _chars[_charPos]; if (ValidIdentifierChar(currentChar)) { _charPos++; break; } else if (char.IsWhiteSpace(currentChar) || currentChar == ':') { _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); return; } throw JsonReaderException.Create(this, "Invalid JavaScript property identifier character: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); } } }
public static int IndexOf(this StringReference s, char c) { return(IndexOf(s, c, 0, 0)); }