Declrations
template <
class CharT,
class Traits = std::char_traits<CharT>
>
class json_separator;
template <
class CharT,
class Traits = std::char_traits<CharT>
>
class basic_json_array;
typedef basic_tokenmap<json_separator<char>, std::string, std::string> json
typedef basic_json_array<char> json_array;
#ifdef CXL_USE_WCHAR
typedef basic_tokenmap<json_separator<wchar_t>, std::wstring, std::wstring> wjson
typedef basic_json_array<wchar_t> wjson_array;
#endif
Overview
json,および json_array は,JSON 形式データの解析を行うためのクラス群です. json は,JSON 形式データの object ("{" で始まり,"}" で終わる文字列)を解析し,json_array は array ("[" で始まり,"]" で終わる文字列)を解析します.JSON 形式データの書式の詳細については,JSON の紹介を参照して下さい.
json は,object の中に array や object が存在する場合,その部分の再帰的な解析は行わず, 文字列のまま返します.該当箇所の解析が必要な場合は,返された文字列を引数にして再度 json,または json_array を用いて解析を行って下さい.json_array は,array に含まれる文字列を object とそれ以外に分けて解析します. 解析結果は,それぞれ objects(),strings() メソッドで取得することができます.
json,および json_array は,string,number,true,false,null を全て文字列として格納し,その結果を返します (e.g., true->"true", false->"false", null->"null").
Example
// ワイド文字関連のCLXライブラリを使用するために必要
#ifndef CLX_USE_WCHAR
#define CLX_USE_WCHAR
#endif
#include <iostream>
#include <string>
#include "clx/http.h"
#include "clx/uri.h"
#include "clx/code_convert.h"
#include "clx/json.h"
int main(int argc, char* argv[]) {
if (argc < 3) std::exit(-1);
clx::http session(clx::uri::encode(argv[1]));
if (!session.get(clx::uri::encode(argv[2]))) {
std::cerr << "failed to get response" << std::endl;
std::exit(-1);
}
/*
* parse a hatena bookmark entry.
* はてなのJSONは()で括られているので,その部分は除外.
*/
std::wstring body = clx::code_convert<wchar_t>(session.body());
clx::wjson hateb(body.begin() + 1, body.end() - 1);
std::wcout.imbue(std::locale("japanese"));
for (clx::wjson::iterator pos = hateb.begin(); pos != hateb.end(); pos++) {
std::wcout << pos->first << L": " << pos->second << std::endl;
}
std::wcout << std::endl;
if (hateb.find(L"bookmarks") == hateb.end()) return 0;
// parse and print the detail of "bookmarks"
clx::wjson_array bk(hateb[L"bookmarks"]);
std::wcout << L"bookmarks have " << bk.objects().size() << L" object, "
<< bk.strings().size() << L" string" << std::endl;
std::wcout << L"-----" << std::endl;
for (size_t i = 0; i < bk.objects().size(); i++) {
std::wcout << L"object " << i << L":" << std::endl;
for (clx::wjson::iterator pos = bk.object(i).begin();
pos != bk.object(i).end(); pos++) {
std::wcout << L"\t" << pos->first << L": " << pos->second << std::endl;
}
}
return 0;
}
Result
$ ./test b.hatena.ne.jp "/entry/json/?url=http://d.hatena.ne.jp/tt_clown/20080823/p1"
bookmarks: [{"comment":" ","timestamp":"2008/08/25 16:01:00","user":"HISAMATSU",
"tags":["\u306f\u3066\u306a","ruby"]},{"comment":"\u306f\u3066\u306a\u30d6\u30c3
\u30af\u30de\u30fc\u30af\u3000\u985e\u4f3c","timestamp":"2008/08/24 21:11:03",
"user":"poafag","tags":["ruby","\u306f\u3066\u306a"]}]
count: 2
eid: 9756340
entry_url: http://b.hatena.ne.jp/entry/http://d.hatena.ne.jp/tt_clown/20080823/p1
related: []
screenshot: http://screenshot.hatena.ne.jp/images/120x90/0/f/f/9/5
/d9701e8c6a5392d9a3a729155d07315e310.jpg
title: github::clown::ruby-hatena - Life like a clown
url: http://d.hatena.ne.jp/tt_clown/20080823/p1
bookmarks have 2 object, 0 string
-----
object 0:
comment:
tags: ["\u306f\u3066\u306a","ruby"]
timestamp: 2008/08/25 16:01:00
user: HISAMATSU
object 1:
comment: はてなブックマーク 類似
tags: ["ruby","\u306f\u3066\u306a"]
timestamp: 2008/08/24 21:11:03
user: poafag
Template Parameters
json
tokenmap を参照して下さい.
json_array
- CharT
- 文字の型を指定します.
- Traits
- 文字列を扱うためのtraitsを指定します.デフォルト値は, std::char_traits<CharT>.
Related Types
json
tokenmap を参照して下さい.
json_array
typedef CharT char_type;
typedef std::basic_string<CharT, Traits> string_type;
typedef basic_tokenmap<json_separator<CharT, Traits>, string_type,
string_type> object_type;
typedef size_t size_type;
Construction and Member Functions
json
tokenmap を参照して下さい.
json_array
basic_json_array(); template <class InputIterator> explicit basic_json_array(InputIterator first, InputIterator last); explicit basic_json_array(const string_type& src); explicit basic_json_array(const char_type* src); virtual ~basic_json_array(); void reset(); template <class InputIterator> basic_json_array& assign(InputIterator first, InputIterator last); basic_json_array& assign(const string_type& src); basic_json_array& assign(const char_type* src);
入力イテレータ,または文字列を指定して解析を行います.解析結果は,object とそれ以外に分かれて内部変数で保持されます.
std::vector<object_type>& objects(); const std::vector<object_type> objects() const; object_type& object(size_type index); const object_type& object(size_type index) const; std::vector<string_type>& strings(); const std::vector<string_type>& strings() const; string_type& string(size_type index); const string_type& string(size_type index) const;
objects(),strings() メソッドは,解析結果を格納した配列全体を返します.これに対して, object(),string() メソッドは,引数に指定された添え字に対応する object, および文字列を返します.