/// <summary> /// Matches a JSON array content: the match ends with the first ]. /// Whitespaces and JS comments (//... or /* ... */) are skipped. /// </summary> /// <param name="this">This <see cref="StringMatcher"/>.</param> /// <param name="value">The list of objects on success.</param> /// <returns>True on success, false otherwise.</returns> public static bool MatchJSONArrayContent(this StringMatcher @this, out List <object> value) { value = null; while ([email protected]) { @this.SkipWhiteSpacesAndJSComments(); if (@this.TryMatchChar(']')) { if (value == null) { value = new List <object>(); } return(true); } object cell; if (!MatchJSONObject(@this, out cell)) { return(false); } if (value == null) { value = new List <object>(); } value.Add(cell); @this.SkipWhiteSpacesAndJSComments(); // Allow trailing comma: ,] is valid. @this.TryMatchChar(','); } return(@this.SetError("JSON array definition but reached end of match.")); }
/// <summary> /// Matches a { "JSON" : "object" }, a ["JSON", "array"] or a terminal value (string, null, double, true or false) /// and any combination of them. /// Whitespaces and JS comments (//... or /* ... */) are skipped. /// </summary> /// <param name="this">This <see cref="StringMatcher"/>.</param> /// <param name="value"> /// A list of objects (for array), a list of KeyValuePair<string,object> for object or /// a double, string, boolean or null (for null).</param> /// <returns>True on success, false on error.</returns> public static bool MatchJSONObject(this StringMatcher @this, out object value) { value = null; @this.SkipWhiteSpacesAndJSComments(); if (@this.TryMatchChar('{')) { List <KeyValuePair <string, object> > c; if (!MatchJSONObjectContent(@this, out c)) { return(false); } value = c; return(true); } if (@this.TryMatchChar('[')) { List <object> t; if (!MatchJSONArrayContent(@this, out t)) { return(false); } value = t; return(true); } if (TryMatchJSONTerminalValue(@this, out value)) { return(true); } return(@this.SetError("JSON value.")); }
/// <summary> /// Matches Int32 values that must not start with '0' ('0' is valid but '0d', where d is any digit, is not). /// A signed integer starts with a '-'. '-0' is valid but '-0d' (where d is any digit) is not. /// If the value is to big for an Int32, it fails. /// </summary> /// <param name="this">This <see cref="StringMatcher"/>.</param> /// <param name="i">The result integer. 0 on failure.</param> /// <param name="minValue">Optional minimal value.</param> /// <param name="maxValue">Optional maximal value.</param> /// <returns><c>true</c> when matched, <c>false</c> otherwise.</returns> public static bool MatchInt32(this StringMatcher @this, out int i, int minValue = int.MinValue, int maxValue = int.MaxValue) { i = 0; int savedIndex = @this.StartIndex; long value = 0; bool signed; if (@this.IsEnd) { return(@this.SetError()); } if ((signed = @this.TryMatchChar('-')) && @this.IsEnd) { return(@this.BackwardAddError(savedIndex)); } char c; if (@this.TryMatchChar('0')) { if ([email protected] && (c = @this.Head) >= '0' && c <= '9') { return(@this.BackwardAddError(savedIndex, "0...9")); } return(@this.SetSuccess()); } unchecked { long iMax = Int32.MaxValue; if (signed) { iMax = iMax + 1; } while ([email protected] && (c = @this.Head) >= '0' && c <= '9') { value = value * 10 + (c - '0'); if (value > iMax) { break; } @this.UncheckedMove(1); } } if (@this.StartIndex > savedIndex) { if (signed) { value = -value; } if (value < (long)minValue || value > (long)maxValue) { return(@this.BackwardAddError(savedIndex, String.Format(CultureInfo.InvariantCulture, "value between {0} and {1}", minValue, maxValue))); } i = (int)value; return(@this.SetSuccess()); } return(@this.SetError()); }
/// <summary> /// Matches a very simple version of a JSON object content: this match stops at the first closing }. /// Whitespaces and JS comments (//... or /* ... */) are skipped. /// </summary> /// <param name="this">This <see cref="StringMatcher"/>.</param> /// <param name="o">The read object on success as a list of KeyValuePair.</param> /// <returns>True on success, false on error.</returns> public static bool MatchJSONObjectContent(this StringMatcher @this, out List <KeyValuePair <string, object> > o) { o = null; while ([email protected]) { @this.SkipWhiteSpacesAndJSComments(); string propName; object value; if (@this.TryMatchChar('}')) { if (o == null) { o = new List <KeyValuePair <string, object> >(); } return(true); } if ([email protected](out propName)) { o = null; return(@this.SetError("Quoted Property Name.")); } @this.SkipWhiteSpacesAndJSComments(); if ([email protected](':') || !MatchJSONObject(@this, out value)) { o = null; return(false); } if (o == null) { o = new List <KeyValuePair <string, object> >(); } o.Add(new KeyValuePair <string, object>(propName, value)); @this.SkipWhiteSpacesAndJSComments(); // This accepts e trailing comma at the end of a property list: ..."a":0,} is not an error. @this.TryMatchChar(','); } return(@this.SetError("JSON object definition but reached end of match.")); }
/// <summary> /// Tries to match a //.... or /* ... */ comment. /// Proper termination of comment (by a new line or the closing */) is not required: /// a ending /*... is considered valid. /// </summary> /// <param name="this">This <see cref="StringMatcher"/>.</param> /// <returns>True on success, false if the <see cref="StringMatcher.Head"/> is not on a /.</returns> public static bool TryMatchJSComment(this StringMatcher @this) { if ([email protected]('/')) { return(false); } if (@this.TryMatchChar('/')) { while ([email protected] && @this.Head != '\n') { @this.UncheckedMove(1); } if ([email protected]) { @this.UncheckedMove(1); } return(true); } else if (@this.TryMatchChar('*')) { while ([email protected]) { if (@this.Head == '*') { @this.UncheckedMove(1); if (@this.IsEnd || @this.TryMatchChar('/')) { return(true); } } @this.UncheckedMove(1); } return(true); } @this.UncheckedMove(-1); return(false); }
/// <summary> /// Visits any json item: it is either a terminal (<see cref="VisitTerminalValue"/>), /// {"an":"object"} (see <see cref="VisitObjectContent"/> or ["an","array"] (see <see cref="VisitArrayContent"/>). /// </summary> /// <returns>True on success. On error a message may be retrieved from the <see cref="Matcher"/>.</returns> public virtual bool Visit() { SkipWhiteSpaces(); if (_m.TryMatchChar('{')) { return(VisitObjectContent()); } if (_m.TryMatchChar('[')) { return(VisitArrayContent()); } return(VisitTerminalValue()); }