C++ の json11 を使う (Create)

```cpp:json_create.cpp
// --------------------------------------------------------------------
/*
	json_create.cpp

				May/28/2018
*/
// --------------------------------------------------------------------
#include	<iostream>
#include	<fstream>
#include	<string>
#include	<map>
#include	<cstring>
#include	<boost/lexical_cast.hpp>

#include "json11.hpp"

using namespace std;
using namespace json11;

typedef map<string,string> Unit;
// --------------------------------------------------------------------
map <string,Unit> dict_append_proc
	(map <string,Unit> dict_aa,string id,string name,
	int population,string date_mod)
{
	Unit unit_aa;

	unit_aa["name"] = name;
	unit_aa["population"] = boost::lexical_cast<string>(population);

	unit_aa["date_mod"] = date_mod;

	string key = boost::lexical_cast<string>(id);

	dict_aa[key] = unit_aa;

	return dict_aa;
}

// -----------------------------------------------------------------------
// [8]:
string dict_to_json_proc (map <string,Unit> dict_aa)
{
	int nnx = (unsigned int)dict_aa.size();

	Json::object my_json;

	map <string,Unit>:: iterator it = dict_aa.begin ();

	while (it != dict_aa.end ())
		{
		string key = (*it).first;
		Unit unit_aa = (*it).second;

		int population = atoi (unit_aa["population"].c_str ());

		my_json[key] = Json::object {{ "name", unit_aa["name"]},{ "population", population},{ "date_mod", unit_aa["date_mod"] }};

		it++;
		}

	Json data = my_json;

	string json_str = data.dump();

	return	json_str;
}

// --------------------------------------------------------------------
static map <string,Unit >  data_prepare_proc ()
{
	map <string,Unit> dict_aa;

	dict_aa = dict_append_proc (dict_aa,"t0921","宇都宮",76381,"1922-10-8");

	dict_aa = dict_append_proc (dict_aa,"t0922","小山",91237,"1922-8-12");
	dict_aa = dict_append_proc (dict_aa,"t0923","佐野",23158,"1922-3-28");
	dict_aa = dict_append_proc (dict_aa,"t0924","足利",51486,"1922-9-21");

	dict_aa = dict_append_proc (dict_aa,"t0925","日光",83971,"1922-1-5");
	dict_aa = dict_append_proc (dict_aa,"t0926","下野",74968,"1922-5-22");
	dict_aa = dict_append_proc (dict_aa,"t0927","さくら",46185,"1922-7-17");
	dict_aa = dict_append_proc (dict_aa,"t0928","矢板",59614,"1922-3-04");
	dict_aa = dict_append_proc (dict_aa,"t0929","真岡",71823,"1922-5-9");
	dict_aa = dict_append_proc (dict_aa,"t0930","栃木",82361,"1922-8-12");
	dict_aa = dict_append_proc (dict_aa,"t0931","大田原",36128,"1922-5-25");
	dict_aa = dict_append_proc (dict_aa,"t0932","鹿沼",19254,"1922-4-8");
	dict_aa = dict_append_proc (dict_aa,"t0933","那須塩原",35682,"1922-3-17");
	dict_aa = dict_append_proc (dict_aa,"t0934","那須烏山",24135,"1922-2-21");

	return	dict_aa;
}
// --------------------------------------------------------------------
int main (int argc,char *argv[])
{
	cerr << "*** 開始 ***\n";

	char file_out[160];

	strcpy (file_out,argv[1]);

	cerr << "\tfile_out = " << file_out << "\n";

	map <string,Unit> dict_aa = data_prepare_proc ();

	cout << "要素数：" << (unsigned int)dict_aa.size() << endl;

	string str_json = dict_to_json_proc (dict_aa);

	ofstream fp_out (file_out);
	fp_out << str_json;
	fp_out.close ();

	cerr << "*** 終了 ***\n";
}

// --------------------------------------------------------------------
```

```text:Makefile
json_create.exe: json_create.cpp
	clang++ -o json_create json_create.cpp -ljson11
clean:
	rm -f json_create
```

実行コマンド

```bash
./json_create cities.json
```