Created
January 23, 2018 05:55
-
-
Save Bak-Jin-Hyeong/161a507c8d2c6cad315967c1a1d8aab8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "jsmn.h" | |
struct JSMN_Verify_Stack_Element | |
{ | |
int offset; | |
int num_tokens; | |
}; | |
int JSMN_Verify(const jsmntok_t* tokens, const JSMN_Verify_Stack_Element param) | |
{ | |
if (!tokens || param.num_tokens < 0) | |
{ | |
return -1; | |
} | |
if (param.num_tokens == 0) | |
{ | |
return 0; | |
} | |
const auto& t = tokens[param.offset]; | |
const auto type = t.type; | |
const int num_children = t.size; | |
if (type == JSMN_OBJECT) | |
{ | |
int n = 0; | |
for (int i = 0; i < num_children; ++i) | |
{ | |
if (n + 2 >= param.num_tokens) | |
{ | |
return -1; | |
} | |
const auto key_index = param.offset + 1 + n; | |
const auto& key = tokens[key_index]; | |
if (key.type != JSMN_STRING) | |
{ | |
return -1; | |
} | |
int num_value_tokens = JSMN_Verify( | |
tokens, { key_index + 1, param.num_tokens - n - 1 }); | |
if (num_value_tokens < 0) | |
{ | |
return -1; | |
} | |
n += num_value_tokens + 1; | |
} | |
return n + 1; | |
} | |
else if (type == JSMN_ARRAY) | |
{ | |
int n = 0; | |
for (int i = 0; i < num_children; ++i) | |
{ | |
if (n + 1 >= param.num_tokens) | |
{ | |
return -1; | |
} | |
int num_value_tokens = JSMN_Verify( | |
tokens, { param.offset + 1 + n, param.num_tokens - n }); | |
if (num_value_tokens < 0) | |
{ | |
return -1; | |
} | |
n += num_value_tokens; | |
} | |
return n + 1; | |
} | |
else if (type == JSMN_STRING) | |
{ | |
return 1; | |
} | |
else if (type == JSMN_PRIMITIVE) | |
{ | |
return 1; | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment