* Load json document from memory buffer. * @param pabyData Buffer.data. * @param nLength Buffer size. * @return true on success. If error occurred it can be received using * CPLGetLastErrorMsg method. * */
| 225 | * |
| 226 | */ |
| 227 | bool CPLJSONDocument::LoadMemory(const GByte *pabyData, int nLength) |
| 228 | { |
| 229 | if (nullptr == pabyData) |
| 230 | { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | if (m_poRootJsonObject) |
| 235 | json_object_put(TO_JSONOBJ(m_poRootJsonObject)); |
| 236 | |
| 237 | if (nLength == 4 && |
| 238 | memcmp(reinterpret_cast<const char *>(pabyData), "true", nLength) == 0) |
| 239 | { |
| 240 | m_poRootJsonObject = json_object_new_boolean(true); |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | if (nLength == 5 && |
| 245 | memcmp(reinterpret_cast<const char *>(pabyData), "false", nLength) == 0) |
| 246 | { |
| 247 | m_poRootJsonObject = json_object_new_boolean(false); |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | json_tokener *jstok = json_tokener_new(); |
| 252 | m_poRootJsonObject = json_tokener_parse_ex( |
| 253 | jstok, reinterpret_cast<const char *>(pabyData), nLength); |
| 254 | bool bParsed = jstok->err == json_tokener_success; |
| 255 | if (!bParsed) |
| 256 | { |
| 257 | CPLError(CE_Failure, CPLE_AppDefined, |
| 258 | "JSON parsing error: %s (at offset %d)", |
| 259 | json_tokener_error_desc(jstok->err), jstok->char_offset); |
| 260 | json_tokener_free(jstok); |
| 261 | return false; |
| 262 | } |
| 263 | json_tokener_free(jstok); |
| 264 | return bParsed; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Load json document from memory buffer. |