Variant f_json_decode(CStrRef json, bool assoc /* = false */, CVarRef options /* = 0 */) { if (json.empty()) { return null; } int64 json_options = options.toInt64();; if (options.isBoolean() && options.toBooleanVal()) { json_options = k_JSON_FB_LOOSE; } Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, (json_options & k_JSON_FB_LOOSE))) { return z; } if (json.size() == 4) { if (!strcasecmp(json.data(), "null")) return null; if (!strcasecmp(json.data(), "true")) return true; } else if (json.size() == 5 && !strcasecmp(json.data(), "false")) { return false; } int64 p; double d; DataType type = json->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { return p; } else if (type == KindOfDouble) { return d; } return null; }
void test_json(const char* json) { auto old_limit = tl_heap->getMemoryLimit(); tl_heap->setMemoryLimit(tl_heap->getStatsCopy().usage() + 0x10000); auto caught = false; req::vector<Variant> buf; ASSERT_LE(tl_heap->getStatsCopy().usage(), tl_heap->getMemoryLimit()); ASSERT_FALSE(getSurpriseFlag(MemExceededFlag)); auto len = strlen(json); try { for (int i = 0; i < 100000; i++) { // call into SimpleParser Variant z; auto ok = JSON_parser(z, json, len, true, 0xffff, k_JSON_FB_LOOSE); ASSERT_TRUE(ok); ASSERT_FALSE(getSurpriseFlag(MemExceededFlag)); buf.push_back(z); } } catch (RequestMemoryExceededException& e) { caught = true; } EXPECT_TRUE(caught); tl_heap->setMemoryLimit(old_limit); tl_heap->resetCouldOOM(); }
Variant f_json_decode(const String& json, bool assoc /* = false */, CVarRef options /* = 0 */) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); if (json.empty()) { return uninit_null(); } int64_t json_options = options.toInt64(); if (options.isBoolean() && options.toBooleanVal()) { json_options = k_JSON_FB_LOOSE; } const int64_t supported_options = k_JSON_FB_LOOSE | k_JSON_FB_COLLECTIONS | k_JSON_FB_STABLE_MAPS; Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, (json_options & supported_options))) { return z; } if (json.size() == 4) { if (!strcasecmp(json.data(), "null")) return uninit_null(); if (!strcasecmp(json.data(), "true")) return true; } else if (json.size() == 5 && !strcasecmp(json.data(), "false")) { return false; } int64_t p; double d; DataType type = json->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { return p; } else if (type == KindOfDouble) { return d; } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { return json.substr(1, json.size() - 2); } if ((json_options & k_JSON_FB_LOOSE) && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { return json.substr(1, json.size() - 2); } if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */ json_set_last_error_code(json_error_codes::JSON_ERROR_SYNTAX); } assert(json_get_last_error_code() != json_error_codes::JSON_ERROR_NONE); return uninit_null(); }
Variant f_json_decode(CStrRef json, bool assoc /* = false */, CVarRef options /* = 0 */) { if (json.empty()) { return uninit_null(); } int64_t json_options = options.toInt64(); if (options.isBoolean() && options.toBooleanVal()) { json_options = k_JSON_FB_LOOSE; } Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, (json_options & k_JSON_FB_LOOSE))) { return z; } if (json.size() == 4) { if (!strcasecmp(json.data(), "null")) return uninit_null(); if (!strcasecmp(json.data(), "true")) return true; } else if (json.size() == 5 && !strcasecmp(json.data(), "false")) { return false; } int64_t p; double d; DataType type = json->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { return p; } else if (type == KindOfDouble) { return d; } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { return json.substr(1, json.size() - 2); } if ((json_options & k_JSON_FB_LOOSE) && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { return json.substr(1, json.size() - 2); } if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */ return uninit_null(); } return json; }
Variant f_json_decode(CStrRef json, bool assoc /* = false */, bool loose /* = false */) { if (json.empty()) { return null; } Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, loose)) { return z; } if (json.size() == 4) { if (!strcasecmp(json.data(), "null")) return null; if (!strcasecmp(json.data(), "true")) return true; } else if (json.size() == 5 && !strcasecmp(json.data(), "false")) { return false; } int64 p; double d; DataType type = json->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { return p; } else if (type == KindOfDouble) { return d; } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { return json.substr(1, json.size() - 2); } if (loose && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { return json.substr(1, json.size() - 2); } if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */ return null; } return json; }
Variant f_json_decode(const String& json, bool assoc /* = false */, CVarRef options /* = 0 */) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); if (json.empty()) { return uninit_null(); } int64_t json_options = options.toInt64(); if (options.isBoolean() && options.toBooleanVal()) { json_options = k_JSON_FB_LOOSE; } const int64_t supported_options = k_JSON_FB_LOOSE | k_JSON_FB_COLLECTIONS | k_JSON_FB_STABLE_MAPS; int64_t parser_options = json_options & supported_options; Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, parser_options)) { return z; } if (json.size() == 4) { if (!strcasecmp(json.data(), "null")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return uninit_null(); } if (!strcasecmp(json.data(), "true")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return true; } } else if (json.size() == 5 && !strcasecmp(json.data(), "false")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return false; } int64_t p; double d; DataType type = json->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return p; } else if (type == KindOfDouble) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return d; } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); // Wrap the string in an array to allow the JSON_parser to handle // things like unicode escape sequences, then unwrap to get result String wrapped("["); wrapped += json + "]"; // Stick to a normal hhvm array for the wrapper const int64_t mask = ~(k_JSON_FB_COLLECTIONS | k_JSON_FB_STABLE_MAPS); if (JSON_parser(z, wrapped.data(), wrapped.size(), false, parser_options & mask) && z.isArray()) { Array arr = z.toArray(); if ((arr.size() == 1) && arr.exists(0)) { return arr[0]; } // The input string could be something like: "foo","bar" // Which will parse inside the [] wrapper, but be invalid json_set_last_error_code(json_error_codes::JSON_ERROR_SYNTAX); } } if ((json_options & k_JSON_FB_LOOSE) && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return json.substr(1, json.size() - 2); } if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */ json_set_last_error_code(json_error_codes::JSON_ERROR_SYNTAX); } assert(json_get_last_error_code() != json_error_codes::JSON_ERROR_NONE); return uninit_null(); }
TypedValue HHVM_FUNCTION(json_decode, const String& json, bool assoc, int64_t depth, int64_t options) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); if (json.empty()) { return make_tv<KindOfNull>(); } const int64_t supported_options = k_JSON_FB_LOOSE | k_JSON_FB_COLLECTIONS | k_JSON_FB_STABLE_MAPS | k_JSON_BIGINT_AS_STRING | k_JSON_FB_HACK_ARRAYS; int64_t parser_options = options & supported_options; Variant z; const auto ok = JSON_parser(z, json.data(), json.size(), assoc, depth, parser_options); if (UNLIKELY(StructuredLog::coinflip(RuntimeOption::EvalSerDesSampleRate))) { StructuredLog::logSerDes("json", "des", json, z); } if (ok) { return tvReturn(std::move(z)); } String trimmed = HHVM_FN(trim)(json, "\t\n\r "); if (trimmed.size() == 4) { if (!strcasecmp(trimmed.data(), "null")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return make_tv<KindOfNull>(); } if (!strcasecmp(trimmed.data(), "true")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return make_tv<KindOfBoolean>(true); } } else if (trimmed.size() == 5 && !strcasecmp(trimmed.data(), "false")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return make_tv<KindOfBoolean>(false); } int64_t p; double d; DataType type = json.get()->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return make_tv<KindOfInt64>(p); } else if (type == KindOfDouble) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); if ((options & k_JSON_BIGINT_AS_STRING) && (json.toInt64() == LLONG_MAX || json.toInt64() == LLONG_MIN) && errno == ERANGE) { // Overflow bool is_float = false; for (int i = (trimmed[0] == '-' ? 1 : 0); i < trimmed.size(); ++i) { if (trimmed[i] < '0' || trimmed[i] > '9') { is_float = true; break; } } if (!is_float) { return tvReturn(trimmed); } } return make_tv<KindOfDouble>(d); } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); // Wrap the string in an array to allow the JSON_parser to handle // things like unicode escape sequences, then unwrap to get result String wrapped("["); wrapped += json + "]"; // Stick to a normal hhvm array for the wrapper const int64_t mask = ~(k_JSON_FB_COLLECTIONS | k_JSON_FB_STABLE_MAPS); if (JSON_parser(z, wrapped.data(), wrapped.size(), false, depth, parser_options & mask) && z.isArray()) { Array arr = z.toArray(); if ((arr.size() == 1) && arr.exists(0)) { return tvReturn(arr[0]); } // The input string could be something like: "foo","bar" // Which will parse inside the [] wrapper, but be invalid json_set_last_error_code(json_error_codes::JSON_ERROR_SYNTAX); } } if ((options & k_JSON_FB_LOOSE) && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return tvReturn(json.substr(1, json.size() - 2)); } assert(json_get_last_error_code() != json_error_codes::JSON_ERROR_NONE); return make_tv<KindOfNull>(); }
Variant HHVM_FUNCTION(json_decode, const String& json, bool assoc /* = false */, int64_t depth /* = 512 */, int64_t options /* = 0 */) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); if (json.empty()) { return init_null(); } const int64_t supported_options = k_JSON_FB_LOOSE | k_JSON_FB_COLLECTIONS | k_JSON_FB_STABLE_MAPS | k_JSON_BIGINT_AS_STRING; int64_t parser_options = options & supported_options; Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, depth, parser_options)) { return z; } String trimmed = f_trim(json, "\t\n\r "); if (trimmed.size() == 4) { if (!strcasecmp(trimmed.data(), "null")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return init_null(); } if (!strcasecmp(trimmed.data(), "true")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return true; } } else if (trimmed.size() == 5 && !strcasecmp(trimmed.data(), "false")) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return false; } int64_t p; double d; DataType type = json.get()->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return p; } else if (type == KindOfDouble) { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); if ((options & k_JSON_BIGINT_AS_STRING) && (json.toInt64() == LLONG_MAX || json.toInt64() == LLONG_MIN) && errno == ERANGE) { // Overflow bool is_float = false; for (int i = (trimmed[0] == '-' ? 1 : 0); i < trimmed.size(); ++i) { if (trimmed[i] < '0' || trimmed[i] > '9') { is_float = true; break; } } if (!is_float) { return trimmed; } } return d; } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); // Wrap the string in an array to allow the JSON_parser to handle // things like unicode escape sequences, then unwrap to get result String wrapped("["); wrapped += json + "]"; // Stick to a normal hhvm array for the wrapper const int64_t mask = ~(k_JSON_FB_COLLECTIONS | k_JSON_FB_STABLE_MAPS); if (JSON_parser(z, wrapped.data(), wrapped.size(), false, depth, parser_options & mask) && z.isArray()) { Array arr = z.toArray(); if ((arr.size() == 1) && arr.exists(0)) { return arr[0]; } // The input string could be something like: "foo","bar" // Which will parse inside the [] wrapper, but be invalid json_set_last_error_code(json_error_codes::JSON_ERROR_SYNTAX); } } if ((options & k_JSON_FB_LOOSE) && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { json_set_last_error_code(json_error_codes::JSON_ERROR_NONE); return json.substr(1, json.size() - 2); } assert(json_get_last_error_code() != json_error_codes::JSON_ERROR_NONE); return init_null(); }