diff --git a/doc/Examples.md b/doc/Examples.md index e9a451e83..546538db5 100644 --- a/doc/Examples.md +++ b/doc/Examples.md @@ -1,4 +1,4 @@ -# Examples +# Examples ### Parse and decode @@ -17,6 +17,7 @@ ### Encode [Encode a json value to a string](#B1) +[Encode Chinese characters](#B5) [Encode a json value to a stream](#B2) [Escape all non-ascii characters](#B3) [Replace the representation of NaN, Inf and -Inf when serializing. And when reading in again.](#B4) @@ -509,6 +510,21 @@ j.dump(s); // compressed j.dump(s, indenting::indent); // pretty print ``` +
+ +#### Encode Chinese characters + +``` +jsoncons::json j; + +std::string s = (const char*)u8"你好"; +j.try_emplace("hello", s); +assert(j["hello"].as() == s); + +std::string json_string; +j.dump(json_string); +``` +
#### Encode a json value to a stream diff --git a/examples/src/serialization_examples.cpp b/examples/src/serialization_examples.cpp index 50e42ebb4..3719ba950 100644 --- a/examples/src/serialization_examples.cpp +++ b/examples/src/serialization_examples.cpp @@ -1,9 +1,10 @@ -// Copyright 2013-2023 Daniel Parker +// Copyright 2013-2023 Daniel Parker // Distributed under Boost license #include #include #include +#include using namespace jsoncons; @@ -443,6 +444,18 @@ void decimal_precision_examples() std::cout << "(4) a: " << j2["a"].as() << ", b: " << j2["b"].as() << "\n\n"; } +void chinese_characters() +{ + jsoncons::json j; + + std::string s = (const char*)u8"你好"; + j.try_emplace("hello", s); + assert(j["hello"].as() == s); + + std::string json_string; + j.dump(json_string); +} + int main() { std::cout << "\nSerialization examples\n\n"; @@ -455,7 +468,8 @@ int main() bignum_serialization_examples2(); bignum_serialization_examples1(); decimal_precision_examples(); - bignum_access_examples(); + bignum_access_examples(); + chinese_characters(); std::cout << std::endl; }