예제 #1
0
        public static JSONObject ReadFile(string path)
        {
            JSONTokenizer tokenizer = new JSONTokenizer(null, flag_res, BLANK, true);

            tokenizer.Read(path);
            return(ParseJSONObject(tokenizer));
        }
예제 #2
0
        public static JSONObject Read(char[] input)
        {
            JSONTokenizer tokenizer = new JSONTokenizer(input, flag_res, BLANK,
                                                        true);

            return(ParseJSONObject(tokenizer));
        }
예제 #3
0
        private static JSONObject ParseJSONObject(JSONTokenizer tokenizer)
        {
            JSONObject currentObject = null;
            string     currentKey = null;
            object     currentValue = null;
            bool       isNumber = true, isComma = false;
            State      state = JSONParser.State.START;

            for (; tokenizer.HasMoreTokens();)
            {
                string token = tokenizer.NextToken();
                if (LEFT_BRACE.Equals(token))
                {
                    if (state == JSONParser.State.START)
                    {
                        currentObject = new JSONObject();
                        state         = JSONParser.State.KEY;
                        isComma       = true;
                    }
                    else if (state == JSONParser.State.VALUE)
                    {
                        tokenizer.PutBackToken(token);
                        currentValue = ParseJSONObject(tokenizer);
                        isNumber     = false;
                        isComma      = false;
                    }
                    else
                    {
                        throw new System.Exception("expected JSON object after {");
                    }
                }
                else if (RIGHT_BRACE.Equals(token))
                {
                    if (currentKey != null && currentValue != null)
                    {
                        currentObject.Put(currentKey, currentValue);
                        isComma = false;
                    }
                    state = JSONParser.State.END;
                    return(currentObject);
                }
                else if (QUOT.Equals(token))
                {
                    if (state == JSONParser.State.VALUE)
                    {
                        isNumber = false;
                    }
                    isComma = false;
                }
                else if (COLON.Equals(token))
                {
                    if (state == JSONParser.State.KEY)
                    {
                        state = JSONParser.State.VALUE;
                    }
                    else
                    {
                        throw new Exception("expected key before :");
                    }
                }
                else if (COMMA.Equals(token))
                {
                    isComma = false;
                    if (state == JSONParser.State.VALUE)
                    {
                        if (currentKey != null && currentValue != null)
                        {
                            currentObject.Put(currentKey, currentValue);
                        }
                        else
                        {
                            throw new Exception("missing key or value");
                        }
                        state = JSONParser.State.KEY;
                    }
                    else
                    {
                        throw new Exception("unexpected ,");
                    }
                }
                else
                {
                    if (isComma)
                    {
                        if (state == JSONParser.State.KEY)
                        {
                            currentKey = token;
                        }
                        else if (state == JSONParser.State.VALUE)
                        {
                            currentValue = token;
                            isComma      = false;
                            isNumber     = false;
                        }
                    }
                    else if (state == JSONParser.State.KEY)
                    {
                        currentKey = token;
                    }
                    else if (state == JSONParser.State.VALUE)
                    {
                        if (isNumber)
                        {
                            if (TRUE.Equals(token, StringComparison.InvariantCultureIgnoreCase) ||
                                FALSE.Equals(token, StringComparison.InvariantCultureIgnoreCase))
                            {
                                currentValue = Boolean.Parse(token);
                            }
                            else if (MathUtils.IsNan(token))
                            {
                                try {
                                    currentValue = Single.Parse(token, JavaRuntime.NumberFormat);
                                } catch (FormatException) {
                                    throw new Exception(
                                              "non number or non boolean value must be enclosed within [\"]");
                                }
                            }
                        }
                        else
                        {
                            currentValue = token;
                        }
                        isNumber = true;
                    }
                }
            }
            if (state != JSONParser.State.END)
            {
                throw new Exception("missing }");
            }

            return(currentObject);
        }
예제 #4
0
		private static JSONObject ParseJSONObject(JSONTokenizer tokenizer) {
			JSONObject currentObject = null;
			string currentKey = null;
			object currentValue = null;
			bool isNumber = true, isComma = false;
			State state = JSONParser.State.START;
			for (; tokenizer.HasMoreTokens();) {
				string token = tokenizer.NextToken();
				if (LEFT_BRACE.Equals(token)) {
					if (state == JSONParser.State.START) {
						currentObject = new JSONObject();
						state = JSONParser.State.KEY;
						isComma = true;
					} else if (state == JSONParser.State.VALUE) {
						tokenizer.PutBackToken(token);
						currentValue = ParseJSONObject(tokenizer);
						isNumber = false;
						isComma = false;
					} else {
						throw new System.Exception("expected JSON object after {");
					}
				} else if (RIGHT_BRACE.Equals(token)) {
					if (currentKey != null && currentValue != null) {
						currentObject.Put(currentKey, currentValue);
						isComma = false;
					}
					state = JSONParser.State.END;
					return currentObject;
				} else if (QUOT.Equals(token)) {
					if (state == JSONParser.State.VALUE) {
						isNumber = false;
					}
					isComma = false;
				} else if (COLON.Equals(token)) {
					if (state == JSONParser.State.KEY) {
						state = JSONParser.State.VALUE;
					} else {
						throw new Exception("expected key before :");
					}
				} else if (COMMA.Equals(token)) {
					isComma = false;
					if (state == JSONParser.State.VALUE) {
						if (currentKey != null && currentValue != null) {
							currentObject.Put(currentKey, currentValue);
						} else {
							throw new Exception("missing key or value");
						}
						state = JSONParser.State.KEY;
					} else {
						throw new Exception("unexpected ,");
					}
				} else {
					if (isComma) {
						if (state == JSONParser.State.KEY) {
							currentKey = token;
						} else if (state == JSONParser.State.VALUE) {
							currentValue = token;
							isComma = false;
							isNumber = false;
						}
					} else if (state == JSONParser.State.KEY) {
						currentKey = token;
					} else if (state == JSONParser.State.VALUE) {
						if (isNumber) {
							if (TRUE.Equals(token,StringComparison.InvariantCultureIgnoreCase)
									|| FALSE.Equals(token,StringComparison.InvariantCultureIgnoreCase)) {
								currentValue = Boolean.Parse(token);
							} else if (MathUtils.IsNan(token)) {
								try {
									currentValue = Single.Parse(token,JavaRuntime.NumberFormat);
								} catch (FormatException) {
									throw new Exception(
											"non number or non boolean value must be enclosed within [\"]");
								}
							}
						} else {
							currentValue = token;
						}
						isNumber = true;
					}
				}
			}
			if (state != JSONParser.State.END) {
				throw new Exception("missing }");
			}
	
			return currentObject;
		}
예제 #5
0
		public static JSONObject ReadFile(string path) {
			JSONTokenizer tokenizer = new JSONTokenizer(null, flag_res, BLANK, true);
			tokenizer.Read(path);
			return ParseJSONObject(tokenizer);
		}
예제 #6
0
		public static JSONObject Read(char[] input) {
			JSONTokenizer tokenizer = new JSONTokenizer(input, flag_res, BLANK,
					true);
			return ParseJSONObject(tokenizer);
		}