| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- /*
- 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 "Assert.h"
- #include "JSON.h"
- #include "Types.h"
- #include "List.h"
- #include "Allocator.h"
- #include <cstdio>
- namespace crown
- {
- static const char* g_current_char = NULL;
- //-----------------------------------------------------------------------------
- char JSON::next(const char* str, const char c, bool force_reset)
- {
- static char* current_str = (char*)str;
- static size_t index = 0;
- static char current = str[index];
- if (current_str != str || force_reset == true)
- {
- current_str = (char*)str;
- index = 0;
- current = str[index];
- }
- if (c && c != current)
- {
- CE_ASSERT(false, "Expected '%c' got '%c'", c, current);
- }
- index++;
- current = str[index];
- g_current_char = &str[index];
- return current;
- }
- //-----------------------------------------------------------------------------
- char JSON::skip_whites(const char* s)
- {
- char cur = (*g_current_char);
- while (cur && cur <= ' ') cur = next(s);
- return cur;
- }
- //-----------------------------------------------------------------------------
- char JSON::skip_string(const char* s)
- {
- char cur = (*g_current_char);
- bool escaped = false;
- if (cur == '"')
- {
- while ((cur = next(s)) != 0)
- {
- if (cur == '"' && !escaped)
- {
- cur = next(s);
- return cur;
- }
- else if (cur == '\\') escaped = true;
- else escaped = false;
- }
- }
- return cur;
- }
- //-----------------------------------------------------------------------------
- char JSON::skip_number(const char* s)
- {
- char cur = (*g_current_char);
- while (cur && ((cur >= '0' && cur <= '9') || cur == '-' || cur == '.' || cur == '+' ||
- cur == 'e' || cur == 'E')) cur = next(s);
- return cur;
- }
- //-----------------------------------------------------------------------------
- char JSON::skip_object(const char* s)
- {
- char cur = (*g_current_char);
- uint32_t brackets = 1;
- if (cur == '{')
- {
- brackets++;
- cur = next(s, '{');
- while (cur && brackets != 1)
- {
- if (cur == '}') brackets--;
- else if (cur == '{') brackets++;
- cur = next(s);
- }
- }
- return cur;
- }
- //-----------------------------------------------------------------------------
- char JSON::skip_array(const char* s)
- {
- char cur = (*g_current_char);
- uint32_t brackets = 1;
- if (cur == '[')
- {
- brackets++;
- cur = next(s, '[');
- while (cur && brackets != 1)
- {
- if (cur == ']') brackets--;
- else if (cur == '[') brackets++;
- cur = next(s);
- }
- }
- return cur;
- }
- //-----------------------------------------------------------------------------
- char JSON::skip_bool(const char* s)
- {
- char cur = (*g_current_char);
- switch (cur)
- {
- case 't':
- {
- cur = next(s, 't');
- cur = next(s, 'r');
- cur = next(s, 'u');
- cur = next(s, 'e');
- break;
- }
- case 'f':
- {
- cur = next(s, 'f');
- cur = next(s, 'a');
- cur = next(s, 'l');
- cur = next(s, 's');
- cur = next(s, 'e');
- break;
- }
- default:
- {
- break;
- }
- }
- return cur;
- }
- //-----------------------------------------------------------------------------
- bool JSON::is_escapee(char c)
- {
- return c == '"' || c == '\\' || c == '/' || c == '\b' || c == '\f' || c == '\n' ||
- c == '\r' || c == '\t';
- }
- //-----------------------------------------------------------------------------
- JSONType JSON::type(const char* s)
- {
- CE_ASSERT_NOT_NULL(s);
- switch (s[0])
- {
- case '{': return JT_OBJECT;
- case '[': return JT_ARRAY;
- case '"': return JT_STRING;
- case '-': return JT_NUMBER;
- default: return s[0] >= '0' && s[0] <= '9' ? JT_NUMBER : (s[0] == 'n' ? JT_NIL : JT_BOOL);
- }
- }
- //-----------------------------------------------------------------------------
- void JSON::parse_string(const char* s, List<char>& str)
- {
- CE_ASSERT_NOT_NULL(s);
- char cur = '\0';
- if (s[0] == '"')
- {
- while ((cur = next(s)) != 0)
- {
- if (cur == '"')
- {
- next(s);
- str.push_back('\0');
- return;
- }
- else if (cur == '\\')
- {
- cur = next(s);
- if (cur == 'u')
- {
- CE_ASSERT(false, "Not supported at the moment");
- }
- else if (is_escapee(cur))
- {
- str.push_back('\\');
- str.push_back(cur);
- }
- else
- {
- break; // to invalid string
- }
- }
- else
- {
- str.push_back(cur);
- }
- }
- }
- CE_ASSERT(false, "Not a valid string");
- }
- //-----------------------------------------------------------------------------
- double JSON::parse_number(const char* s)
- {
- CE_ASSERT_NOT_NULL(s);
- List<char> str(default_allocator());
- char cur = s[0];
- if (cur == '-')
- {
- str.push_back('-');
- cur = next(s, '-');
- }
- while (cur >= '0' && cur <= '9')
- {
- str.push_back(cur);
- cur = next(s);
- }
- if (cur == '.')
- {
- str.push_back('.');
- while ((cur = next(s)) != 0 && cur >= '0' && cur <= '9')
- {
- str.push_back(cur);
- }
- }
- if (cur == 'e' || cur == 'E')
- {
- str.push_back(cur);
- cur = next(s);
- if (cur == '-' || cur == '+')
- {
- str.push_back(cur);
- cur = next(s);
- }
- while (cur >= '0' && cur <= '9')
- {
- str.push_back(cur);
- cur = next(s);
- }
- }
- // Ensure null terminated
- str.push_back('\0');
- float number = 0.0f;
- // Fixme
- sscanf(str.begin(), "%f", &number);
- return number;
- }
- //-----------------------------------------------------------------------------
- bool JSON::parse_bool(const char* s)
- {
- CE_ASSERT_NOT_NULL(s);
- switch(s[0])
- {
- case 't':
- {
- next(s, 't');
- next(s, 'r');
- next(s, 'u');
- next(s, 'e');
- return true;
- }
- case 'f':
- {
- next(s, 'f');
- next(s, 'a');
- next(s, 'l');
- next(s, 's');
- next(s, 'e');
- return false;
- }
- default: break;
- }
- CE_ASSERT(false, "Not a boolean");
- }
- //-----------------------------------------------------------------------------
- int32_t JSON::parse_int(const char* s)
- {
- CE_ASSERT_NOT_NULL(s);
- return (int32_t) parse_number(s);
- }
- //-----------------------------------------------------------------------------
- float JSON::parse_float(const char* s)
- {
- CE_ASSERT_NOT_NULL(s);
- return (float) parse_number(s);
- }
- //-----------------------------------------------------------------------------
- void JSON::parse_array(const char* s, List<const char*>& array)
- {
- CE_ASSERT_NOT_NULL(s);
- char cur = s[0];
- if (cur == '[')
- {
- cur = next(s, '[');
- // Skip whitespaces
- while (cur && cur <= ' ')
- {
- cur = next(s);
- }
- if (cur == ']')
- {
- cur = next(s, ']');
- return;
- }
- while (cur)
- {
- array.push_back(g_current_char);
- cur = skip_array(s);
- cur = skip_object(s);
- cur = skip_number(s);
- cur = skip_string(s);
- cur = skip_bool(s);
- cur = skip_whites(s);
- // Closing bracket (top-most array)
- if (cur == ']')
- {
- cur = next(s, ']');
- return;
- }
- // Skip until next ','
- cur = next(s, ',');
- // Skip whites, eventually
- cur = skip_whites(s);
- }
- }
- CE_ASSERT(false, "Not an array");
- }
- //-----------------------------------------------------------------------------
- void JSON::parse_object(const char* s, List<JSONPair>& map)
- {
- CE_ASSERT_NOT_NULL(s);
- char cur = s[0];
- if (cur == '{')
- {
- cur = next(s, '{');
- cur = skip_whites(s);
- if (cur == '}')
- {
- next(s, '}');
- return;
- }
- while (cur)
- {
- JSONPair pair;
- pair.key = g_current_char;
- // Skip any value
- cur = skip_array(s);
- cur = skip_object(s);
- cur = skip_number(s);
- cur = skip_string(s);
- cur = skip_bool(s);
- cur = skip_whites(s);
- cur = next(s, ':');
- cur = skip_whites(s);
- pair.val = g_current_char;
- map.push_back(pair);
- // Skip any value
- cur = skip_array(s);
- cur = skip_object(s);
- cur = skip_number(s);
- cur = skip_string(s);
- cur = skip_bool(s);
- cur = skip_whites(s);
- if (cur == '}')
- {
- next(s, '}');
- return;
- }
- cur = next(s, ',');
- cur = skip_whites(s);
- }
- }
- CE_ASSERT(false, "Not an object");
- }
- } // namespace crown
|