| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- /*
- 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 "container_types.h"
- #include "string_utils.h"
- #include "dynamic_string.h"
- #include "map.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 const char* skip_value(const char* s)
- {
- CE_ASSERT_NOT_NULL(s);
- const char* ch = s;
- ch = skip_array(ch);
- ch = skip_object(ch);
- ch = skip_number(ch);
- ch = skip_string(ch);
- ch = skip_bool(ch);
- ch = skip_null(ch);
- 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;
- Array<char> str(default_allocator());
- if ((*ch) == '-')
- {
- array::push_back(str, '-');
- ch = next(ch, '-');
- }
- while ((*ch) >= '0' && (*ch) <= '9')
- {
- array::push_back(str, (*ch));
- ch = next(ch);
- }
- if ((*ch) == '.')
- {
- array::push_back(str, '.');
- while ((*(ch = next(ch))) && (*ch) >= '0' && (*ch) <= '9')
- {
- array::push_back(str, *ch);
- }
- }
- if ((*ch) == 'e' || (*ch) == 'E')
- {
- array::push_back(str, *ch);
- ch = next(ch);
- if ((*ch) == '-' || (*ch) == '+')
- {
- array::push_back(str, *ch);
- ch = next(ch);
- }
- while ((*ch) >= '0' && (*ch) <= '9')
- {
- array::push_back(str, *ch);
- ch = next(ch);
- }
- }
- // Ensure null terminated
- array::push_back(str, '\0');
- return string::parse_double(array::begin(str));
- }
- 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, Array<const char*>& 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(array, ch);
- ch = skip_value(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, Map<DynamicString, const char*>& 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)
- {
- DynamicString key;
- parse_string(ch, key);
- ch = skip_string(ch);
- ch = skip_whites(ch);
- ch = next(ch, ':');
- ch = skip_whites(ch);
- map::set(object, key, ch);
- ch = skip_value(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
|