/* JSONParser v1.2 Copyright (C) 2009 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener adiener@sacredsoftware.net */ #include "JSONParser.h" #include #include #include "UTFUtilities.h" JSONNode * JSONParser_parse(const char * jsonString, size_t length) { JSONNode * node; node = malloc(sizeof(JSONNode)); node->type = JSON_TYPE_NULL; node->key = NULL; if (!JSONParser_parseNode(jsonString, length, node, true, NULL)) { JSONParser_freeNodeContents(node); free(node); return NULL; } return node; } static bool unescapeStringInternal(const char * string, size_t length, char * outString, size_t * outLength) { size_t charIndex, unescapedCharIndex; bool escaping = false; size_t rememberedCharIndex; uint16_t * utf16String; uint8_t * utf8String; unsigned int utf16Char; size_t utf16Length, utf8Length; unescapedCharIndex = 0; for (charIndex = 0; charIndex < length; charIndex++) { if (!escaping && string[charIndex] == '\\') { escaping = true; } else { if (escaping) { escaping = false; switch (string[charIndex]) { case 'b': if (outString != NULL) { outString[unescapedCharIndex] = '\b'; } unescapedCharIndex++; break; case 'f': if (outString != NULL) { outString[unescapedCharIndex] = '\f'; } unescapedCharIndex++; break; case 'n': if (outString != NULL) { outString[unescapedCharIndex] = '\n'; } unescapedCharIndex++; break; case 'r': if (outString != NULL) { outString[unescapedCharIndex] = '\r'; } unescapedCharIndex++; break; case 't': if (outString != NULL) { outString[unescapedCharIndex] = '\t'; } unescapedCharIndex++; break; case 'u': rememberedCharIndex = charIndex - 1; while (charIndex < length) { if (charIndex >= (length - 4)) { return false; } charIndex += 4; if (charIndex < length - 2 && string[charIndex + 1] == '\\' && string[charIndex + 2] == 'u') { charIndex += 2; } else { break; } } utf16String = malloc(sizeof(uint16_t) * (charIndex - rememberedCharIndex + 1) / 6); utf16Length = 0; while (rememberedCharIndex < charIndex) { rememberedCharIndex += 2; if (!sscanf(string + rememberedCharIndex, "%4x", &utf16Char)) { free(utf16String); return false; } rememberedCharIndex += 4; utf16String[utf16Length++] = utf16Char; } if (!utf16StringIsWellFormed(utf16String, utf16Length)) { free(utf16String); return false; } utf8String = utf16StringToUTF8String(utf16String, utf16Length); free(utf16String); utf8Length = utf8StringLength(utf8String); if (outString != NULL) { strncpy(outString + unescapedCharIndex, (char *) utf8String, utf8Length); } unescapedCharIndex += utf8Length; free(utf8String); break; default: if (outString != NULL) { outString[unescapedCharIndex] = string[charIndex]; } unescapedCharIndex++; break; } } else { if (outString != NULL) { outString[unescapedCharIndex] = string[charIndex]; } unescapedCharIndex++; } } } if (outString != NULL) { outString[unescapedCharIndex] = '\x00'; } if (outLength != NULL) { *outLength = unescapedCharIndex; } return true; } static char * unescapeString(const char * string, size_t length) { char * unescapedString; size_t unescapedLength; if (!unescapeStringInternal(string, length, NULL, &unescapedLength)) { return NULL; } unescapedString = malloc(unescapedLength + 1); unescapeStringInternal(string, length, unescapedString, NULL); return unescapedString; } enum { STATE_SEEK_ROOT, STATE_SEEK_KEY, STATE_FOUND_KEY, STATE_SEEK_COLON, STATE_SEEK_VALUE, STATE_FOUND_STRING, STATE_FOUND_OTHER_VALUE, STATE_SEEK_COMMA_OR_END_OF_LIST, STATE_SEEK_EOF }; bool JSONParser_parseNode(const char * jsonString, size_t length, JSONNode * outNode, bool expectEOF, size_t * outCharsConsumed) { size_t charIndex; int state; size_t childListSize = 0; size_t rememberedCharIndex = 0; char * key = NULL; JSONNode childNode; size_t lengthAdvanced; bool escaping = false; state = STATE_SEEK_ROOT; for (charIndex = 0; charIndex < length; charIndex++) { switch (state) { case STATE_SEEK_ROOT: switch (jsonString[charIndex]) { case ' ': case '\t': case '\x0A': case '\x0D': break; case '{': outNode->type = JSON_TYPE_OBJECT; outNode->numberOfChildren = 0; outNode->children = NULL; state = STATE_SEEK_KEY; break; case '[': outNode->type = JSON_TYPE_ARRAY; outNode->numberOfChildren = 0; outNode->children = NULL; state = STATE_SEEK_VALUE; break; default: return false; } break; case STATE_SEEK_KEY: switch (jsonString[charIndex]) { case ' ': case '\t': case '\x0A': case '\x0D': break; case '"': rememberedCharIndex = charIndex + 1; if (outNode->children == NULL) { childListSize = 8; outNode->children = malloc(sizeof(JSONNode) * childListSize); } state = STATE_FOUND_KEY; break; case '}': if (outNode->type != JSON_TYPE_OBJECT) { return false; } if (!expectEOF) { if (outCharsConsumed != NULL) { *outCharsConsumed = charIndex; } return true; } state = STATE_SEEK_EOF; break; case ']': if (outNode->type != JSON_TYPE_ARRAY) { return false; } if (!expectEOF) { if (outCharsConsumed != NULL) { *outCharsConsumed = charIndex; } return true; } state = STATE_SEEK_EOF; break; default: return false; } break; case STATE_FOUND_KEY: switch (jsonString[charIndex]) { case '\\': escaping = !escaping; break; case '"': if (escaping) { escaping = false; break; } escaping = false; key = unescapeString(jsonString + rememberedCharIndex, charIndex - rememberedCharIndex); if (key == NULL) { return false; } state = STATE_SEEK_COLON; break; default: escaping = false; break; } break; case STATE_SEEK_COLON: switch (jsonString[charIndex]) { case ' ': case '\t': case '\x0A': case '\x0D': break; case ':': state = STATE_SEEK_VALUE; break; default: free(key); return false; } break; case STATE_SEEK_VALUE: switch (jsonString[charIndex]) { case ' ': case '\t': case '\x0A': case '\x0D': break; case '"': rememberedCharIndex = charIndex + 1; if (outNode->children == NULL) { childListSize = 8; outNode->children = malloc(sizeof(JSONNode) * childListSize); } state = STATE_FOUND_STRING; break; case 't': case 'f': case 'n': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': case '.': rememberedCharIndex = charIndex; if (outNode->children == NULL) { childListSize = 8; outNode->children = malloc(sizeof(JSONNode) * childListSize); } state = STATE_FOUND_OTHER_VALUE; break; case '{': case '[': if (JSONParser_parseNode(jsonString + charIndex, length - charIndex, &childNode, false, &lengthAdvanced)) { charIndex += lengthAdvanced; childNode.key = key; key = NULL; if (outNode->children == NULL) { childListSize = 8; outNode->children = malloc(sizeof(JSONNode) * childListSize); } if (outNode->numberOfChildren >= childListSize) { childListSize *= 2; outNode->children = realloc(outNode->children, sizeof(JSONNode) * childListSize); } outNode->children[outNode->numberOfChildren++] = childNode; state = STATE_SEEK_COMMA_OR_END_OF_LIST; } else { if (key != NULL) { free(key); } return false; } break; case ']': if (outNode->type != JSON_TYPE_ARRAY) { return false; } if (!expectEOF) { if (outCharsConsumed != NULL) { *outCharsConsumed = charIndex; } return true; } state = STATE_SEEK_EOF; break; default: if (key != NULL) { free(key); } return false; } break; case STATE_FOUND_STRING: switch (jsonString[charIndex]) { case '\\': escaping = !escaping; break; case '"': if (escaping) { escaping = false; break; } escaping = false; childNode.type = JSON_TYPE_STRING; childNode.key = key; key = NULL; childNode.value.string = unescapeString(jsonString + rememberedCharIndex, charIndex - rememberedCharIndex); if (childNode.value.string == NULL) { free(childNode.key); return false; } if (outNode->numberOfChildren >= childListSize) { childListSize *= 2; outNode->children = realloc(outNode->children, sizeof(JSONNode) * childListSize); } outNode->children[outNode->numberOfChildren++] = childNode; state = STATE_SEEK_COMMA_OR_END_OF_LIST; break; default: escaping = false; break; } break; case STATE_FOUND_OTHER_VALUE: switch (jsonString[charIndex]) { case 'a': case 'e': case 'l': case 'r': case 's': case 'u': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'E': case '+': case '-': case '.': break; default: if (!strncmp(jsonString + rememberedCharIndex, "true", 4)) { childNode.type = JSON_TYPE_BOOLEAN; childNode.key = key; key = NULL; childNode.value.boolean = true; if (outNode->numberOfChildren >= childListSize) { childListSize *= 2; outNode->children = realloc(outNode->children, sizeof(JSONNode) * childListSize); } outNode->children[outNode->numberOfChildren++] = childNode; } else if (!strncmp(jsonString + rememberedCharIndex, "false", 5)) { childNode.type = JSON_TYPE_BOOLEAN; childNode.key = key; key = NULL; childNode.value.boolean = false; if (outNode->numberOfChildren >= childListSize) { childListSize *= 2; outNode->children = realloc(outNode->children, sizeof(JSONNode) * childListSize); } outNode->children[outNode->numberOfChildren++] = childNode; } else if (!strncmp(jsonString + rememberedCharIndex, "null", 4)) { childNode.type = JSON_TYPE_NULL; childNode.key = key; key = NULL; if (outNode->numberOfChildren >= childListSize) { childListSize *= 2; outNode->children = realloc(outNode->children, sizeof(JSONNode) * childListSize); } outNode->children[outNode->numberOfChildren++] = childNode; } else { char * endPtr; childNode.value.number = strtod(jsonString + rememberedCharIndex, &endPtr); if (endPtr != jsonString + charIndex) { if (key != NULL) { free(key); } return false; } childNode.type = JSON_TYPE_NUMBER; childNode.key = key; key = NULL; if (outNode->numberOfChildren >= childListSize) { childListSize *= 2; outNode->children = realloc(outNode->children, sizeof(JSONNode) * childListSize); } outNode->children[outNode->numberOfChildren++] = childNode; } state = STATE_SEEK_COMMA_OR_END_OF_LIST; charIndex--; break; } break; case STATE_SEEK_COMMA_OR_END_OF_LIST: switch (jsonString[charIndex]) { case ' ': case '\t': case '\x0A': case '\x0D': break; case '}': if (outNode->type != JSON_TYPE_OBJECT) { return false; } if (!expectEOF) { if (outCharsConsumed != NULL) { *outCharsConsumed = charIndex; } return true; } state = STATE_SEEK_EOF; break; case ']': if (outNode->type != JSON_TYPE_ARRAY) { return false; } if (!expectEOF) { if (outCharsConsumed != NULL) { *outCharsConsumed = charIndex; } return true; } state = STATE_SEEK_EOF; break; case ',': if (outNode->type == JSON_TYPE_OBJECT) { state = STATE_SEEK_KEY; } else { state = STATE_SEEK_VALUE; } break; default: return false; } break; case STATE_SEEK_EOF: switch (jsonString[charIndex]) { case ' ': case '\t': case '\x0A': case '\x0D': break; default: return false; } } } if (outCharsConsumed != NULL) { *outCharsConsumed = charIndex; } return (state == STATE_SEEK_EOF); } void JSONParser_freeNodeContents(JSONNode * node) { size_t childIndex; switch (node->type) { case JSON_TYPE_OBJECT: case JSON_TYPE_ARRAY: for (childIndex = 0; childIndex < node->numberOfChildren; childIndex++) { JSONParser_freeNodeContents(&node->children[childIndex]); } if (node->children != NULL) { free(node->children); } break; case JSON_TYPE_STRING: if (node->value.string != NULL) { free(node->value.string); } break; default: break; } if (node->key != NULL) { free(node->key); } }