|
|
@@ -49,6 +49,8 @@ struct json_dec_t { |
|
|
|
size_t pairs =0; |
|
|
|
char* begin = nullptr; |
|
|
|
int s =0; |
|
|
|
bool str_value = false; // flag to indicate the value is string |
|
|
|
|
|
|
|
for (size_t i=0 ; i<size ; ++i) { |
|
|
|
switch (state) { |
|
|
|
case ST: |
|
|
@@ -99,26 +101,46 @@ struct json_dec_t { |
|
|
|
valid_ = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
if (!begin && std::isspace(buffer[i])) |
|
|
|
if (!begin && std::isspace(buffer[i])) // consume pre-spaces |
|
|
|
continue; |
|
|
|
else if (!begin && !std::isspace(buffer[i])) |
|
|
|
begin = (buffer[i] != '\"') ? (char*)&buffer[i] : (char*)&buffer[i+1]; |
|
|
|
else if (begin && (std::isspace(buffer[i]) || buffer[i] == ',' || buffer[i] == '}')) { |
|
|
|
s = (char*)&buffer[i] - begin; |
|
|
|
s -= (buffer[i-1] == '\"') ? 1:0; |
|
|
|
if (s > 0) |
|
|
|
pairs_[pairs].value = std::string_view{begin, (size_t)s}; |
|
|
|
else |
|
|
|
pairs_[pairs].value = std::string_view{}; |
|
|
|
++pairs; |
|
|
|
begin =nullptr; |
|
|
|
s =0; |
|
|
|
if (std::isspace(buffer[i])) |
|
|
|
else if (!begin && !std::isspace(buffer[i])) { // first character |
|
|
|
if (buffer[i] == '\"') { |
|
|
|
begin = (char*)&buffer[i+1]; |
|
|
|
str_value = true; |
|
|
|
} |
|
|
|
else { |
|
|
|
begin = (char*)&buffer[i]; |
|
|
|
str_value = false; |
|
|
|
} |
|
|
|
} |
|
|
|
else if (begin) { |
|
|
|
if (str_value && (buffer[i] == '\"')) { |
|
|
|
s = (char*)&buffer[i] - begin; |
|
|
|
if (s > 0) |
|
|
|
pairs_[pairs].value = std::string_view{begin, (size_t)s}; |
|
|
|
else |
|
|
|
pairs_[pairs].value = std::string_view{}; |
|
|
|
++pairs; |
|
|
|
begin =nullptr; |
|
|
|
s =0; |
|
|
|
state = SP; |
|
|
|
else if (buffer[i] == ',') |
|
|
|
state = KEY; |
|
|
|
else if (buffer[i] == '}') |
|
|
|
return; |
|
|
|
} |
|
|
|
else if (!str_value && (std::isspace(buffer[i]) || buffer[i] == ',' || buffer[i] == '}')) { |
|
|
|
s = (char*)&buffer[i] - begin; |
|
|
|
if (s > 0) |
|
|
|
pairs_[pairs].value = std::string_view{begin, (size_t)s}; |
|
|
|
else |
|
|
|
pairs_[pairs].value = std::string_view{}; |
|
|
|
++pairs; |
|
|
|
begin =nullptr; |
|
|
|
s =0; |
|
|
|
if (std::isspace(buffer[i])) |
|
|
|
state = SP; |
|
|
|
else if (buffer[i] == ',') |
|
|
|
state = KEY; |
|
|
|
else if (buffer[i] == '}') |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|