| 333 | |
| 334 | |
| 335 | bool JSONReader::countArrayElement(UInt32& count) { |
| 336 | count = 0; |
| 337 | const UInt8* cur(current()); |
| 338 | const UInt8* end(cur+packet.available()); |
| 339 | UInt32 inner(0); |
| 340 | bool nothing(true); |
| 341 | bool done(false); |
| 342 | while (cur<end && (inner || *cur != ']')) { |
| 343 | |
| 344 | // skip string |
| 345 | if (*cur == '"') { |
| 346 | while (++cur<end && *cur != '"') { |
| 347 | if (*cur== '\\') { |
| 348 | if (++cur == end) |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | if(cur==end) { |
| 353 | packet.next(packet.available()); |
| 354 | ERROR("JSON malformed, marker \" end of text not found"); |
| 355 | return false; |
| 356 | } |
| 357 | nothing = false; |
| 358 | } else if (*cur == '{' || *cur == '[') { |
| 359 | ++inner; |
| 360 | nothing = true; |
| 361 | } else if (*cur == ']' || *cur == '}') { |
| 362 | if (inner == 0) { |
| 363 | packet.next(packet.available()); |
| 364 | ERROR("JSON malformed, marker ", *cur, " without beginning"); |
| 365 | return false; |
| 366 | } |
| 367 | --inner; |
| 368 | nothing = false; |
| 369 | } else if (*cur == ',') { |
| 370 | if (nothing) { |
| 371 | packet.next(packet.available()); |
| 372 | ERROR("JSON malformed, comma separator without value before"); |
| 373 | return false; |
| 374 | } |
| 375 | nothing = true; |
| 376 | if(inner==0) |
| 377 | done = false; |
| 378 | } else if (!isspace(*cur)) |
| 379 | nothing = false; |
| 380 | |
| 381 | if (inner == 0 && !done && !nothing) { |
| 382 | ++count; |
| 383 | done = true; |
| 384 | } |
| 385 | ++cur; |
| 386 | } |
| 387 | if (cur==end) { |
| 388 | ERROR("JSON malformed, marker ] end of array not found"); |
| 389 | return false; |
| 390 | } |
| 391 | return true; |
| 392 | } |