public void add(classJSONValue obj) { list_.Add(obj); }
// JSONのValueを取得 // PAIRの":"以降、ARRAYの","以降の文字列を入れる // <value>は、<string>,<number>,<object>,<array>,true,false,null // <string>は、先頭が'\"'のとき // <number>は、先頭が'-'か数字(0~9)のとき(e表記はとりあえず無視) private static classJSONValue getJSONValue(string json, ref int index) { classJSONValue result = new classJSONValue(); char charData; bool isEnd = false; // 先頭の空白を削除 while (!isEnd && json.Length > index) { charData = json[index]; switch (charData) { case '\n': //空白類文字は無視 case '\r': case '\t': case ' ': index++; break; default: isEnd = true; break; } } // 値の取得 charData = json[index]; switch (charData) { case '\"': // <string>の処理 string str = getString(json, ref index); if (str != "") { result.value = str; } isEnd = true; break; case 'n': // nullの処理 // 文字列が"null"ならnullと取り出す if (json.Substring(index, "null".Length) == "null") { result.setNull(); index += "null".Length; } else { // エラー } isEnd = true; break; case 't': // trueの処理 if (json.Substring(index, "true".Length) == "true") { result.value = true; index += "true".Length; } else { // エラー } isEnd = true; break; case 'f': // falseの処理 if (json.Substring(index, "false".Length) == "false") { result.value = false; index += "false".Length; } else { // エラー } isEnd = true; break; case '{': // OBJECT START result.value = getJSONObject(json, ref index); break; case '[': // ARRAY result.value = getJSONArray(json, ref index); break; case ',': // OBJECTの中かARRAYの中。どちらにしても、上に返す。 index++; isEnd = true; break; case '-': // <number>の処理 result.value = getNumber(json, ref index); isEnd = true; break; default: if (Char.IsDigit(charData)) { // 数値 result.value = getNumber(json, ref index); } else { // エラー } isEnd = true; break; } return result; }
// JSONのValueを取得 // PAIRの":"以降、ARRAYの","以降の文字列を入れる // <value>は、<string>,<number>,<object>,<array>,true,false,null // <string>は、先頭が'\"'のとき // <number>は、先頭が'-'か数字(0~9)のとき(e表記はとりあえず無視) private static classJSONValue getJSONValue(string json, ref int index) { classJSONValue result = new classJSONValue(); char charData; bool isEnd = false; // 先頭の空白を削除 while (!isEnd && json.Length > index) { charData = json[index]; switch (charData) { case '\n': //空白類文字は無視 case '\r': case '\t': case ' ': index++; break; default: isEnd = true; break; } } // 値の取得 charData = json[index]; switch (charData) { case '\"': // <string>の処理 string str = getString(json, ref index); if (str != "") { result.value = str; } isEnd = true; break; case 'n': // nullの処理 // 文字列が"null"ならnullと取り出す if (json.Substring(index, "null".Length) == "null") { result.setNull(); index += "null".Length; } else { // エラー } isEnd = true; break; case 't': // trueの処理 if (json.Substring(index, "true".Length) == "true") { result.value = true; index += "true".Length; } else { // エラー } isEnd = true; break; case 'f': // falseの処理 if (json.Substring(index, "false".Length) == "false") { result.value = false; index += "false".Length; } else { // エラー } isEnd = true; break; case '{': // OBJECT START result.value = getJSONObject(json, ref index); break; case '[': // ARRAY result.value = getJSONArray(json, ref index); break; case ',': // OBJECTの中かARRAYの中。どちらにしても、上に返す。 index++; isEnd = true; break; case '-': // <number>の処理 result.value = getNumber(json, ref index); isEnd = true; break; default: if (Char.IsDigit(charData)) { // 数値 result.value = getNumber(json, ref index); } else { // エラー } isEnd = true; break; } return(result); }
// <array> = [<value>,<value>,…] // 上位レベルで'['が出たらこれに放りこむ // ']'が出たらARRAYを閉じる private static classJSONArray getJSONArray(string json, ref int index) { classJSONArray result = new classJSONArray(); classJSONValue value = null; char charData; bool isEnd = false; // 初めの文字は'['であることが必須 if (json[index] != '[') { return(null); } index++; while (!isEnd && json.Length > index) { charData = json[index]; switch (charData) { case '\n': //空白類文字は無視 case '\r': case '\t': case ' ': index++; break; case ']': // ARRAYの終わり isEnd = true; index++; break; case '{': // OBJECT START case '\"': // PAIRの始まり value = getJSONValue(json, ref index); if (value != null) { result.add(value); } break; case ',': index++; value = getJSONValue(json, ref index); if (value != null) { result.add(value); } break; default: if (Char.IsDigit(charData)) { // 数値 value = getJSONValue(json, ref index); if (value != null) { result.add(value); } } else { // エラー isEnd = true; } break; } } return(result); }
// <object> = {<string>:<value>} // 上位レベルで'{'が見つかったら'{'の位置からこの関数に放りこむ //自分のレベルで'}'が見つかったらOBJECTの閉じる private static classJSONObject getJSONObject(string json, ref int index) { classJSONObject obj = null; classJSONPair pair = null; classJSONValue value = null; bool isEnd = false; char charData; ANALYZE_KIND state = ANALYZE_KIND.PAIR; // 初めの文字は'{'であることが必須 if (json[index] != '{') { return(null); } index++; obj = new classJSONObject(); while (!isEnd && json.Length > index) { charData = json[index]; switch (state) { case ANALYZE_KIND.PAIR: // PAIRの開始 // まずはname(string)が来るので、ダブルクォーテーションで囲まれている文字列を取得 switch (charData) { case '\"': state = ANALYZE_KIND.PAIR_NAME; //stringの開始 //index++; string str = getString(json, ref index); if (str != "") { pair = new classJSONPair(); pair.name = str; state = ANALYZE_KIND.PAIR_VALUE; // 次はVALUE } else { // エラー return(null); } break; case '\n': //空白類文字は無視 case '\r': case '\t': case ' ': index++; break; case ',': // 初回以外に出たらOBJECT中の次のPAIRが出現. index++; break; case '}': // Objectの終わり index++; isEnd = true; break; default: // エラー isEnd = true; break; } break; case ANALYZE_KIND.PAIR_VALUE: // PAIRのnameは取得済のはず // ':'が出たら次から値 switch (charData) { case '\n': //空白類文字は無視 case '\r': case '\t': case ' ': index++; break; case ':': // 空白を除き次にDQが出たらstring // '-'、数値が出てきたらnumber // "true"/"false" は bool // "null" は null index++; if (pair != null) { value = getJSONValue(json, ref index); pair.value = value; obj.set(pair); state = ANALYZE_KIND.PAIR; } break; default: // 空白以外はエラー isEnd = true; break; } break; } } return(obj); }