DrawingProcess
๋“œํ”„ DrawingProcess
DrawingProcess
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
ยซ   2025/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (970)
    • Profile & Branding (25)
      • Career (18)
    • IT Trends (254)
      • Conference, Faire (Experien.. (31)
      • News (187)
      • Youtube (19)
      • TED (8)
      • Web Page (2)
      • IT: Etc... (6)
    • Contents (98)
      • Book (67)
      • Lecture (31)
    • Project Process (94)
      • Ideation (0)
      • Study Report (34)
      • Challenge & Award (22)
      • 1Day1Process (5)
      • Making (5)
      • KRC-FTC (Team TC(5031, 5048.. (10)
      • GCP (GlobalCitizenProject) (15)
    • Study: ComputerScience(CS) (72)
      • CS: Basic (9)
      • CS: Database(SQL) (5)
      • CS: Network (14)
      • CS: OperatingSystem (3)
      • CS: Linux (39)
      • CS: Etc... (2)
    • Study: Software(SW) (95)
      • SW: Language (29)
      • SW: Algorithms (1)
      • SW: DataStructure & DesignP.. (1)
      • SW: Opensource (15)
      • SW: Error Bug Fix (43)
      • SW: Etc... (6)
    • Study: Artificial Intellige.. (150) N
      • AI: Research (1)
      • AI: 2D Vision(Det, Seg, Tra.. (35)
      • AI: 3D Vision (71) N
      • AI: MultiModal (3)
      • AI: SLAM (0)
      • AI: Light Weight(LW) (3)
      • AI: Data Pipeline (7)
      • AI: Machine Learning(ML) (1)
    • Study: Robotics(Robot) (33)
      • Robot: ROS(Robot Operating .. (9)
      • Robot: Positioning (8)
      • Robot: Planning & Control (7)
    • Study: DeveloperTools(DevTo.. (83)
      • DevTool: Git (12)
      • DevTool: CMake (13)
      • DevTool: NoSQL(Elastic, Mon.. (25)
      • DevTool: Container (17)
      • DevTool: IDE (11)
      • DevTool: CloudComputing (4)
    • ์ธ์ƒ์„ ์‚ด๋ฉด์„œ (65)
      • ๋‚˜์˜ ์ทจ๋ฏธ๋“ค (7)
      • ๋‚˜์˜ ์ƒ๊ฐ๋“ค (42)
      • ์—ฌํ–‰์„ ๋– ๋‚˜์ž~ (10)
      • ๋ถ„๊ธฐ๋ณ„ ํšŒ๊ณ  (6)

๊ฐœ๋ฐœ์ž ๋ช…์–ธ

โ€œ ๋งค์ฃผ ๋ชฉ์š”์ผ๋งˆ๋‹ค ๋‹น์‹ ์ด ํ•ญ์ƒ ํ•˜๋˜๋Œ€๋กœ ์‹ ๋ฐœ๋ˆ์„ ๋ฌถ์œผ๋ฉด ์‹ ๋ฐœ์ด ํญ๋ฐœํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด๋ผ.
์ปดํ“จํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ด๋Ÿฐ ์ผ์ด ํ•ญ์ƒ ์ผ์–ด๋‚˜๋Š”๋ฐ๋„ ์•„๋ฌด๋„ ๋ถˆํ‰ํ•  ์ƒ๊ฐ์„ ์•ˆ ํ•œ๋‹ค. โ€

- Jef Raskin

๋งฅ์˜ ์•„๋ฒ„์ง€ - ์• ํ”Œ์ปดํ“จํ„ฐ์˜ ๋งคํ‚จํ† ์‹œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ฃผ๋„

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
DrawingProcess

๋“œํ”„ DrawingProcess

Study: Software(SW)/SW: Opensource

[OpenSource ์‚ฌ์šฉํ•˜๊ธฐ] nlohmann/json: c++ json library (feat. Modern C++)

2022. 7. 5. 09:13
๋ฐ˜์‘ํ˜•
๐Ÿ’ก๋ณธ ๋ฌธ์„œ๋Š” 'OpenSource ๋œฏ์–ด๋ณด๊ธฐ'๋ผ๋Š” ํ”„๋กœ์ ํŠธ์˜ ์ผํ™˜์œผ๋กœ.
nlohmann/json๋ผ๋Š” c++ json library๋ฅผ ์„ค์น˜, ์‹คํ–‰ ๋“ฑ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋”๋ถˆ์–ด, library ๋‚ด ๊ตฌ์กฐ, ์†Œ์Šค ๋ถ„์„ ๋ฐ ํŒจํ„ด ๋ถ„์„๊นŒ์ง€ ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๋‹ˆ ํ•ด๋‹น ์˜คํ”ˆ์†Œ์Šค์— ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ๋ด๋‘๊ธธ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

1. ์˜คํ”ˆ์†Œ์Šค์˜ ๋ชฉ์ 

1.1 'nlohmann/json'์— ๋Œ€ํ•˜์—ฌ.

  • C++ Json library
  • modern C++์˜ operation์„ ์ง€์›
  • nlohmann/json.hpp ํ•˜๋‚˜์˜ ํ—ค๋”ํŒŒ์ผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ
    •  That's it. No library, no subproject, no dependencies, no complex build system.
  • C++ data types: 
    • std::string for strings
    • int64_t, uint64_t or double for numbers
    • std::map for objects
    • std::vector for arrays
    • bool for Booleans.
      • you can template the generalized class basic_json to your needs.
  • Json library ์ค‘ ๋น ๋ฅธ ํŽธ์— ์†ํ•จ

1.2 'nlohmann/json' ์ถ”๊ฐ€ ์ •๋ณด

  • Git: https://github.com/nlohmann/json
  • Docs: https://json.nlohmann.me/ 
  • Lisence: MIT License: Copyright © 2013-2022 Niels Lohmann

2. ํ™˜๊ฒฝ ๊ตฌ์ถ•

2.1 ํ™˜๊ฒฝ ๊ตฌ์ถ•: CMake (FetchContent)

include(FetchContent)

FetchContent_Declare(
    json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.10.5
)
FetchContent_MakeAvailable(json)

target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)

2.2 ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ (Generate Json)

2.2.1 Json ์ƒ์„ฑ(like jsoncpp)

// create an empty structure (null)
json j;

// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;

// add another null object by passing nullptr
j["nothing"] = nullptr;

// add an object inside the object
j["answer"]["everything"] = 42;

// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };

// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };

2.2.2 JsonํŒŒ์ผ๊ณผ ์œ ์‚ฌ

// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
  {"pi", 3.141},
  {"nothing", nullptr},
  {"answer", {
    {"everything", 42}
  }},
  {"list", {1, 0, 2}},
  {"object", {
    {"currency", "USD"},
    {"value", 42.99}
  }}
};

2.2.3 JsonํŒŒ์ผ๊ณผ ๋™์ผ

You can create a JSON value (deserialization) by appending _json to a string literal:

// create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;

// or even nicer with a raw string literal
auto j2 = R"(
  {
    "happy": true,
    "pi": 3.141
  }
)"_json;

2.3 ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ (Parsing Json)

2.3.1 Json Parsing

// parse explicitly
auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");

2.3.2 Json To String

// explicit conversion to string
std::string s = j.dump();    // {"happy":true,"pi":3.141}

// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
//     "happy": true,
//     "pi": 3.141
// }

2.3.3 Retrieve Json value

// retrieve the string value
auto cpp_string = j_value.get<std::string>();
// retrieve the string value (alternative when a variable already exists)
std::string cpp_string2;
j_value.get_to(cpp_string2);

// retrieve another value
auto cpp_int = j_value["int value"].get<int>();
auto cpp_int64 = j_value["int64 value"].get<int64_t>();
auto cpp_double = j_value["double value"].get<double>();
auto cpp_object = j_value["object value"].get<std::map>();
auto cpp_array = j_value["array value"].get<std::vector>();

// use function at() to access the object values rather than operator[]. 
// In case a key does not exist, at throws an exception that you can handle, 
// whereas operator[] exhibits undefined behavior.
auto cpp_uint64 = j_value.at("uint64 value").get<uint64_t>();

// Good data handling: conditional number conversion
if (cpp_int.is_number_integer()) { auto cpp_int64 = j_value["int64 value"].get<int64_t>(); }
number type default type possible values
signed integers std::int64_t std::int32_t, std::int16_t, etc.
unsigned integers std::uint64_t std::uint32_t, std::uint16_t, etc.
floating-point double float, long double
  • The type for signed integers must be convertible from long long. The type for floating-point numbers is used in case of overflow.
  • The type for unsigned integers must be convertible from unsigned long long. The type for floating-point numbers is used in case of overflow.

+ JsonCpp get() method (in nlohmann::json)

// JsonCpp
auto two = j_object.value("two", 2).get();

// i) nlohmann::json value (Implemented by try, catch)
auto two = j_object.value("two", 2).get<int32_t>();

// ii) nlohmann::json try, catch
try {
    auto two = j_object.at("two").get<int32_t>();
} catch(){
    auto two = 2
}

// iii) nlohmann::json find
if (j_object.find("two") == j_object.end()) {
	j_object["two"] = 2;
}
auto two = j_object["two"].get<int32_t>();

๋‹จ, nlohmannjson.value("key", "default value")๋ฅผ ์“ธ ๋•Œ ์ฃผ์˜ํ•ด์•ผํ•  ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • json struct์— ์•„๋ฌด ๊ฐ’์ด ์—†์œผ๋ฉด ํŠน์ • key ๊ฐ’์„ ๋ฝ‘์œผ๋ ค๋Š” ์‹œ๋„๋งŒ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
    • [Error Message]: C++ exception with description "[json.exception.type_error.306] cannot use value() with null" thrown in the test body.

2.4 ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ (Arbitrary types conversions)

2.4.1 Using to_json, from_json: struct to json convertor

When calling the json constructor with your type, your custom to_json method will be automatically called.

Likewise, when calling get<your_type>() or get_to(your_type&), the from_json method will be called.

using nlohmann::json;

namespace ns {
    void to_json(json& j, const person& p) {
        j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
    }

    void from_json(const json& j, person& p) {
        j.at("name").get_to(p.name);
        j.at("address").get_to(p.address);
        j.at("age").get_to(p.age);
    }
} // namespace ns


// create a person
ns::person p {"Ned Flanders", "744 Evergreen Terrace", 60};

// conversion: person -> json
json j = p;

std::cout << j << std::endl;
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}

// conversion: json -> person
auto p2 = j.get<ns::person>();

// that's it
assert(p == p2);

2.4.2 Macros: Simplyfy to_json, from_json

namespace ns {
    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, address, age)
}

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(name, member1, member2, ...) is to be defined inside the namespace of the class/struct to create code for.

namespace ns {
    class address {
      private:
        std::string street;
        int housenumber;
        int postcode;

      public:
        NLOHMANN_DEFINE_TYPE_INTRUSIVE(address, street, housenumber, postcode)
    };
}

NLOHMANN_DEFINE_TYPE_INTRUSIVE(name, member1, member2, ...) is to be defined inside the class/struct to create code for. This macro can also access private members.

2.4.3 Convert third-party types: char*, char[], union, ...

template <typename T>
struct adl_serializer {
    static void to_json(json& j, const T& value) {
        // calls the "to_json" method in T's namespace
    }

    static void from_json(const json& j, T& value) {
        // same thing, but with the "from_json" method
    }
};

์ฐธ๊ณ : https://github.com/nlohmann/json#how-do-i-convert-third-party-types

+ Specializing enum conversion

// example enum type declaration
enum TaskState {
    TS_STOPPED,
    TS_RUNNING,
    TS_INVALID=-1,
};

// map TaskState values to JSON as strings
NLOHMANN_JSON_SERIALIZE_ENUM( TaskState, {
    {TS_INVALID, nullptr},
    {TS_STOPPED, "stopped"},
    {TS_RUNNING, "running"}
})

usage:

// enum to JSON as string
json j = TS_STOPPED;
assert(j == "stopped");

// json string to enum
json j3 = "running";
assert(j3.get<TaskState>() == TS_RUNNING);

// undefined json value to enum (where the first map entry above is the default)
json jPi = 3.14;
assert(jPi.get<TaskState>() == TS_INVALID );

+ json visualization

jsonvisio: https://jsonvisio.com/editor

์ฐธ๊ณ 

  • [Github] nlohmann/json: https://github.com/nlohmann/json
  • [Official Docs] JSON for Modern C++: https://json.nlohmann.me/
  • [Official Docs] nlohmann/json value example: https://json.nlohmann.me/api/basic_json/value/#examples
  • [Official Docs] Number Handling: https://json.nlohmann.me/features/types/number_handling/#number-comparison
  • [nlohmann] nlohmann/json community: https://gitter.im/nlohmann/json?at=5c8388748f294b134a127674
  • [stackoverflow] How do i read/write JSON with c++?: https://stackoverflow.com/questions/70684671/how-do-i-read-write-json-with-c
  • [blog] tutorial to use nlohmann json for serializing data with modern cpp: https://kezunlin.me/post/f3c3eb8/
  • [blog] Nlohmann Json ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒ˜ํ”Œ
  • https://json.nlohmann.me/integration/cmake/
  • [Git issue] Change JsonCpp to nlohmann/json: https://github.com/drogonframework/drogon/issues/249
  • [KeZunLin] to_json, from_json: https://kezunlin.me/post/f3c3eb8/ 

 

 

๋ฐ˜์‘ํ˜•
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'Study: Software(SW) > SW: Opensource' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[C++] C++ Json ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ณ€๊ฒฝ: JsonCpp to Nlohmann/json...  (0) 2022.08.10
[OpenSource ์‚ฌ์šฉํ•˜๊ธฐ] googleMock(gMock): C++ Mocking Library for googletest  (0) 2022.07.16
[OpenSource ์‚ฌ์šฉํ•˜๊ธฐ/๋œฏ์–ด๋ณด๊ธฐ] OpenSource ์‚ฌ์šฉํ•˜๊ธฐ/๋œฏ์–ด๋ณด๊ธฐ ๊ฐœ์š” ๋ฐ ๋ชฉ๋ก  (0) 2022.06.29
[Opensource ๋œฏ์–ด๋ณด๊ธฐ] Opensource๋ฅผ ๋ถ„์„ํ•˜๋Š” ์ด์œ ?  (0) 2022.06.29
[Opensource] ์˜คํ”ˆ์†Œ์Šค ๋ฐฐํฌ์‹œ ์†Œ์Šค์ฝ”๋“œ ์ œ๊ณต์˜๋ฌด์™€ ๋ฒ”์œ„  (0) 2022.06.29
    'Study: Software(SW)/SW: Opensource' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [C++] C++ Json ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ณ€๊ฒฝ: JsonCpp to Nlohmann/json...
    • [OpenSource ์‚ฌ์šฉํ•˜๊ธฐ] googleMock(gMock): C++ Mocking Library for googletest
    • [OpenSource ์‚ฌ์šฉํ•˜๊ธฐ/๋œฏ์–ด๋ณด๊ธฐ] OpenSource ์‚ฌ์šฉํ•˜๊ธฐ/๋œฏ์–ด๋ณด๊ธฐ ๊ฐœ์š” ๋ฐ ๋ชฉ๋ก
    • [Opensource ๋œฏ์–ด๋ณด๊ธฐ] Opensource๋ฅผ ๋ถ„์„ํ•˜๋Š” ์ด์œ ?
    DrawingProcess
    DrawingProcess
    ๊ณผ์ •์„ ๊ทธ๋ฆฌ์ž!

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”