/* Copyright (c) 2013 Daniele Bartolini, Michele Rossi Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "JSON.h" #include "List.h" #include "StringUtils.h" #include "DynamicString.h" namespace crown { namespace json { //----------------------------------------------------------------------------- static const char* next(const char* str, const char c = 0) { CE_ASSERT_NOT_NULL(str); if (c && c != (*str)) { CE_ASSERT(false, "Expected '%c' got '%c'", c, (*str)); } return str + 1; } //----------------------------------------------------------------------------- static const char* skip_whites(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; while ((*ch) && (*ch) <= ' ') ch = next(ch); return ch; } //----------------------------------------------------------------------------- static const char* skip_string(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; bool escaped = false; if ((*ch) == '"') { while ((*(ch = next(ch))) != 0) { if ((*ch) == '"' && !escaped) { ch = next(ch); return ch; } else if ((*ch) == '\\') escaped = true; else escaped = false; } } return ch; } //----------------------------------------------------------------------------- static const char* skip_number(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; while ((*ch) && (((*ch) >= '0' && (*ch) <= '9') || (*ch) == '-' || (*ch) == '.' || (*ch) == '+' || (*ch) == 'e' || (*ch) == 'E')) { ch = next(ch); } return ch; } //----------------------------------------------------------------------------- static const char* skip_object(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; uint32_t brackets = 1; if ((*ch) == '{') { brackets++; ch = next(ch, '{'); while ((*ch) && brackets != 1) { if ((*ch) == '}') brackets--; else if ((*ch) == '{') brackets++; ch = next(ch); } } return ch; } //----------------------------------------------------------------------------- static const char* skip_array(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; uint32_t brackets = 1; if ((*ch) == '[') { brackets++; ch = next(ch, '['); while ((*ch) && brackets != 1) { if ((*ch) == ']') brackets--; else if ((*ch) == '[') brackets++; ch = next(ch); } } return ch; } //----------------------------------------------------------------------------- static const char* skip_bool(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; switch ((*ch)) { case 't': { ch = next(ch, 't'); ch = next(ch, 'r'); ch = next(ch, 'u'); ch = next(ch, 'e'); break; } case 'f': { ch = next(ch, 'f'); ch = next(ch, 'a'); ch = next(ch, 'l'); ch = next(ch, 's'); ch = next(ch, 'e'); break; } default: { break; } } return ch; } //----------------------------------------------------------------------------- static const char* skip_null(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; if ((*ch) == 'n') { ch = next(ch, 'n'); ch = next(ch, 'u'); ch = next(ch, 'l'); ch = next(ch, 'l'); } return ch; } //----------------------------------------------------------------------------- static bool is_escapee(char c) { return c == '"' || c == '\\' || c == '/' || c == '\b' || c == '\f' || c == '\n' || c == '\r' || c == '\t'; } //----------------------------------------------------------------------------- JSONType::Enum type(const char* s) { CE_ASSERT_NOT_NULL(s); const char c = s[0]; switch (c) { case '{': return JSONType::OBJECT; case '[': return JSONType::ARRAY; case '"': return JSONType::STRING; case '-': return JSONType::NUMBER; default: return (c >= '0' && c <= '9') ? JSONType::NUMBER : (c == 'n' ? JSONType::NIL : JSONType::BOOL); } } //----------------------------------------------------------------------------- void parse_string(const char* s, DynamicString& str) { CE_ASSERT_NOT_NULL(s); const char* ch = s; if ((*ch) == '"') { while ((*(ch = next(ch)))) { // Empty string if ((*ch) == '"') { ch = next(ch); return; } else if ((*ch) == '\\') { ch = next(ch); if ((*ch) == 'u') { CE_FATAL("Not supported at the moment"); } else if (is_escapee(*ch)) { str += (*ch); } else { // Go to invalid string break; } } else { str += (*ch); } } } CE_FATAL("Bad string"); } //----------------------------------------------------------------------------- double parse_number(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; List str(default_allocator()); if ((*ch) == '-') { str.push_back('-'); ch = next(ch, '-'); } while ((*ch) >= '0' && (*ch) <= '9') { str.push_back((*ch)); ch = next(ch); } if ((*ch) == '.') { str.push_back('.'); while ((*(ch = next(ch))) && (*ch) >= '0' && (*ch) <= '9') { str.push_back(*ch); } } if ((*ch) == 'e' || (*ch) == 'E') { str.push_back(*ch); ch = next(ch); if ((*ch) == '-' || (*ch) == '+') { str.push_back(*ch); ch = next(ch); } while ((*ch) >= '0' && (*ch) <= '9') { str.push_back(*ch); ch = next(ch); } } // Ensure null terminated str.push_back('\0'); return string::parse_double(str.begin()); } //----------------------------------------------------------------------------- bool parse_bool(const char* s) { CE_ASSERT_NOT_NULL(s); const char* ch = s; switch(*ch) { case 't': { ch = next(ch, 't'); ch = next(ch, 'r'); ch = next(ch, 'u'); ch = next(ch, 'e'); return true; } case 'f': { ch = next(ch, 'f'); ch = next(ch, 'a'); ch = next(ch, 'l'); ch = next(ch, 's'); ch = next(ch, 'e'); return false; } default: { CE_FATAL("Bad boolean"); return false; } } } //----------------------------------------------------------------------------- int32_t parse_int(const char* s) { CE_ASSERT_NOT_NULL(s); return (int32_t) parse_number(s); } //----------------------------------------------------------------------------- float parse_float(const char* s) { CE_ASSERT_NOT_NULL(s); return (float) parse_number(s); } //----------------------------------------------------------------------------- void parse_array(const char* s, List& array) { CE_ASSERT_NOT_NULL(s); const char* ch = s; if ((*ch) == '[') { ch = next(ch, '['); // Skip whitespaces while ((*ch) && (*ch) <= ' ') { ch = next(ch); } if ((*ch) == ']') { ch = next(ch, ']'); return; } while (*ch) { array.push_back(ch); ch = skip_array(ch); ch = skip_object(ch); ch = skip_number(ch); ch = skip_string(ch); ch = skip_bool(ch); ch = skip_null(ch); ch = skip_whites(ch); // Closing bracket (top-most array) if ((*ch) == ']') { ch = next(ch, ']'); return; } // Skip until next ',' ch = next(ch, ','); // Skip whites, eventually ch = skip_whites(ch); } } CE_FATAL("Bad array"); } //----------------------------------------------------------------------------- void parse_object(const char* s, List& object) { CE_ASSERT_NOT_NULL(s); const char* ch = s; if ((*ch) == '{') { ch = next(ch, '{'); ch = skip_whites(ch); if ((*ch) == '}') { next(ch, '}'); return; } while (*ch) { JSONPair pair; pair.key = ch; // Skip any value ch = skip_array(ch); ch = skip_object(ch); ch = skip_number(ch); ch = skip_string(ch); ch = skip_bool(ch); ch = skip_null(ch); ch = skip_whites(ch); ch = next(ch, ':'); ch = skip_whites(ch); pair.val = ch; object.push_back(pair); // Skip any value ch = skip_array(ch); ch = skip_object(ch); ch = skip_number(ch); ch = skip_string(ch); ch = skip_bool(ch); ch = skip_null(ch); ch = skip_whites(ch); if ((*ch) == '}') { next(ch, '}'); return; } ch = next(ch, ','); ch = skip_whites(ch); } } CE_FATAL("Bad object"); } } // namespace json } // namespace crown