Skip to content

Commit 2bf2993

Browse files
committed
Example update
1 parent 5130e4d commit 2bf2993

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

doc/Examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ int main()
11751175
{
11761176
"rater": "HikingAsylum.example.com",
11771177
"assertion": "is-good",
1178-
"rated": "sk",
1178+
"rated": "Marilyn C",
11791179
"rating": 0.90
11801180
}
11811181
]

doc/ref/cbor/cbor.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ Output:
147147
{
148148
"rater": "HikingAsylum.example.com",
149149
"assertion": "is-good",
150-
"rated": "sk",
150+
"rated": "Marilyn C",
151151
"rating": 0.9
152152
}
153153
]
154154
}
155155
156156
(2)
157-
sk, 0.9
157+
Marilyn C, 0.9
158158
159-
(3) sk
159+
(3) Marilyn C
160160
```
161161

doc/ref/csv/csv.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Output:
112112
}
113113
```
114114

115-
#### Decode a CSV source to a C++ object that satisfies [json_type_traits](../json_type_traits.md) requirements
115+
#### Convert a CSV source to a C++ object that satisfies [json_type_traits](../json_type_traits.md) requirements, and back
116116

117117
```c++
118118
#include <jsoncons/json.hpp>
@@ -124,20 +124,21 @@ namespace csv = jsoncons::csv;
124124

125125
int main()
126126
{
127-
const std::string s = R"(Date,1Y,2Y,3Y,5Y
127+
const std::string input = R"(Date,1Y,2Y,3Y,5Y
128128
2017-01-09,0.0062,0.0075,0.0083,0.011
129129
2017-01-08,0.0063,0.0076,0.0084,0.0112
130130
2017-01-08,0.0063,0.0076,0.0084,0.0112
131131
)";
132132

133-
csv::csv_options options;
134-
options.header_lines(1)
133+
csv::csv_options ioptions;
134+
ioptions.header_lines(1)
135135
.mapping(csv::mapping_type::n_rows);
136136

137137
typedef std::vector<std::tuple<std::string,double,double,double,double>> table_type;
138138

139-
table_type table = csv::decode_csv<table_type>(s,options);
139+
table_type table = csv::decode_csv<table_type>(input,ioptions);
140140

141+
std::cout << "(1)\n";
141142
for (const auto& row : table)
142143
{
143144
std::cout << std::get<0>(row) << ","
@@ -146,12 +147,29 @@ int main()
146147
<< std::get<3>(row) << ","
147148
<< std::get<4>(row) << "\n";
148149
}
150+
std::cout << "\n";
151+
152+
std::string output;
153+
154+
csv::csv_options ooptions;
155+
ooptions.column_names("Date,1Y,2Y,3Y,5Y");
156+
csv::encode_csv<table_type>(table, output, ooptions);
157+
158+
std::cout << "(2)\n";
159+
std::cout << output << "\n";
149160
}
150161
```
151162
Output:
152163
```
164+
(1)
153165
2017-01-09,0.0062,0.0075,0.0083,0.011
154166
2017-01-08,0.0063,0.0076,0.0084,0.011
155167
2017-01-08,0.0063,0.0076,0.0084,0.011
168+
169+
(2)
170+
Date,1Y,2Y,3Y,5Y
171+
2017-01-09,0.0062,0.0075,0.0083,0.011
172+
2017-01-08,0.0063,0.0076,0.0084,0.0112
173+
2017-01-08,0.0063,0.0076,0.0084,0.0112
156174
```
157175

examples/src/csv_examples.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,21 @@ void csv_source_to_json_value()
3838

3939
void csv_source_to_cpp_object()
4040
{
41-
const std::string s = R"(Date,1Y,2Y,3Y,5Y
41+
const std::string input = R"(Date,1Y,2Y,3Y,5Y
4242
2017-01-09,0.0062,0.0075,0.0083,0.011
4343
2017-01-08,0.0063,0.0076,0.0084,0.0112
4444
2017-01-08,0.0063,0.0076,0.0084,0.0112
4545
)";
4646

47-
csv::csv_options options;
48-
options.header_lines(1)
47+
csv::csv_options ioptions;
48+
ioptions.header_lines(1)
4949
.mapping(csv::mapping_type::n_rows);
5050

5151
typedef std::vector<std::tuple<std::string,double,double,double,double>> table_type;
5252

53-
table_type table = csv::decode_csv<table_type>(s,options);
53+
table_type table = csv::decode_csv<table_type>(input,ioptions);
5454

55+
std::cout << "(1)\n";
5556
for (const auto& row : table)
5657
{
5758
std::cout << std::get<0>(row) << ","
@@ -60,6 +61,16 @@ void csv_source_to_cpp_object()
6061
<< std::get<3>(row) << ","
6162
<< std::get<4>(row) << "\n";
6263
}
64+
std::cout << "\n";
65+
66+
std::string output;
67+
68+
csv::csv_options ooptions;
69+
ooptions.column_names("Date,1Y,2Y,3Y,5Y");
70+
csv::encode_csv<table_type>(table, output, ooptions);
71+
72+
std::cout << "(2)\n";
73+
std::cout << output << "\n";
6374
}
6475

6576
void csv_decode_without_type_inference()
@@ -392,8 +403,8 @@ void csv_examples()
392403
csv_parser_type_inference();
393404

394405
decode_csv_with_subfields();
395-
csv_source_to_cpp_object();
396406
csv_source_to_json_value();
407+
csv_source_to_cpp_object();
397408
std::cout << std::endl;
398409
}
399410

0 commit comments

Comments
 (0)