public Boolean ParseHTTP(String ParameterName, HTTPRequest HTTPRequest, out String Value, out HTTPResponse HTTPResp, String Context = null, String DefaultValue = null) { Object JSONToken; if (!TryGetValue(ParameterName, out JSONToken)) { Value = DefaultValue; HTTPResp = HTTPExtentions.CreateBadRequest(HTTPRequest, Context, ParameterName); return(false); } if (JSONToken != null) { Value = JSONToken.ToString(); if (Value.IsNullOrEmpty()) { Value = DefaultValue; HTTPResp = HTTPExtentions.CreateBadRequest(HTTPRequest, Context, ParameterName, JSONToken.ToString()); return(false); } } else { Value = DefaultValue; HTTPResp = null; return(false); } HTTPResp = null; return(true); }
internal static ArrayList ParseArray(char[] json, ref int index, ref int line) { ArrayList array = new ArrayList(); // [ NextToken(json, ref index, ref line); bool done = false; while (!done) { JSONToken token = LookAhead(json, index, ref line); if (token == JSONToken.NONE) { throw new JsonException("Invalid token found while parsing array.", index, line); } else if (token == JSONToken.COMMA) { NextToken(json, ref index, ref line); } else if (token == JSONToken.SQUARED_CLOSE) { NextToken(json, ref index, ref line); break; } else { object value = ParseValue(json, ref index, ref line); array.Add(value); } } return(array); }
public Boolean ParseHTTP(String ParameterName, HTTPRequest HTTPRequest, out Double Value, out HTTPResponse HTTPResp, String Context = null, Double DefaultValue = 0) { Object JSONToken; if (!TryGetValue(ParameterName, out JSONToken)) { Value = DefaultValue; HTTPResp = HTTPExtentions.CreateBadRequest(HTTPRequest, Context, ParameterName); return(false); } if (JSONToken != null) { if (!Double.TryParse(JSONToken.ToString(), NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-US"), out Value)) { Log.Timestamp("Bad request: Invalid \"" + ParameterName + "\" property value!"); Value = DefaultValue; HTTPResp = HTTPExtentions.CreateBadRequest(HTTPRequest, Context, ParameterName, JSONToken.ToString()); return(false); } } else { Value = 0; HTTPResp = null; return(false); } HTTPResp = null; return(true); }
/// <summary> /// /// </summary> /// <param name="reader"></param> public JSONReader(TextReader reader) { this.reader = reader; this.val = null; this.token = JSONToken.Null; this.location = JSONLocation.Normal; this.lastLocations = new Stack(); this.needProp = false; }
/// <summary> /// /// </summary> /// <param name="reader"></param> public JSONReader(TextReader reader) { this.reader = reader; this.val = null; this.token = JSONToken.Null; this.location = JSONLocation.Normal; this.lastLocations = new Stack(); this.needProp = false; }
private HttpResponseMessage ConstructedJSONToken(JSONToken jsonToken, params string[] responseParams) { var json = JsonConvert.SerializeObject(jsonToken); var response = Request.CreateResponse(HttpStatusCode.OK, "token"); response = AddCustomDataToHttpResponseHeader(response, responseParams); response.Content = new StringContent(json, Encoding.Unicode); return(response); }
/** * Utility function to stream tokens from a reader to a write, while providing a basic eventing model **/ private static void streamTokens(JSONParser fromStream, JSONGenerator toStream, JSONParserEvents events) { int depth = 0; while (fromStream.nextToken() != null) { // Give event handler chance to inject if (events != null) { events.nextToken(fromStream, depth, toStream); } // Forward to output stream JSONToken currentToken = fromStream.getCurrentToken(); if (currentToken == JSONToken.START_ARRAY) { toStream.writeStartArray(); depth++; } else if (currentToken == JSONToken.START_OBJECT) { toStream.writeStartObject(); depth++; } else if (currentToken == JSONToken.FIELD_NAME) { toStream.writeFieldName(fromStream.getCurrentName()); } else if (currentToken == JSONToken.VALUE_STRING || currentToken == JSONToken.VALUE_FALSE || currentToken == JSONToken.VALUE_TRUE || currentToken == JSONToken.VALUE_NUMBER_FLOAT || currentToken == JSONToken.VALUE_NUMBER_INT) { toStream.writeString(fromStream.getText()); } else if (currentToken == JSONToken.END_OBJECT) { toStream.writeEndObject(); depth--; } else if (currentToken == JSONToken.END_ARRAY) { toStream.writeEndArray(); depth--; } // Don't continue to stream beyond the initial starting point if (depth == 0) { break; } } }
private bool ReadNull() { this.token = JSONToken.Null; this.val = null; this.ReadAway(3); // ull this.ReadAway(); if (this.location == JSONLocation.InObject && !this.needProp) { this.needProp = true; } return(true); }
public void nextToken(JSONParser fromStream, int depth, JSONGenerator toStream) { // Inject children? JSONToken currentToken = fromStream.getCurrentToken(); if (depth == 2 && currentToken == JSONToken.END_OBJECT) { toStream.writeFieldName(relationshipName); toStream.writeStartObject(); toStream.writeNumberField("totalSize", children[childListIdx].size()); toStream.writeBooleanField("done", true); toStream.writeFieldName("records"); streamTokens(childrenParser, toStream, null); toStream.writeEndObject(); childListIdx++; } }
private static bool FindNext(JSONReader reader, JSONToken token) { if (reader.TokenType == token) { return(true); } while (reader.Read() && reader.TokenType != JSONToken.EndObject && reader.TokenType != JSONToken.EndArray) { if (reader.TokenType == token) { return(true); } } return(false); }
private bool ReadNumber(char start) { StringBuilder buff = new StringBuilder(); int chr; bool isFloat = false; this.token = JSONToken.Integer; buff.Append(start); while ((chr = this.reader.Peek()) != -1) { if (Char.IsNumber((char)chr) || (char)chr == '-' || (char)chr == '.') { if (((char)chr) == '.') { isFloat = true; } buff.Append((char)this.reader.Read()); } else { break; } } this.ReadAway(); if (isFloat) { this.token = JSONToken.Float; this.val = Convert.ToDouble(buff.ToString().Replace('.', ',')); } else { this.val = Convert.ToInt32(buff.ToString()); } if (this.location == JSONLocation.InObject && !this.needProp) { this.needProp = true; } return(true); }
private bool ReadBool(char chr) { this.token = JSONToken.Boolean; this.val = chr == 't'; if (chr == 't') { this.ReadAway(3); // rue } else { this.ReadAway(4); // alse } this.ReadAway(); if (this.location == JSONLocation.InObject && !this.needProp) { this.needProp = true; } return(true); }
public HttpResponseMessage Login(User user) { try { if (user.vendorname.ToLower() == "bookmyshow") { if (user.username.ToLower() == ConfigurationManager.AppSettings["bmsUserName"] && user.password.ToLower() == ConfigurationManager.AppSettings["bmsPassword"]) { var jsonToken = new JSONToken(); jsonToken.token = AuthHelper.CreateJSONWebToken(user.username, user.password, user.vendorname); return(ConstructedJSONToken(jsonToken, user.username)); } return(Request.CreateResponse(HttpStatusCode.Forbidden, "Invalid username or password")); } return(Request.CreateResponse(HttpStatusCode.Forbidden, "Invalid vendor name")); } catch (Exception ex) { Log.ErrorFormat("Error occured while getting theatre list: ", ex); throw new ServiceException(message: "Internal Server Error occured while processing the request", httpCode: HttpStatusCode.InternalServerError); } }
internal static object ParseValue(char[] json, ref int index, ref int line) { JSONToken token = LookAhead(json, index, ref line); switch (token) { case JSONToken.STRING: return(ParseString(json, ref index, ref line)); case JSONToken.NUMBER: return(ParseNumber(json, ref index, ref line)); case JSONToken.CURLY_OPEN: return(ParseObject(json, ref index, ref line)); case JSONToken.SQUARED_OPEN: return(ParseArray(json, ref index, ref line)); case JSONToken.TRUE: NextToken(json, ref index, ref line); return(Boolean.Parse("TRUE")); case JSONToken.FALSE: NextToken(json, ref index, ref line); return(Boolean.Parse("FALSE")); case JSONToken.NULL: NextToken(json, ref index, ref line); return(null); case JSONToken.NONE: break; } throw new JsonException("Invalid token found while parsing value.", index, line); }
internal void Parse(IList <JSONToken> tokens, ref int idx) { if ((tokens[idx] is StartBlock) == false) { throw new Exception("JSON object must start with a '{'"); } idx++; while (idx < tokens.Count) { JSONToken t1 = tokens[idx]; if (t1 is EndBlock) { // end of (maybe empty) object idx++; break; } if (idx + 3 >= tokens.Count) { // need at least 4 tokens for a field // <fieldname><:><value><,|}> throw new Exception("Invalid Json, looking for fieldname:"); } JSONToken t2 = tokens[idx + 1]; JSONToken t3 = tokens[idx + 2]; JSONToken t4 = tokens[idx + 3]; if (t1 is StartArray) { // object is an array JSONArray arr = new JSONArray(); arr.Parse(tokens, ref idx); Add("root", arr); t4 = tokens[idx]; if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } if (((t1 is StringLiteral) == false) || ((t2 is FieldNameToken) == false)) { throw new Exception("Invalid Json, looking for fieldname:"); } StringLiteral fieldName = t1 as StringLiteral; if (t3 is StringLiteral) { Add(fieldName.Value, new JSONStringField(((StringLiteral)t3).Value)); idx += 3; //move to the next token if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } else if (t3 is DoubleLiteral) { Add(fieldName.Value, new JSONNumericalField(((DoubleLiteral)t3).Value)); idx += 3; //move to the next token if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } else if (t3 is IntLiteral) { Add(fieldName.Value, new JSONNumericalField(((IntLiteral)t3).Value)); idx += 3; //move to the next token if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } else if (t3 is BoolLiteral) { Add(fieldName.Value, new JSONBoolField(((BoolLiteral)t3).Value)); idx += 3; //move to the next token if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } else if (t3 is NullLiteral) { Add(fieldName.Value, new JSONNullField()); idx += 3; //move to the next token if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } else if (t3 is StartBlock) { if (t4 is EndBlock) { // null object Add(fieldName.Value, new JSONNullField()); idx += 4; t4 = tokens[idx]; if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } } else { // field value is an object JSONObject obj = new JSONObject(); idx += 2; //skip over field name and ':' obj.Parse(tokens, ref idx); Add(fieldName.Value, obj); t4 = tokens[idx]; if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } } else if (t3 is StartArray) { if (t4 is EndArray) { // null object Add(fieldName.Value, new JSONNullField()); idx += 4; t4 = tokens[idx]; if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } } else { // field value is an object JSONArray arr = new JSONArray(); idx += 2; //skip over field name and ':' arr.Parse(tokens, ref idx); Add(fieldName.Value, arr); t4 = tokens[idx]; if ((t4 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t4 is EndBlock) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or '}'"); } } } else { throw new Exception("Unexpected tokens"); } } }
private bool ReadNumber(char start) { StringBuilder buff = new StringBuilder(); int chr; bool isFloat = false; this.token = JSONToken.Integer; buff.Append(start); while ((chr = this.reader.Peek()) != -1) { if (Char.IsNumber((char) chr) || (char) chr == '-' || (char) chr == '.') { if (((char) chr) == '.') isFloat = true; buff.Append((char) this.reader.Read()); } else break; } this.ReadAway(); if (isFloat) { this.token = JSONToken.Float; this.val = Convert.ToDouble(buff.ToString().Replace('.', ',')); } else this.val = Convert.ToInt32(buff.ToString()); if (this.location == JSONLocation.InObject && !this.needProp) this.needProp = true; return true; }
private bool ReadBool(char chr) { this.token = JSONToken.Boolean; this.val = chr == 't'; if (chr == 't') this.ReadAway(3); // rue else this.ReadAway(4); // alse this.ReadAway(); if (this.location == JSONLocation.InObject && !this.needProp) this.needProp = true; return true; }
private bool ReadString(char quote) { StringBuilder buff = new StringBuilder(); this.token = JSONToken.String; bool endString = false; int chr; while ((chr = this.reader.Peek()) != -1) { switch (chr) { case '\\': // Read away slash chr = this.reader.Read(); // Read escape code chr = this.reader.Read(); switch (chr) { case 't': buff.Append('\t'); break; case 'b': buff.Append('\b'); break; case 'f': buff.Append('\f'); break; case 'r': buff.Append('\r'); break; case 'n': buff.Append('\n'); break; case 'u': buff.Append((char) Convert.ToInt32(ReadLen(4), 16)); break; default: buff.Append((char) chr); break; } break; case '\'': case '"': if (chr == quote) endString = true; chr = this.reader.Read(); if (chr != -1 && chr != quote) buff.Append((char) chr); break; default: buff.Append((char) this.reader.Read()); break; } // String terminated if (endString) break; } this.ReadAway(); this.val = buff.ToString(); // Needed a property if (this.needProp) { this.token = JSONToken.PropertyName; this.needProp = false; return true; } if (this.location == JSONLocation.InObject && !this.needProp) this.needProp = true; return true; }
private bool ReadNull() { this.token = JSONToken.Null; this.val = null; this.ReadAway(3); // ull this.ReadAway(); if (this.location == JSONLocation.InObject && !this.needProp) this.needProp = true; return true; }
private static bool FindNext(JSONReader reader, JSONToken token) { if (reader.TokenType == token) return true; while (reader.Read() && reader.TokenType != JSONToken.EndObject && reader.TokenType != JSONToken.EndArray) { if (reader.TokenType == token) return true; } return false; }
/// <summary> /// /// </summary> /// <returns></returns> public bool Read() { int chr = this.reader.Read(); if (chr != -1) { switch ((char) chr) { case '[': this.lastLocations.Push(this.location); this.location = JSONLocation.InArray; this.token = JSONToken.StartArray; this.val = null; this.ReadAway(); return true; case ']': this.location = (JSONLocation) this.lastLocations.Pop(); this.token = JSONToken.EndArray; this.val = null; this.ReadAway(); if (this.location == JSONLocation.InObject) this.needProp = true; return true; case '{': this.lastLocations.Push(this.location); this.location = JSONLocation.InObject; this.needProp = true; this.token = JSONToken.StartObject; this.val = null; this.ReadAway(); return true; case '}': this.location = (JSONLocation) this.lastLocations.Pop(); this.token = JSONToken.EndObject; this.val = null; this.ReadAway(); if (this.location == JSONLocation.InObject) this.needProp = true; return true; // String case '"': case '\'': return this.ReadString((char) chr); // Null case 'n': return this.ReadNull(); // Bool case 't': case 'f': return this.ReadBool((char) chr); default: // Is number if (Char.IsNumber((char) chr) || (char) chr == '-' || (char) chr == '.') return this.ReadNumber((char) chr); return true; } } return false; }
// Token: 0x06000043 RID: 67 RVA: 0x00002CC0 File Offset: 0x00000EC0 protected bool LoadSettings() { global::AuthorCreationProject authorCreationProject; Stream stream = this.GetStream(true, "dat.asc", out authorCreationProject); if (stream != null) { try { using (JSONStream jsonstream = JSONStream.CreateWriter(stream)) { while (jsonstream.Read()) { JSONToken token = jsonstream.token; if (token == 1) { string text; while (jsonstream.ReadNextProperty(ref text)) { string text2 = text; if (text2 != null) { if (global::AuthorCreation.< > f__switch$map0 == null) { global::AuthorCreation.< > f__switch$map0 = new Dictionary <string, int>(2) { { "project", 0 }, { "settings", 1 } }; } int num; if (global::AuthorCreation.< > f__switch$map0.TryGetValue(text2, out num)) { if (num != 0) { if (num == 1) { this.LoadSettings(jsonstream); } } else { jsonstream.ReadSkip(); } } } } } } } return(true); } finally { stream.Dispose(); } return(false); } return(false); }
public void JSONTokenConstructorTest() { JSONToken target = new JSONToken(); Assert.Inconclusive("TODO: Implement code to verify target"); }
public void JSONTokenConstructorTest() { JSONToken target = new JSONToken(); Assert.Inconclusive("TODO: Implement code to verify target"); }
/// <summary> /// /// </summary> /// <returns></returns> public bool Read() { int chr = this.reader.Read(); if (chr != -1) { switch ((char)chr) { case '[': this.lastLocations.Push(this.location); this.location = JSONLocation.InArray; this.token = JSONToken.StartArray; this.val = null; this.ReadAway(); return(true); case ']': this.location = (JSONLocation)this.lastLocations.Pop(); this.token = JSONToken.EndArray; this.val = null; this.ReadAway(); if (this.location == JSONLocation.InObject) { this.needProp = true; } return(true); case '{': this.lastLocations.Push(this.location); this.location = JSONLocation.InObject; this.needProp = true; this.token = JSONToken.StartObject; this.val = null; this.ReadAway(); return(true); case '}': this.location = (JSONLocation)this.lastLocations.Pop(); this.token = JSONToken.EndObject; this.val = null; this.ReadAway(); if (this.location == JSONLocation.InObject) { this.needProp = true; } return(true); // String case '"': case '\'': return(this.ReadString((char)chr)); // Null case 'n': return(this.ReadNull()); // Bool case 't': case 'f': return(this.ReadBool((char)chr)); default: // Is number if (Char.IsNumber((char)chr) || (char)chr == '-' || (char)chr == '.') { return(this.ReadNumber((char)chr)); } return(true); } } return(false); }
private bool ReadString(char quote) { StringBuilder buff = new StringBuilder(); this.token = JSONToken.String; bool endString = false; int chr; while ((chr = this.reader.Peek()) != -1) { switch (chr) { case '\\': // Read away slash chr = this.reader.Read(); // Read escape code chr = this.reader.Read(); switch (chr) { case 't': buff.Append('\t'); break; case 'b': buff.Append('\b'); break; case 'f': buff.Append('\f'); break; case 'r': buff.Append('\r'); break; case 'n': buff.Append('\n'); break; case 'u': buff.Append((char)Convert.ToInt32(ReadLen(4), 16)); break; default: buff.Append((char)chr); break; } break; case '\'': case '"': if (chr == quote) { endString = true; } chr = this.reader.Read(); if (chr != -1 && chr != quote) { buff.Append((char)chr); } break; default: buff.Append((char)this.reader.Read()); break; } // String terminated if (endString) { break; } } this.ReadAway(); this.val = buff.ToString(); // Needed a property if (this.needProp) { this.token = JSONToken.PropertyName; this.needProp = false; return(true); } if (this.location == JSONLocation.InObject && !this.needProp) { this.needProp = true; } return(true); }
internal void Parse(IList <JSONToken> tokens, ref int idx) { if ((tokens[idx] is StartArray) == false) { throw new Exception("JSON array must start with a '['"); } idx++; while (idx < tokens.Count) { JSONToken t1 = tokens[idx]; if (t1 is EndArray) { // end of (maybe empty) array idx++; break; } if (idx + 1 >= tokens.Count) { // need at least 2 tokens for an array item // <value><,|]> throw new Exception("Invalid Json, looking for fieldname:"); } JSONToken t2 = tokens[idx + 1]; if (t1 is StringLiteral) { Add(new JSONStringField(((StringLiteral)t1).Value)); idx++; //move to the next token if ((t2 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t2 is EndArray) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or ']'"); } } else if (t1 is DoubleLiteral) { Add(new JSONNumericalField(((DoubleLiteral)t1).Value)); idx++; //move to the next token if ((t2 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t2 is EndArray) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or ']'"); } } else if (t1 is IntLiteral) { Add(new JSONNumericalField(((IntLiteral)t1).Value)); idx++; //move to the next token if ((t2 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t2 is EndArray) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or ']'"); } } else if (t1 is BoolLiteral) { Add(new JSONBoolField(((BoolLiteral)t1).Value)); idx++; //move to the next token if ((t2 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t2 is EndArray) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or ']'"); } } else if (t1 is NullLiteral) { Add(new JSONNullField()); idx++; //move to the next token if ((t2 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t2 is EndArray) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or ']'"); } } else if (t1 is StartBlock) { // field value is an object JSONObject obj = new JSONObject(); obj.Parse(tokens, ref idx); Add(obj); t2 = tokens[idx]; if ((t2 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t2 is EndArray) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or ']'"); } } else if (t1 is StartArray) { // field value is an object JSONArray arr = new JSONArray(); arr.Parse(tokens, ref idx); Add(arr); t2 = tokens[idx]; if ((t2 is FieldSeperatorToken)) { idx++; //move to the next field } else if (t2 is EndArray) { // end of object idx++; break; } else { throw new Exception("Invalid Json, looking for ',' or ']'"); } } else { throw new Exception("Unexpected tokens"); } } }
public Boolean ParseHTTP(String ParameterName, HTTPRequest HTTPRequest, out DateTime Value, out HTTPResponse HTTPResp, String Context = null, DateTime DefaultValue = default(DateTime)) { Object JSONToken; if (!TryGetValue(ParameterName, out JSONToken)) { Value = DefaultValue; HTTPResp = HTTPExtentions.CreateBadRequest(HTTPRequest, Context, ParameterName); return(false); } if (JSONToken != null) { try { Value = (DateTime)JSONToken; } catch (Exception) { Log.Timestamp("Bad request: Invalid \"" + ParameterName + "\" property value!"); Value = DefaultValue; HTTPResp = HTTPExtentions.CreateBadRequest(HTTPRequest, Context, ParameterName, JSONToken.ToString()); return(false); } } else { Value = DateTime.Now; HTTPResp = null; return(false); } HTTPResp = null; return(true); }