Skip to content

Commit

Permalink
Added example for encoding chinese_characters
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Oct 12, 2023
1 parent 62e3858 commit 07c1e93
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
18 changes: 17 additions & 1 deletion doc/Examples.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Examples
# Examples

### Parse and decode

Expand All @@ -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)
Expand Down Expand Up @@ -509,6 +510,21 @@ j.dump(s); // compressed
j.dump(s, indenting::indent); // pretty print
```

<div id="B5"/>

#### Encode Chinese characters

```
jsoncons::json j;
std::string s = (const char*)u8"你好";
j.try_emplace("hello", s);
assert(j["hello"].as<std::string>() == s);
std::string json_string;
j.dump(json_string);
```

<div id="B2"/>

#### Encode a json value to a stream
Expand Down
18 changes: 16 additions & 2 deletions examples/src/serialization_examples.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2013-2023 Daniel Parker
// Copyright 2013-2023 Daniel Parker
// Distributed under Boost license

#include <string>
#include <jsoncons/json.hpp>
#include <iomanip>
#include <assert.h>

using namespace jsoncons;

Expand Down Expand Up @@ -443,6 +444,18 @@ void decimal_precision_examples()
std::cout << "(4) a: " << j2["a"].as<double>() << ", b: " << j2["b"].as<double>() << "\n\n";
}

void chinese_characters()
{
jsoncons::json j;

std::string s = (const char*)u8"你好";
j.try_emplace("hello", s);
assert(j["hello"].as<std::string>() == s);

std::string json_string;
j.dump(json_string);
}

int main()
{
std::cout << "\nSerialization examples\n\n";
Expand All @@ -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;
}

0 comments on commit 07c1e93

Please sign in to comment.