|
| 1 | +#include "../test.h" |
| 2 | + |
| 3 | +#include "ujson4c/src/ujdecode.h" |
| 4 | + |
| 5 | +extern "C" { |
| 6 | + |
| 7 | +static void GenStat(Stat* s, UJObject v) { |
| 8 | + switch (UJGetType(v)) { |
| 9 | + case UJT_Object: |
| 10 | + { |
| 11 | + void* iter = UJBeginObject(v); |
| 12 | + UJString key; |
| 13 | + UJObject value; |
| 14 | + while (UJIterObject(&iter, &key, &value)) { |
| 15 | + s->memberCount++; |
| 16 | + s->stringCount++; |
| 17 | + s->stringLength += key.cchLen; |
| 18 | + GenStat(s, value); |
| 19 | + } |
| 20 | + s->objectCount++; |
| 21 | + } |
| 22 | + break; |
| 23 | + |
| 24 | + case UJT_Array: |
| 25 | + { |
| 26 | + void* iter = UJBeginArray(v); |
| 27 | + UJObject value; |
| 28 | + while (UJIterArray(&iter, &value)) { |
| 29 | + GenStat(s, value); |
| 30 | + s->elementCount++; |
| 31 | + } |
| 32 | + s->arrayCount++; |
| 33 | + } |
| 34 | + break; |
| 35 | + |
| 36 | + case UJT_String: |
| 37 | + { |
| 38 | + size_t length; |
| 39 | + UJReadString(v, &length); |
| 40 | + s->stringCount++; |
| 41 | + s->stringLength += length; |
| 42 | + } |
| 43 | + break; |
| 44 | + |
| 45 | + case UJT_Long: |
| 46 | + case UJT_LongLong: |
| 47 | + case UJT_Double: |
| 48 | + s->numberCount++; break; |
| 49 | + |
| 50 | + case UJT_True: s->trueCount++; break; |
| 51 | + case UJT_False: s->falseCount++; break; |
| 52 | + case UJT_Null: s->nullCount++; break; |
| 53 | + default:; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +} // extern "C" |
| 58 | + |
| 59 | +class Ujson4cParseResult : public ParseResultBase { |
| 60 | +public: |
| 61 | + Ujson4cParseResult() : root(), state() {} |
| 62 | + ~Ujson4cParseResult() { UJFree(state); } |
| 63 | + |
| 64 | + UJObject root; |
| 65 | + void* state; |
| 66 | +}; |
| 67 | + |
| 68 | +class Ujson4c : public TestBase { |
| 69 | +public: |
| 70 | + Ujson4c() : TestBase("Ujson4c") { |
| 71 | + } |
| 72 | + |
| 73 | + virtual ParseResultBase* Parse(const char* json, size_t length) const { |
| 74 | + (void)length; |
| 75 | + Ujson4cParseResult* pr = new Ujson4cParseResult; |
| 76 | + pr->root = UJDecode(json, length, NULL, &pr->state); |
| 77 | + return pr; |
| 78 | + } |
| 79 | + |
| 80 | + virtual bool Statistics(const ParseResultBase* parseResult, Stat* stat) const { |
| 81 | + const Ujson4cParseResult* pr = static_cast<const Ujson4cParseResult*>(parseResult); |
| 82 | + memset(stat, 0, sizeof(Stat)); |
| 83 | + GenStat(stat, pr->root); |
| 84 | + return true; |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +REGISTER_TEST(Ujson4c); |
0 commit comments