// <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; }
// <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); }