Rapid Json NEXT Rapid Json https github commiloyiprapidjson

Rapid. Json NEXT 김명찬









Rapid. Json - 설치법 • https: //github. com/miloyip/rapidjson/ • Include 폴더의 내용을 본인 Include 폴더에 복사하면 끝. #include "rapidjson/document. h" using namespace rapidjson;

Rapid. Json – 기본 사용. • root 는 오브젝트로 생성됨. • Value 클래스로 모든값이 저장됨. #include "rapidjson/document. h" using namespace rapidjson; char json[] =. . . Document document; document. Parse(json); char json[] = R"( { "hello": "world", "t" : true, "f" : false, "n" : null, "i" : 123, "pi" : 3. 1416, "a" : [1, 2, 3, 4] })";

![Rapid. Json - string assert(document. Is. Object()); assert(document. Has. Member("hello")); assert(document["hello"]. Is. String() == Rapid. Json - string assert(document. Is. Object()); assert(document. Has. Member("hello")); assert(document["hello"]. Is. String() ==](http://slidetodoc.com/presentation_image_h/be1a456d66ed7d215e53715a5865b469/image-13.jpg)
Rapid. Json - string assert(document. Is. Object()); assert(document. Has. Member("hello")); assert(document["hello"]. Is. String() == true); char json[] = R"( { "hello": "world", "t" : true, "f" : false, "n" : null, "i" : 123, "pi" : 3. 1416, "a" : [1, 2, 3, 4] })"; Document document; document. Parse(json); sprintf(json, "%s", document["hello"]. Get. String()); Multi. Byte. To. Wide. Char(CP_ACP, 0, json, -1, temp, strlen(json)); Text. Out(memory. DC, 10, temp, lstrlen(temp));
![Rapid. Json - bool assert(document["t"]. Is. Bool()); assert(document["t"]. Get. Bool() == true); char json[] Rapid. Json - bool assert(document["t"]. Is. Bool()); assert(document["t"]. Get. Bool() == true); char json[]](http://slidetodoc.com/presentation_image_h/be1a456d66ed7d215e53715a5865b469/image-14.jpg)
Rapid. Json - bool assert(document["t"]. Is. Bool()); assert(document["t"]. Get. Bool() == true); char json[] = R"( { "hello": "world", "t" : true, "f" : false, "n" : null, "i" : 123, "pi" : 3. 1416, "a" : [1, 2, 3, 4] })"; Document document; document. Parse(json); sprintf(json, "%s", document["t"]. Get. Bool() ? "True" : "False"); Multi. Byte. To. Wide. Char(CP_ACP, 0, json, -1, temp, strlen(json)); Text. Out(memory. DC, 10, temp, lstrlen(temp));
![Rapid. Json - null assert(document["n"]. Is. Null() == true); char json[] = R"( { Rapid. Json - null assert(document["n"]. Is. Null() == true); char json[] = R"( {](http://slidetodoc.com/presentation_image_h/be1a456d66ed7d215e53715a5865b469/image-15.jpg)
Rapid. Json - null assert(document["n"]. Is. Null() == true); char json[] = R"( { "hello": "world", "t" : true, "f" : false, "n" : null, "i" : 123, "pi" : 3. 1416, "a" : [1, 2, 3, 4] })"; Document document; document. Parse(json); sprintf(json, "%s", document["n"]. Is. Null() ? "True" : "False"); Multi. Byte. To. Wide. Char(CP_ACP, 0, json, -1, temp, strlen(json)); Text. Out(memory. DC, 10, temp, lstrlen(temp));
![Rapid. Json - number char json[] = R"( { "hello": "world", "t" : true, Rapid. Json - number char json[] = R"( { "hello": "world", "t" : true,](http://slidetodoc.com/presentation_image_h/be1a456d66ed7d215e53715a5865b469/image-16.jpg)
Rapid. Json - number char json[] = R"( { "hello": "world", "t" : true, "f" : false, "n" : null, "i" : 123, "pi" : 3. 1416, "a" : [1, 2, 3, 4] })"; assert(document["i"]. Is. Number()); // In this case, Is. Uint()/Is. Int 64()/Is. UInt 64() also return true. Document document; assert(document["i"]. Is. Int()); document. Parse(json); // Alternative (int)document["i"] assert(document["pi"]. Is. Number()); assert(document["pi"]. Is. Double());
![Rapid. Json - array assert(document["a"]. Is. Array()); // Using a reference for consecutive access Rapid. Json - array assert(document["a"]. Is. Array()); // Using a reference for consecutive access](http://slidetodoc.com/presentation_image_h/be1a456d66ed7d215e53715a5865b469/image-17.jpg)
Rapid. Json - array assert(document["a"]. Is. Array()); // Using a reference for consecutive access // is handy and faster. const Value& a = document["a"]; assert(a. Is. Array()); char json[] = R"( { "hello": "world", "t" : true, "f" : false, "n" : null, "i" : 123, "pi" : 3. 1416, "a" : [1, 2, 3, 4] })"; Document document; document. Parse(json); for (Size. Type i = 0; i < a. Size(); i++) // Uses Size. Type instead of size_t { char tmp. Json[50]; sprintf(tmp. Json , "a[%d] = %d ", i, a[i]. Get. Int()); strcat(json, tmp. Json); }

Rapid. Json - array for (Size. Type i = 0; i < a. Size(); i++) // Uses Size. Type instead of size_t { char tmp. Json[50]; sprintf(tmp. Json , "a[%d] = %d ", i, a[i]. Get. Int()); strcat(json, tmp. Json); } for (auto itr = a. Begin(); itr != a. End(); ++itr) { char tmp. Json[50]; sprintf(tmp. Json, "%d ", itr->Get. Int()); strcat(json, tmp. Json); } • Size. Type Capacity() const • bool Empty() const
![Rapid. Json – typename(enum) static const char* k. Type. Names[] = { "Null", "False", Rapid. Json – typename(enum) static const char* k. Type. Names[] = { "Null", "False",](http://slidetodoc.com/presentation_image_h/be1a456d66ed7d215e53715a5865b469/image-19.jpg)
Rapid. Json – typename(enum) static const char* k. Type. Names[] = { "Null", "False", "True", "Object", "Array", "String", "Number" }; sprintf(json, "%s", k. Type. Names[a. Get. Type()]); auto tmp = a. Get. Type(); //typename을 확인하고 싶다면. . auto 위에 마우스 커서를. . . sprintf(json, "%s", k. Type. Names[tmp]); • Enum으로 처리됩니다! • bool 대신 False, True가 있는 것이 특이하네요. .

Rapid. Json - 검색 auto itr = document. Find. Member("hello"); if (itr != document. Member. End()) // End() means cannot find sprintf(json, "%s ", itr->value. Get. String());

Rapid. Json - Number • JSON은 Number라는 단독 타입으로 숫자를 넘김. • 파서가 각 값들의 타입을 결정한다. • Rapid. Json은 여러 가지 타입으로 숫자를 저장하기 때문에 숫자를 꺼낼 때는 해당 타입이 가능한지 질의해 보는 것이 좋은 방법. Checking bool Is. Number() bool Is. Uint() bool Is. Int() bool Is. Uint 64() bool Is. Int 64() bool Is. Double() Obtaining N/A unsigned Get. Uint() int Get. Int() uint 64_t Get. Uint 64() int 64_t Get. Int 64() double Get. Double()

Rapid. Json – Number • 예를들어 • x = 123 x. Is. Int() == x. Is. Uint() == x. Is. Int 64() == x. Is. Uint 64() == true • x = -3, 000, 000 x. Is. Int 64() == true

![Rapid. Json - comparision • Value 클래스에 != , == 사용 가능 if (document["hello"] Rapid. Json - comparision • Value 클래스에 != , == 사용 가능 if (document["hello"]](http://slidetodoc.com/presentation_image_h/be1a456d66ed7d215e53715a5865b469/image-24.jpg)
Rapid. Json - comparision • Value 클래스에 != , == 사용 가능 if (document["hello"] == document["n"]) /*. . . */; // Compare values if (document["hello"] == "world") /*. . . */; // Compare value with literal string if (document["i"] != 123) /*. . . */; // Compare with integers if (document["pi"] != 3. 14) /*. . . */; // Compare with double.

Rapid. Json – modifying DOM Document d; // Null d. Set. Object(); Value v; // Null v. Set. Int(10); v = 10; // Shortcut, same as above Value b(true); // calls Value(bool) Value i(-123); // calls Value(int) Value u(123 u); // calls Value(unsigned) Value d(1. 5); // calls Value(double)

Rapid. Json - modifying DOM • 빈값 생성 Value v; //기본 생성자로 생성한 후에 v. Set. Object(); // 빈 Object로 변환함 v. Set. Array(); // 빈 Array로 변환함


Rapid. Json – create string • 문자열(string)을 만들때는 반드시 깊은 복사 Document document; Value author; char buffer[10]; int len = sprintf(buffer, "%s %s", "Milo", "Yip"); // 문자열 동적 생성 author. Set. String(buffer, len, document. Get. Allocator()); memset(buffer, 0, sizeof(buffer)); // author. Get. String()는 아직 버퍼가 파괴된 이후에도 // "Milo Yip"를 포함한다.

Rapid. Json – create string • 얕은복사? – 안됨. const char * cstr = getenv("USER"); size_t cstr_len =. . . ; // 여기서 길이를 측정한다. Value s; // s. Set. String(cstr); // 컴파일 되지 않는다. s. Set. String(String. Ref(cstr)); // ok, 널문자열이며 생명주기가 보장된다. s = String. Ref(cstr); // 위와 같다. s. Set. String(String. Ref(cstr, cstr_len)); // 빠르고, 널 문자열을 포함한다. s = String. Ref(cstr, cstr_len); // 위와 같다.

Rapid. Json – modifying Array • API 목록 • Clear() • Reserve(Size. Type, Allocator&) • Value& Push. Back(Value&, Allocator&) • template <typename T> Generic. Value& Push. Back(T, Allocator&) • Value& Pop. Back() • Value. Iterator Erase(Const. Value. Iterator pos) • Value. Iterator Erase(Const. Value. Iterator first, Const. Value. Iterator last)

Rapid. Json – modifying Array Value a(k. Array. Type); Document: : Allocator. Type& allocator = document. Get. Allocator(); for (int i = 5; i <= 10; i++) a. Push. Back(i, allocator); // allocator is needed for potential realloc(). a. Push. Back("Lua", allocator). Push. Back("Mio", allocator);

Rapid. Json – modify object Value contact(k. Object); contact. Add. Member("name", "Milo", document. Get. Allocator()); contact. Add. Member("married", true, document. Get. Allocator());

Rapid. Json – Deep Copy 깊은복사 Document d; Document: : Allocator. Type& a = d. Get. Allocator(); Value v 1("foo"); // Value v 2(v 1); // 허용하지 않는다 Value v 2(v 1, a); // 카피를 만든다. assert(v 1. Is. String()); // v 1은 문자열이었는데… d. Set. Array(). Push. Back(v 1, a). Push. Back(v 2, a); assert(v 1. Is. Null() && v 2. Is. Null()); // 둘다 d로 이동 v 2. Copy. From(d, a); // 전체 도큐먼트를 v 2로 카피 assert(d. Is. Array() && d. Size() == 2); // d는 손대지 않았다. v 1. Set. Object(). Add. Member("array", v 2, a); d. Push. Back(v 1, a);

Rapid. Json – swap value Value a(123); Value b("Hello"); a. Swap(b); assert(a. Is. String()); assert(b. Is. Int());

Rapid. Json • 감사합니다. • 참고자료 • https: //github. com/miloyip/rapidjson/ • http: //miloyip. github. io/rapidjson/md_doc_tutorial. html • http: //postgame. tistory. com/558
- Slides: 35