aboutsummaryrefslogtreecommitdiffstats
path: root/json.h
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2014-07-31 00:50:38 +0900
committeripknHama <ipknhama@gmail.com>2014-08-02 22:32:48 +0900
commit2e8f9f383d3ebc78fb2cb8ad37ad855ac28d11dd (patch)
tree43a9aa78cde60fe55fde5735f40dae7c34405df8 /json.h
parente68f5db64fc2e2f54184d3eef446520a2c2379f9 (diff)
downloadcrow-2e8f9f383d3ebc78fb2cb8ad37ad855ac28d11dd.tar.gz
crow-2e8f9f383d3ebc78fb2cb8ad37ad855ac28d11dd.zip
begin implementation: mustache based template engine
Diffstat (limited to 'json.h')
-rw-r--r--json.h60
1 files changed, 59 insertions, 1 deletions
diff --git a/json.h b/json.h
index 1e4a7c2..bb67d72 100644
--- a/json.h
+++ b/json.h
@@ -27,6 +27,11 @@
namespace crow
{
+ namespace mustache
+ {
+ class template_t;
+ }
+
namespace json
{
std::string escape(const std::string& str)
@@ -307,6 +312,11 @@ namespace crow
return it != end() && it->key_ == str;
}
+ int count(const std::string& str)
+ {
+ return has(str) ? 1 : 0;
+ }
+
rvalue* begin() const
{
#ifndef CROW_JSON_NO_ERROR_CHECK
@@ -935,8 +945,9 @@ namespace crow
class wvalue
{
+ friend class crow::mustache::template_t;
public:
- type t() { return t_; }
+ type t() const { return t_; }
private:
type t_{type::Null};
double d {};
@@ -947,6 +958,39 @@ namespace crow
wvalue() {}
+ wvalue(const rvalue& r)
+ {
+ t_ = r.t();
+ switch(r.t())
+ {
+ case type::Null:
+ case type::False:
+ case type::True:
+ return;
+ case type::Number:
+ d = r.d();
+ return;
+ case type::String:
+ s = r.s();
+ return;
+ case type::List:
+ l = std::move(std::unique_ptr<std::vector<wvalue>>(new std::vector<wvalue>{}));
+ l->reserve(r.size());
+ for(auto it = r.begin(); it != r.end(); ++it)
+ l->emplace_back(*it);
+ return;
+ case type::Object:
+ o = std::move(
+ std::unique_ptr<
+ std::unordered_map<std::string, wvalue>
+ >(
+ new std::unordered_map<std::string, wvalue>{}));
+ for(auto it = r.begin(); it != r.end(); ++it)
+ o->emplace(it->key(), *it);
+ return;
+ }
+ }
+
wvalue(wvalue&& r)
{
*this = std::move(r);
@@ -1093,6 +1137,20 @@ namespace crow
return (*l)[index];
}
+ int count(const std::string& str)
+ {
+ if (t_ != type::Object)
+ reset();
+ t_ = type::Object;
+ if (!o)
+ o = std::move(
+ std::unique_ptr<
+ std::unordered_map<std::string, wvalue>
+ >(
+ new std::unordered_map<std::string, wvalue>{}));
+ return o->count(str);
+ }
+
wvalue& operator[](const std::string& str)
{
if (t_ != type::Object)