| 4 | #include "../choc/containers/choc_Value.h" |
| 5 | |
| 6 | int main() |
| 7 | { |
| 8 | // Create a choc::value::Value object |
| 9 | auto address = choc::json::create ("street", "123 Main St", |
| 10 | "city", "Anytown"); |
| 11 | |
| 12 | auto person = choc::json::create ("name", "John Doe", |
| 13 | "age", 30, |
| 14 | "isStudent", false, |
| 15 | "address", address); |
| 16 | |
| 17 | // Convert the choc::value::Value object to a JSON string |
| 18 | auto jsonString = choc::json::toString (person); |
| 19 | std::cout << "Generated JSON:\n" << jsonString << std::endl; |
| 20 | |
| 21 | // Parse the JSON string back into a choc::value::Value object |
| 22 | auto parsedValueHolder = choc::json::parse (jsonString); |
| 23 | auto parsedValue = parsedValueHolder.getView(); |
| 24 | |
| 25 | // Access the data from the parsed object |
| 26 | std::cout << "\nParsed JSON data:\n"; |
| 27 | std::cout << "Name: " << parsedValue["name"].get<std::string>() << std::endl; |
| 28 | std::cout << "Age: " << parsedValue["age"].get<int>() << std::endl; |
| 29 | std::cout << "Is Student: " << (parsedValue["isStudent"].get<bool>() ? "true" : "false") << std::endl; |
| 30 | std::cout << "Address: " << parsedValue["address"]["street"].get<std::string>() << ", " << parsedValue["address"]["city"].get<std::string>() << std::endl; |
| 31 | |
| 32 | return 0; |
| 33 | } |