Add alarm module 84/56884/14
authorSeungkeun Lee <sngn.lee@samsung.com>
Wed, 13 Jan 2016 09:46:41 +0000 (18:46 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Tue, 2 Feb 2016 01:48:29 +0000 (10:48 +0900)
Change-Id: I00973659b182529e8456a55f59c260e78bc66e84

modules/CMakeLists.txt
modules/tizen-alarm/CMakeLists.txt [new file with mode: 0755]
modules/tizen-alarm/alarm_extension.cc [new file with mode: 0755]
modules/tizen-alarm/alarm_extension.h [new file with mode: 0755]
modules/tizen-alarm/package.json [new file with mode: 0755]
modules/tizen-alarm/picojson.h [new file with mode: 0755]
modules/tizen-alarm/tizen-alarm_api.js [new file with mode: 0755]

index 618ce40..1db4c13 100755 (executable)
@@ -13,3 +13,4 @@ ADD_SUBDIRECTORY(tizen-notification)
 ADD_SUBDIRECTORY(tizen-message-port)
 ADD_SUBDIRECTORY(tizen-device)
 ADD_SUBDIRECTORY(tizen-system-setting)
+ADD_SUBDIRECTORY(tizen-alarm)
diff --git a/modules/tizen-alarm/CMakeLists.txt b/modules/tizen-alarm/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..942027f
--- /dev/null
@@ -0,0 +1,20 @@
+SET(MODULE "tizen-alarm")
+
+# Native Module
+ADD_MODULE(${MODULE} XWALK
+  JSAPI
+    tizen-alarm_api.js
+  SRCS
+    alarm_extension.cc
+  DEPENDS
+    dlog capi-appfw-application bundle glib-2.0
+)
+
+# Copy Project
+INSTALL(FILES package.json
+  DESTINATION ${GLOBAL_NODE_MODULE_PATH}/${MODULE}
+)
+
+INSTALL(TARGETS ${MODULE}
+  DESTINATION ${GLOBAL_NODE_MODULE_PATH}/${MODULE}
+)
diff --git a/modules/tizen-alarm/alarm_extension.cc b/modules/tizen-alarm/alarm_extension.cc
new file mode 100755 (executable)
index 0000000..0aa6d3b
--- /dev/null
@@ -0,0 +1,357 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include "alarm_extension.h"
+
+#include <dlog.h>
+#include <app_alarm.h>
+#include <bundle_internal.h>
+#include <app_control.h>
+#include <app_control_internal.h>
+#include <glib.h>
+
+#include <vector>
+#include <memory>
+#include <thread> // NOLINT
+
+#include "picojson.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "JSNative"
+
+namespace appfw {
+
+namespace {
+
+const int kFlag_map[] = {
+  ALARM_WEEK_FLAG_SUNDAY,
+  ALARM_WEEK_FLAG_MONDAY,
+  ALARM_WEEK_FLAG_TUESDAY,
+  ALARM_WEEK_FLAG_WEDNESDAY,
+  ALARM_WEEK_FLAG_THURSDAY,
+  ALARM_WEEK_FLAG_FRIDAY,
+  ALARM_WEEK_FLAG_SATURDAY
+};
+
+app_control_h MakeAppControlFromJSON(const std::string& json) {
+  bundle* b = nullptr;
+  if (bundle_from_json(json.c_str(), &b) != 0) {
+    return nullptr;
+  }
+  app_control_h appcontrol = nullptr;
+  app_control_create(&appcontrol);
+  app_control_import_from_bundle(appcontrol, b);
+  bundle_free(b);
+  return appcontrol;
+}
+
+void ErrorHandle(int ret, picojson::object* data) {
+  picojson::object& result = *data;
+  if (ret == ALARM_ERROR_PERMISSION_DENIED) {
+    result["result"] = picojson::value("FAIL");
+    result["reason"] = picojson::value("Permission denied");
+  } else if (ret == ALARM_ERROR_NOT_PERMITTED_APP) {
+    result["result"] = picojson::value("FAIL");
+    result["reason"] =
+        picojson::value("Target Application is not permitted "
+                        "UI application is only permitted.");
+  } else if (ret == ALARM_ERROR_INVALID_TIME) {
+    result["result"] = picojson::value("FAIL");
+    result["reason"] = picojson::value("Triggered time is invalid");
+  } else {
+    result["result"] = picojson::value("FAIL");
+    result["reason"] = picojson::value("Fail");
+  }
+}
+
+}  // namespace
+
+
+xwalk::XWalkExtensionInstance* AlarmExtension::CreateInstance() {
+  return new AlarmInstance();
+}
+
+void AlarmInstance::Initialize() {
+  LOGD("Created tizen-alarm instance");
+}
+
+void AlarmInstance::HandleMessage(const char* msg) {
+  picojson::value value;
+  std::string err;
+  picojson::parse(value, msg, msg + strlen(msg), &err);
+  if (!err.empty()) {
+    LOGE("Ignoring message. Can't parse msessage : %s", err.c_str());
+    return;
+  }
+  if (!value.is<picojson::object>()) {
+    LOGE("Ignoring message. It is not an object.");
+    return;
+  }
+
+  auto& request = value.get<picojson::object>();
+  auto found = request.find("asyncid");
+  if (found == request.end()) {
+    LOGE("asyncid was not existed");
+    return;
+  }
+
+  auto cmd = request["cmd"].to_str();
+
+  if (cmd == "getAlarms") {
+    HandleGetAlarms(request["asyncid"].to_str());
+  }
+}
+
+void AlarmInstance::HandleSyncMessage(const char* msg) {
+  // parse json object
+  picojson::value value;
+  std::string err;
+  picojson::parse(value, msg, msg + strlen(msg), &err);
+  if (!err.empty()) {
+    LOGE("Ignoring message. Can't parse msessage : %s", err.c_str());
+    return;
+  }
+  if (!value.is<picojson::object>()) {
+    LOGE("Ignoring message. It is not an object.");
+    return;
+  }
+
+  auto& request = value.get<picojson::object>();
+
+  auto cmd = request["cmd"].to_str();
+  if (cmd == "scheduleAfter") {
+    int after = static_cast<int>(request["after"].get<double>());
+    std::string appcontrol_json = request["appcontrol"].to_str();
+    std::string target = request["target"].to_str();
+    HandleAfterSchduleAfter(target, appcontrol_json, after);
+  } else if (cmd == "scheduleAtDate") {
+    int64_t date = static_cast<int64_t>(request["date"].get<double>());
+    std::string appcontrol_json = request["appcontrol"].to_str();
+    std::string target = request["target"].to_str();
+    int weeks = 0;
+    if (request["weeks"].is<picojson::array>()) {
+      auto arr = request["weeks"].get<picojson::array>();
+      for (auto& i : arr) {
+        int v = static_cast<int>(i.get<double>());
+        if (v >= 0 && v <= 6) {
+          weeks |= kFlag_map[v];
+        }
+      }
+    }
+    HandleAtDate(target, appcontrol_json, date, weeks);
+  } else if (cmd == "remove") {
+    std::string id = request["id"].to_str();
+    HandleRemove(id);
+  } else if (cmd == "removeAll") {
+    HandleRemoveAll();
+  } else if (cmd == "validation") {
+    std::string id = request["id"].to_str();
+    HandleValidation(id);
+  } else if (cmd == "recurrenceWeeks") {
+    std::string id = request["id"].to_str();
+    HandleRecurrenceWeeks(id);
+  } else if (cmd == "scheduleDate") {
+    std::string id = request["id"].to_str();
+    HandleScheduleDate(id);
+  } else if (cmd == "appcontrol") {
+    std::string id = request["id"].to_str();
+    HandleAppControl(id);
+  } else {
+    LOGE("Unknown cmd");
+  }
+}
+
+void AlarmInstance::HandleAfterSchduleAfter(const std::string& target,
+                                            const std::string& appcontrol_json,
+                                            int after) {
+  picojson::object result;
+  app_control_h appcontrol = MakeAppControlFromJSON(appcontrol_json);
+  if (appcontrol == nullptr) {
+    result["result"] = picojson::value("FAIL");
+    result["reason"] = picojson::value("Invalid appcontrol json data");
+    SendSyncReply(picojson::value(result).serialize().c_str());
+    return;
+  }
+  app_control_set_app_id(appcontrol, target.c_str());
+
+  int alarm_id = 0;
+  int ret = alarm_schedule_once_after_delay(appcontrol, after, &alarm_id);
+  if (ret == 0) {
+    result["result"] = picojson::value("OK");
+    result["id"] = picojson::value(std::to_string(alarm_id));
+  } else {
+    ErrorHandle(ret, &result);
+  }
+  SendSyncReply(picojson::value(result).serialize().c_str());
+  app_control_destroy(appcontrol);
+}
+
+void AlarmInstance::HandleAtDate(const std::string& target,
+                                 const std::string& appcontrol_json,
+                                 int64_t date,
+                                 int weeks) {
+  picojson::object result;
+  time_t second = date / 1000;
+  struct tm start_date;
+
+  if (nullptr == localtime_r(&second, &start_date)) {
+    result["result"] = picojson::value("FAIL");
+    result["reason"] = picojson::value("Invalid date");
+    SendSyncReply(picojson::value(result).serialize().c_str());
+    return;
+  }
+
+  app_control_h appcontrol = MakeAppControlFromJSON(appcontrol_json);
+  if (appcontrol == nullptr) {
+    result["result"] = picojson::value("FAIL");
+    result["reason"] = picojson::value("Invalid appcontrol json data");
+    SendSyncReply(picojson::value(result).serialize().c_str());
+    return;
+  }
+  app_control_set_app_id(appcontrol, target.c_str());
+
+  int alarm_id = 0;
+  int ret = 0;
+  if (!weeks) {
+    ret = alarm_schedule_once_at_date(appcontrol, &start_date, &alarm_id);
+  } else {
+    ret = alarm_schedule_with_recurrence_week_flag(appcontrol, &start_date,
+                                                   weeks, &alarm_id);
+  }
+  if (ret == 0) {
+    result["result"] = picojson::value("OK");
+    result["id"] = picojson::value(std::to_string(alarm_id));
+  } else {
+    ErrorHandle(ret, &result);
+  }
+  SendSyncReply(picojson::value(result).serialize().c_str());
+  app_control_destroy(appcontrol);
+}
+
+void AlarmInstance::HandleRemove(const std::string& id) {
+  int alarm_id = atoi(id.c_str());
+  alarm_cancel(alarm_id);
+  SendSyncReply("{}");
+}
+
+void AlarmInstance::HandleRemoveAll() {
+  alarm_cancel_all();
+  SendSyncReply("{}");
+}
+
+void AlarmInstance::HandleValidation(const std::string& id) {
+  picojson::object result;
+  int alarm_id = atoi(id.c_str());
+  struct tm date;
+  if (alarm_get_scheduled_date(alarm_id, &date) == 0) {
+    result["result"] = picojson::value("OK");
+  } else {
+    result["result"] = picojson::value("FAIL");
+  }
+  SendSyncReply(picojson::value(result).serialize().c_str());
+}
+
+void AlarmInstance::HandleRecurrenceWeeks(const std::string& id) {
+  int alarm_id = atoi(id.c_str());
+  picojson::object result;
+  picojson::array arr;
+
+  int weeks = 0;
+  alarm_get_scheduled_recurrence_week_flag(alarm_id, &weeks);
+  for (int i=0; i < 7; i++) {
+    if ((kFlag_map[i] & weeks) != 0) {
+      arr.push_back(picojson::value(static_cast<double>(i)));
+    }
+  }
+  result["data"] = picojson::value(arr);
+  SendSyncReply(picojson::value(result).serialize().c_str());
+}
+
+void AlarmInstance::HandleScheduleDate(const std::string& id) {
+  int alarm_id = atoi(id.c_str());
+  picojson::object result;
+  struct tm date;
+  if (alarm_get_scheduled_date(alarm_id, &date) == 0) {
+    time_t time_value = mktime(&date);
+    result["result"] = picojson::value("OK");
+    result["data"] = picojson::value(static_cast<double>(time_value*1000));
+  }
+  SendSyncReply(picojson::value(result).serialize().c_str());
+}
+
+void AlarmInstance::HandleAppControl(const std::string& id) {
+  int alarm_id = atoi(id.c_str());
+  picojson::object result;
+  app_control_h appcontrol = nullptr;
+
+  if (alarm_get_app_control(alarm_id, &appcontrol) == 0) {
+    bundle* b = nullptr;
+    app_control_to_bundle(appcontrol, &b);
+    char* json = nullptr;
+    if (bundle_to_json(b, &json) == 0) {
+      result["result"] = picojson::value("OK");
+      result["data"] = picojson::value(json);
+      free(json);
+    }
+    bundle_free(b);
+    app_control_destroy(appcontrol);
+  }
+  SendSyncReply(picojson::value(result).serialize().c_str());
+}
+
+struct CallbackData {
+  char* json;
+  AlarmInstance* self;
+};
+
+void AlarmInstance::HandleGetAlarms(const std::string& asyncid) {
+  std::thread task([asyncid, this]() {
+    picojson::object result;
+    picojson::array arr;
+    result["asyncid"] = picojson::value(asyncid);
+
+    int ret = alarm_foreach_registered_alarm([](int alarm_id, void* user_data) {
+      picojson::array& arr = *(static_cast<picojson::array*>(user_data));
+      arr.push_back(picojson::value(std::to_string(alarm_id)));
+      return true;
+    }, &arr);
+
+    if (ret == 0) {
+      result["data"] = picojson::value(arr);
+    } else {
+      ErrorHandle(ret , &result);
+    }
+
+    char* json = strdup(picojson::value(result).serialize().c_str());
+    CallbackData* user_data = new CallbackData();
+    user_data->json = json;
+    user_data->self = this;
+    g_idle_add([](void* user_data) {
+      CallbackData* data = static_cast<CallbackData*>(user_data);
+      data->self->PostMessage(data->json);
+      free(data->json);
+      delete data;
+      return 0;
+    }, user_data);
+  });
+  task.detach();
+}
+
+}  // namespace appfw
+
+EXPORT_XWALK_EXTENSION(tizen_alarm, appfw::AlarmExtension);
diff --git a/modules/tizen-alarm/alarm_extension.h b/modules/tizen-alarm/alarm_extension.h
new file mode 100755 (executable)
index 0000000..bceb1a4
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef ALARM_EXTENSION_H_
+#define ALARM_EXTENSION_H_
+
+#include <xwalk/xwalk_extension.h>
+#include <string>
+
+namespace appfw {
+
+class AlarmExtension : public xwalk::XWalkExtension {
+ public:
+  // @override
+  xwalk::XWalkExtensionInstance* CreateInstance();
+};
+
+class AlarmInstance : public xwalk::XWalkExtensionInstance {
+ public:
+  // @override
+  void Initialize();
+
+  // @override
+  void HandleMessage(const char* msg);
+
+  // @override
+  void HandleSyncMessage(const char* msg);
+
+ private:
+  void HandleAfterSchduleAfter(const std::string& target,
+                               const std::string& appcontrol_json,
+                               int after);
+  void HandleAtDate(const std::string& target,
+                    const std::string& appcontrol_json,
+                    int64_t date,
+                    int weeks);
+  void HandleRemove(const std::string& id);
+  void HandleRemoveAll();
+  void HandleValidation(const std::string& id);
+  void HandleRecurrenceWeeks(const std::string& id);
+  void HandleScheduleDate(const std::string& id);
+  void HandleAppControl(const std::string& id);
+  void HandleGetAlarms(const std::string& asyncid);
+};
+
+}  // namespace appfw
+
+#endif  // ALARM_EXTENSION_H_
+
diff --git a/modules/tizen-alarm/package.json b/modules/tizen-alarm/package.json
new file mode 100755 (executable)
index 0000000..046e2fd
--- /dev/null
@@ -0,0 +1,13 @@
+{
+  "name": "tizen-alarm",
+  "version": "0.0.1",
+  "description": "Module for alarm",
+  "main": "tizen-alarm.xwalk",
+  "dependencies": {
+    "tizen-app-control": ">= 0.0.1"
+  },
+  "author": {
+    "name": "Seungkeun Lee",
+    "email": "sngn.lee@samsung.com"
+  }
+}
diff --git a/modules/tizen-alarm/picojson.h b/modules/tizen-alarm/picojson.h
new file mode 100755 (executable)
index 0000000..0ae6851
--- /dev/null
@@ -0,0 +1,1037 @@
+/*
+ * Copyright 2009-2010 Cybozu Labs, Inc.
+ * Copyright 2011 Kazuho Oku
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CYBOZU LABS, INC. ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL CYBOZU LABS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are
+ * those of the authors and should not be interpreted as representing official
+ * policies, either expressed or implied, of Cybozu Labs, Inc.
+ *
+ */
+#ifndef picojson_h
+#define picojson_h
+
+#include <algorithm>
+#include <cassert>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <iostream>
+#include <iterator>
+#include <map>
+#include <string>
+#include <vector>
+
+#ifdef _MSC_VER
+    #define SNPRINTF _snprintf_s
+    #pragma warning(push)
+    #pragma warning(disable : 4244) // conversion from int to char
+#else
+    #define SNPRINTF snprintf
+#endif
+
+namespace picojson {
+
+  enum {
+    null_type,
+    boolean_type,
+    number_type,
+    string_type,
+    array_type,
+    object_type
+  };
+
+  struct null {};
+
+  class value {
+  public:
+    typedef std::vector<value> array;
+    typedef std::map<std::string, value> object;
+    union _storage {
+      bool boolean_;
+      double number_;
+      std::string* string_;
+      array* array_;
+      object* object_;
+    };
+  protected:
+    int type_;
+    _storage u_;
+  public:
+    value();
+    value(int type, bool);
+    explicit value(bool b);
+    explicit value(double n);
+    explicit value(const std::string& s);
+    explicit value(const array& a);
+    explicit value(const object& o);
+    explicit value(const char* s);
+    value(const char* s, size_t len);
+    ~value();
+    value(const value& x);
+    value& operator=(const value& x);
+    void swap(value& x);
+    template <typename T> bool is() const;
+    template <typename T> const T& get() const;
+    template <typename T> T& get();
+    bool evaluate_as_boolean() const;
+    const value& get(size_t idx) const;
+    const value& get(const std::string& key) const;
+    bool contains(size_t idx) const;
+    bool contains(const std::string& key) const;
+    std::string to_str() const;
+    template <typename Iter> void serialize(Iter os) const;
+    std::string serialize() const;
+  private:
+    template <typename T> value(const T*); // intentionally defined to block implicit conversion of pointer to bool
+  };
+
+  typedef value::array array;
+  typedef value::object object;
+
+  inline value::value() : type_(null_type) {}
+
+  inline value::value(int type, bool) : type_(type) {
+    switch (type) {
+#define INIT(p, v) case p##type: u_.p = v; break
+      INIT(boolean_, false);
+      INIT(number_, 0.0);
+      INIT(string_, new std::string());
+      INIT(array_, new array());
+      INIT(object_, new object());
+#undef INIT
+    default: break;
+    }
+  }
+
+  inline value::value(bool b) : type_(boolean_type) {
+    u_.boolean_ = b;
+  }
+
+  inline value::value(double n) : type_(number_type) {
+    u_.number_ = n;
+  }
+
+  inline value::value(const std::string& s) : type_(string_type) {
+    u_.string_ = new std::string(s);
+  }
+
+  inline value::value(const array& a) : type_(array_type) {
+    u_.array_ = new array(a);
+  }
+
+  inline value::value(const object& o) : type_(object_type) {
+    u_.object_ = new object(o);
+  }
+
+  inline value::value(const char* s) : type_(string_type) {
+    u_.string_ = new std::string(s);
+  }
+
+  inline value::value(const char* s, size_t len) : type_(string_type) {
+    u_.string_ = new std::string(s, len);
+  }
+
+  inline value::~value() {
+    switch (type_) {
+#define DEINIT(p) case p##type: delete u_.p; break
+      DEINIT(string_);
+      DEINIT(array_);
+      DEINIT(object_);
+#undef DEINIT
+    default: break;
+    }
+  }
+
+  inline value::value(const value& x) : type_(x.type_) {
+    switch (type_) {
+#define INIT(p, v) case p##type: u_.p = v; break
+      INIT(string_, new std::string(*x.u_.string_));
+      INIT(array_, new array(*x.u_.array_));
+      INIT(object_, new object(*x.u_.object_));
+#undef INIT
+    default:
+      u_ = x.u_;
+      break;
+    }
+  }
+
+  inline value& value::operator=(const value& x) {
+    if (this != &x) {
+      this->~value();
+      new (this) value(x);
+    }
+    return *this;
+  }
+
+  inline void value::swap(value& x) {
+    std::swap(type_, x.type_);
+    std::swap(u_, x.u_);
+  }
+
+#define IS(ctype, jtype)           \
+  template <> inline bool value::is<ctype>() const { \
+    return type_ == jtype##_type;        \
+  }
+  IS(null, null)
+  IS(bool, boolean)
+  IS(int, number)
+  IS(double, number)
+  IS(std::string, string)
+  IS(array, array)
+  IS(object, object)
+#undef IS
+
+#define GET(ctype, var)           \
+  template <> inline const ctype& value::get<ctype>() const { \
+    assert("type mismatch! call vis<type>() before get<type>()" \
+     && is<ctype>());               \
+    return var;             \
+  }               \
+  template <> inline ctype& value::get<ctype>() {   \
+    assert("type mismatch! call is<type>() before get<type>()"  \
+     && is<ctype>());         \
+    return var;             \
+  }
+  GET(bool, u_.boolean_)
+  GET(double, u_.number_)
+  GET(std::string, *u_.string_)
+  GET(array, *u_.array_)
+  GET(object, *u_.object_)
+#undef GET
+
+  inline bool value::evaluate_as_boolean() const {
+    switch (type_) {
+    case null_type:
+      return false;
+    case boolean_type:
+      return u_.boolean_;
+    case number_type:
+      return u_.number_ != 0;
+    case string_type:
+      return ! u_.string_->empty();
+    default:
+      return true;
+    }
+  }
+
+  inline const value& value::get(size_t idx) const {
+    static value s_null;
+    assert(is<array>());
+    return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
+  }
+
+  inline const value& value::get(const std::string& key) const {
+    static value s_null;
+    assert(is<object>());
+    object::const_iterator i = u_.object_->find(key);
+    return i != u_.object_->end() ? i->second : s_null;
+  }
+
+  inline bool value::contains(size_t idx) const {
+    assert(is<array>());
+    return idx < u_.array_->size();
+  }
+
+  inline bool value::contains(const std::string& key) const {
+    assert(is<object>());
+    object::const_iterator i = u_.object_->find(key);
+    return i != u_.object_->end();
+  }
+
+  inline std::string value::to_str() const {
+    switch (type_) {
+    case null_type:      return "null";
+    case boolean_type:   return u_.boolean_ ? "true" : "false";
+    case number_type:    {
+      char buf[256];
+      double tmp;
+      SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_);
+      return buf;
+    }
+    case string_type:    return *u_.string_;
+    case array_type:     return "array";
+    case object_type:    return "object";
+    default:             assert(0);
+#ifdef _MSC_VER
+      __assume(0);
+#endif
+    }
+    return std::string();
+  }
+
+  template <typename Iter> void copy(const std::string& s, Iter oi) {
+    std::copy(s.begin(), s.end(), oi);
+  }
+
+  template <typename Iter> void serialize_str(const std::string& s, Iter oi) {
+    *oi++ = '"';
+    for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
+      switch (*i) {
+#define MAP(val, sym) case val: copy(sym, oi); break
+  MAP('"', "\\\"");
+  MAP('\\', "\\\\");
+  MAP('/', "\\/");
+  MAP('\b', "\\b");
+  MAP('\f', "\\f");
+  MAP('\n', "\\n");
+  MAP('\r', "\\r");
+  MAP('\t', "\\t");
+#undef MAP
+      default:
+  if ((unsigned char)*i < 0x20 || *i == 0x7f) {
+    char buf[7];
+    SNPRINTF(buf, sizeof(buf), "\\u%04x", *i & 0xff);
+    copy(buf, buf + 6, oi);
+    } else {
+    *oi++ = *i;
+  }
+  break;
+      }
+    }
+    *oi++ = '"';
+  }
+
+  template <typename Iter> void value::serialize(Iter oi) const {
+    switch (type_) {
+    case string_type:
+      serialize_str(*u_.string_, oi);
+      break;
+    case array_type: {
+      *oi++ = '[';
+      for (array::const_iterator i = u_.array_->begin();
+           i != u_.array_->end();
+           ++i) {
+  if (i != u_.array_->begin()) {
+    *oi++ = ',';
+  }
+  i->serialize(oi);
+      }
+      *oi++ = ']';
+      break;
+    }
+    case object_type: {
+      *oi++ = '{';
+      for (object::const_iterator i = u_.object_->begin();
+     i != u_.object_->end();
+     ++i) {
+  if (i != u_.object_->begin()) {
+    *oi++ = ',';
+  }
+  serialize_str(i->first, oi);
+  *oi++ = ':';
+  i->second.serialize(oi);
+      }
+      *oi++ = '}';
+      break;
+    }
+    default:
+      copy(to_str(), oi);
+      break;
+    }
+  }
+
+  inline std::string value::serialize() const {
+    std::string s;
+    serialize(std::back_inserter(s));
+    return s;
+  }
+
+  template <typename Iter> class input {
+  protected:
+    Iter cur_, end_;
+    int last_ch_;
+    bool ungot_;
+    int line_;
+  public:
+    input(const Iter& first, const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {}
+    int getc() {
+      if (ungot_) {
+  ungot_ = false;
+  return last_ch_;
+      }
+      if (cur_ == end_) {
+  last_ch_ = -1;
+  return -1;
+      }
+      if (last_ch_ == '\n') {
+  line_++;
+      }
+      last_ch_ = *cur_++ & 0xff;
+      return last_ch_;
+    }
+    void ungetc() {
+      if (last_ch_ != -1) {
+  assert(! ungot_);
+  ungot_ = true;
+      }
+    }
+    Iter cur() const { return cur_; }
+    int line() const { return line_; }
+    void skip_ws() {
+      while (1) {
+  int ch = getc();
+  if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) {
+    ungetc();
+    break;
+  }
+      }
+    }
+    bool expect(int expect) {
+      skip_ws();
+      if (getc() != expect) {
+  ungetc();
+  return false;
+      }
+      return true;
+    }
+    bool match(const std::string& pattern) {
+      for (std::string::const_iterator pi(pattern.begin());
+     pi != pattern.end();
+     ++pi) {
+  if (getc() != *pi) {
+    ungetc();
+    return false;
+  }
+      }
+      return true;
+    }
+  };
+
+  template<typename Iter> inline int _parse_quadhex(input<Iter> &in) {
+    int uni_ch = 0, hex;
+    for (int i = 0; i < 4; i++) {
+      if ((hex = in.getc()) == -1) {
+  return -1;
+      }
+      if ('0' <= hex && hex <= '9') {
+  hex -= '0';
+      } else if ('A' <= hex && hex <= 'F') {
+  hex -= 'A' - 0xa;
+      } else if ('a' <= hex && hex <= 'f') {
+  hex -= 'a' - 0xa;
+      } else {
+  in.ungetc();
+  return -1;
+      }
+      uni_ch = uni_ch * 16 + hex;
+    }
+    return uni_ch;
+  }
+
+  template<typename String, typename Iter> inline bool _parse_codepoint(String& out, input<Iter>& in) {
+    int uni_ch;
+    if ((uni_ch = _parse_quadhex(in)) == -1) {
+      return false;
+    }
+    if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
+      if (0xdc00 <= uni_ch) {
+  // a second 16-bit of a surrogate pair appeared
+  return false;
+      }
+      // first 16-bit of surrogate pair, get the next one
+      if (in.getc() != '\\' || in.getc() != 'u') {
+  in.ungetc();
+  return false;
+      }
+      int second = _parse_quadhex(in);
+      if (! (0xdc00 <= second && second <= 0xdfff)) {
+  return false;
+      }
+      uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
+      uni_ch += 0x10000;
+    }
+    if (uni_ch < 0x80) {
+      out.push_back(uni_ch);
+    } else {
+      if (uni_ch < 0x800) {
+  out.push_back(0xc0 | (uni_ch >> 6));
+      } else {
+  if (uni_ch < 0x10000) {
+    out.push_back(0xe0 | (uni_ch >> 12));
+  } else {
+    out.push_back(0xf0 | (uni_ch >> 18));
+    out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
+  }
+  out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
+      }
+      out.push_back(0x80 | (uni_ch & 0x3f));
+    }
+    return true;
+  }
+
+  template<typename String, typename Iter> inline bool _parse_string(String& out, input<Iter>& in) {
+    while (1) {
+      int ch = in.getc();
+      if (ch < ' ') {
+  in.ungetc();
+  return false;
+      } else if (ch == '"') {
+  return true;
+      } else if (ch == '\\') {
+  if ((ch = in.getc()) == -1) {
+    return false;
+  }
+  switch (ch) {
+#define MAP(sym, val) case sym: out.push_back(val); break
+    MAP('"', '\"');
+    MAP('\\', '\\');
+    MAP('/', '/');
+    MAP('b', '\b');
+    MAP('f', '\f');
+    MAP('n', '\n');
+    MAP('r', '\r');
+    MAP('t', '\t');
+#undef MAP
+  case 'u':
+    if (! _parse_codepoint(out, in)) {
+      return false;
+    }
+    break;
+  default:
+    return false;
+  }
+      } else {
+  out.push_back(ch);
+      }
+    }
+    return false;
+  }
+
+  template <typename Context, typename Iter> inline bool _parse_array(Context& ctx, input<Iter>& in) {
+    if (! ctx.parse_array_start()) {
+      return false;
+    }
+    if (in.expect(']')) {
+      return true;
+    }
+    size_t idx = 0;
+    do {
+      if (! ctx.parse_array_item(in, idx)) {
+  return false;
+      }
+      idx++;
+    } while (in.expect(','));
+    return in.expect(']');
+  }
+
+  template <typename Context, typename Iter> inline bool _parse_object(Context& ctx, input<Iter>& in) {
+    if (! ctx.parse_object_start()) {
+      return false;
+    }
+    if (in.expect('}')) {
+      return true;
+    }
+    do {
+      std::string key;
+      if (! in.expect('"')
+    || ! _parse_string(key, in)
+    || ! in.expect(':')) {
+  return false;
+      }
+      if (! ctx.parse_object_item(in, key)) {
+  return false;
+      }
+    } while (in.expect(','));
+    return in.expect('}');
+  }
+
+  template <typename Iter> inline bool _parse_number(double& out, input<Iter>& in) {
+    std::string num_str;
+    while (1) {
+      int ch = in.getc();
+      if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.'
+    || ch == 'e' || ch == 'E') {
+  num_str.push_back(ch);
+      } else {
+  in.ungetc();
+  break;
+      }
+    }
+    char* endp;
+    out = strtod(num_str.c_str(), &endp);
+    return endp == num_str.c_str() + num_str.size();
+  }
+
+  template <typename Context, typename Iter> inline bool _parse(Context& ctx, input<Iter>& in) {
+    in.skip_ws();
+    int ch = in.getc();
+    switch (ch) {
+#define IS(ch, text, op) case ch: \
+      if (in.match(text) && op) { \
+  return true; \
+      } else { \
+  return false; \
+      }
+      IS('n', "ull", ctx.set_null());
+      IS('f', "alse", ctx.set_bool(false));
+      IS('t', "rue", ctx.set_bool(true));
+#undef IS
+    case '"':
+      return ctx.parse_string(in);
+    case '[':
+      return _parse_array(ctx, in);
+    case '{':
+      return _parse_object(ctx, in);
+    default:
+      if (('0' <= ch && ch <= '9') || ch == '-') {
+  in.ungetc();
+  double f;
+  if (_parse_number(f, in)) {
+    ctx.set_number(f);
+    return true;
+  } else {
+    return false;
+  }
+      }
+      break;
+    }
+    in.ungetc();
+    return false;
+  }
+
+  class deny_parse_context {
+  public:
+    bool set_null() { return false; }
+    bool set_bool(bool) { return false; }
+    bool set_number(double) { return false; }
+    template <typename Iter> bool parse_string(input<Iter>&) { return false; }
+    bool parse_array_start() { return false; }
+    template <typename Iter> bool parse_array_item(input<Iter>&, size_t) {
+      return false;
+    }
+    bool parse_object_start() { return false; }
+    template <typename Iter> bool parse_object_item(input<Iter>&, const std::string&) {
+      return false;
+    }
+  };
+
+  class default_parse_context {
+  protected:
+    value* out_;
+  public:
+    default_parse_context(value* out) : out_(out) {}
+    bool set_null() {
+      *out_ = value();
+      return true;
+    }
+    bool set_bool(bool b) {
+      *out_ = value(b);
+      return true;
+    }
+    bool set_number(double f) {
+      *out_ = value(f);
+      return true;
+    }
+    template<typename Iter> bool parse_string(input<Iter>& in) {
+      *out_ = value(string_type, false);
+      return _parse_string(out_->get<std::string>(), in);
+    }
+    bool parse_array_start() {
+      *out_ = value(array_type, false);
+      return true;
+    }
+    template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
+      array& a = out_->get<array>();
+      a.push_back(value());
+      default_parse_context ctx(&a.back());
+      return _parse(ctx, in);
+    }
+    bool parse_object_start() {
+      *out_ = value(object_type, false);
+      return true;
+    }
+    template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string& key) {
+      object& o = out_->get<object>();
+      default_parse_context ctx(&o[key]);
+      return _parse(ctx, in);
+    }
+  private:
+    default_parse_context(const default_parse_context&);
+    default_parse_context& operator=(const default_parse_context&);
+  };
+
+  class null_parse_context {
+  public:
+    struct dummy_str {
+      void push_back(int) {}
+    };
+  public:
+    null_parse_context() {}
+    bool set_null() { return true; }
+    bool set_bool(bool) { return true; }
+    bool set_number(double) { return true; }
+    template <typename Iter> bool parse_string(input<Iter>& in) {
+      dummy_str s;
+      return _parse_string(s, in);
+    }
+    bool parse_array_start() { return true; }
+    template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
+      return _parse(*this, in);
+    }
+    bool parse_object_start() { return true; }
+    template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string&) {
+      return _parse(*this, in);
+    }
+  private:
+    null_parse_context(const null_parse_context&);
+    null_parse_context& operator=(const null_parse_context&);
+  };
+
+  // obsolete, use the version below
+  template <typename Iter> inline std::string parse(value& out, Iter& pos, const Iter& last) {
+    std::string err;
+    pos = parse(out, pos, last, &err);
+    return err;
+  }
+
+  template <typename Context, typename Iter> inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) {
+    input<Iter> in(first, last);
+    if (! _parse(ctx, in) && err != NULL) {
+      char buf[64];
+      SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line());
+      *err = buf;
+      while (1) {
+  int ch = in.getc();
+  if (ch == -1 || ch == '\n') {
+    break;
+  } else if (ch >= ' ') {
+    err->push_back(ch);
+  }
+      }
+    }
+    return in.cur();
+  }
+
+  template <typename Iter> inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) {
+    default_parse_context ctx(&out);
+    return _parse(ctx, first, last, err);
+  }
+
+  inline std::string parse(value& out, std::istream& is) {
+    std::string err;
+    parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
+    std::istreambuf_iterator<char>(), &err);
+    return err;
+  }
+
+  template <typename T> struct last_error_t {
+    static std::string s;
+  };
+  template <typename T> std::string last_error_t<T>::s;
+
+  inline void set_last_error(const std::string& s) {
+    last_error_t<bool>::s = s;
+  }
+
+  inline const std::string& get_last_error() {
+    return last_error_t<bool>::s;
+  }
+
+  inline bool operator==(const value& x, const value& y) {
+    if (x.is<null>())
+      return y.is<null>();
+#define PICOJSON_CMP(type)          \
+    if (x.is<type>())           \
+      return y.is<type>() && x.get<type>() == y.get<type>()
+    PICOJSON_CMP(bool);
+    PICOJSON_CMP(double);
+    PICOJSON_CMP(std::string);
+    PICOJSON_CMP(array);
+    PICOJSON_CMP(object);
+#undef PICOJSON_CMP
+    assert(0);
+#ifdef _MSC_VER
+    __assume(0);
+#endif
+    return false;
+  }
+
+  inline bool operator!=(const value& x, const value& y) {
+    return ! (x == y);
+  }
+}
+
+namespace std {
+  template<> inline void swap(picojson::value& x, picojson::value& y)
+    {
+      x.swap(y);
+    }
+}
+
+inline std::istream& operator>>(std::istream& is, picojson::value& x)
+{
+  picojson::set_last_error(std::string());
+  std::string err = picojson::parse(x, is);
+  if (! err.empty()) {
+    picojson::set_last_error(err);
+    is.setstate(std::ios::failbit);
+  }
+  return is;
+}
+
+inline std::ostream& operator<<(std::ostream& os, const picojson::value& x)
+{
+  x.serialize(std::ostream_iterator<char>(os));
+  return os;
+}
+#ifdef _MSC_VER
+    #pragma warning(pop)
+#endif
+
+#endif
+#ifdef TEST_PICOJSON
+#ifdef _MSC_VER
+    #pragma warning(disable : 4127) // conditional expression is constant
+#endif
+
+using namespace std;
+
+static void plan(int num)
+{
+  printf("1..%d\n", num);
+}
+
+static bool success = true;
+
+static void ok(bool b, const char* name = "")
+{
+  static int n = 1;
+  if (! b)
+    success = false;
+  printf("%s %d - %s\n", b ? "ok" : "ng", n++, name);
+}
+
+template <typename T> void is(const T& x, const T& y, const char* name = "")
+{
+  if (x == y) {
+    ok(true, name);
+  } else {
+    ok(false, name);
+  }
+}
+
+#include <algorithm>
+#include <sstream>
+#include <float.h>
+#include <limits.h>
+
+int main(void)
+{
+  plan(85);
+
+  // constructors
+#define TEST(expr, expected) \
+    is(picojson::value expr .serialize(), string(expected), "picojson::value" #expr)
+
+  TEST( (true),  "true");
+  TEST( (false), "false");
+  TEST( (42.0),   "42");
+  TEST( (string("hello")), "\"hello\"");
+  TEST( ("hello"), "\"hello\"");
+  TEST( ("hello", 4), "\"hell\"");
+
+  {
+    double a = 1;
+    for (int i = 0; i < 1024; i++) {
+      picojson::value vi(a);
+      std::stringstream ss;
+      ss << vi;
+      picojson::value vo;
+      ss >> vo;
+      double b = vo.get<double>();
+      if ((i < 53 && a != b) || fabs(a - b) / b > 1e-8) {
+        printf("ng i=%d a=%.18e b=%.18e\n", i, a, b);
+      }
+      a *= 2;
+    }
+  }
+
+#undef TEST
+
+#define TEST(in, type, cmp, serialize_test) {       \
+    picojson::value v;              \
+    const char* s = in;             \
+    string err = picojson::parse(v, s, s + strlen(s));      \
+    ok(err.empty(), in " no error");          \
+    ok(v.is<type>(), in " check type");         \
+    is<type>(v.get<type>(), cmp, in " correct output");     \
+    is(*s, '\0', in " read to eof");          \
+    if (serialize_test) {           \
+      is(v.serialize(), string(in), in " serialize");     \
+    }                 \
+  }
+  TEST("false", bool, false, true);
+  TEST("true", bool, true, true);
+  TEST("90.5", double, 90.5, false);
+  TEST("1.7976931348623157e+308", double, DBL_MAX, false);
+  TEST("\"hello\"", string, string("hello"), true);
+  TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"),
+       true);
+  TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string,
+       string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false);
+  TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false);
+#undef TEST
+
+#define TEST(type, expr) {                 \
+    picojson::value v;                   \
+    const char *s = expr;                \
+    string err = picojson::parse(v, s, s + strlen(s));           \
+    ok(err.empty(), "empty " #type " no error");           \
+    ok(v.is<picojson::type>(), "empty " #type " check type");        \
+    ok(v.get<picojson::type>().empty(), "check " #type " array size"); \
+  }
+  TEST(array, "[]");
+  TEST(object, "{}");
+#undef TEST
+
+  {
+    picojson::value v;
+    const char *s = "[1,true,\"hello\"]";
+    string err = picojson::parse(v, s, s + strlen(s));
+    ok(err.empty(), "array no error");
+    ok(v.is<picojson::array>(), "array check type");
+    is(v.get<picojson::array>().size(), size_t(3), "check array size");
+    ok(v.contains(0), "check contains array[0]");
+    ok(v.get(0).is<double>(), "check array[0] type");
+    is(v.get(0).get<double>(), 1.0, "check array[0] value");
+    ok(v.contains(1), "check contains array[1]");
+    ok(v.get(1).is<bool>(), "check array[1] type");
+    ok(v.get(1).get<bool>(), "check array[1] value");
+    ok(v.contains(2), "check contains array[2]");
+    ok(v.get(2).is<string>(), "check array[2] type");
+    is(v.get(2).get<string>(), string("hello"), "check array[2] value");
+    ok(!v.contains(3), "check not contains array[3]");
+  }
+
+  {
+    picojson::value v;
+    const char *s = "{ \"a\": true }";
+    string err = picojson::parse(v, s, s + strlen(s));
+    ok(err.empty(), "object no error");
+    ok(v.is<picojson::object>(), "object check type");
+    is(v.get<picojson::object>().size(), size_t(1), "check object size");
+    ok(v.contains("a"), "check contains property");
+    ok(v.get("a").is<bool>(), "check bool property exists");
+    is(v.get("a").get<bool>(), true, "check bool property value");
+    is(v.serialize(), string("{\"a\":true}"), "serialize object");
+    ok(!v.contains("z"), "check not contains property");
+  }
+
+#define TEST(json, msg) do {        \
+    picojson::value v;          \
+    const char *s = json;       \
+    string err = picojson::parse(v, s, s + strlen(s));  \
+    is(err, string("syntax error at line " msg), msg);  \
+  } while (0)
+  TEST("falsoa", "1 near: oa");
+  TEST("{]", "1 near: ]");
+  TEST("\n\bbell", "2 near: bell");
+  TEST("\"abc\nd\"", "1 near: ");
+#undef TEST
+
+  {
+    picojson::value v1, v2;
+    const char *s;
+    string err;
+    s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
+    err = picojson::parse(v1, s, s + strlen(s));
+    s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }";
+    err = picojson::parse(v2, s, s + strlen(s));
+    ok((v1 == v2), "check == operator in deep comparison");
+  }
+
+  {
+    picojson::value v1, v2;
+    const char *s;
+    string err;
+    s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
+    err = picojson::parse(v1, s, s + strlen(s));
+    s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }";
+    err = picojson::parse(v2, s, s + strlen(s));
+    ok((v1 != v2), "check != operator for array in deep comparison");
+  }
+
+  {
+    picojson::value v1, v2;
+    const char *s;
+    string err;
+    s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
+    err = picojson::parse(v1, s, s + strlen(s));
+    s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }";
+    err = picojson::parse(v2, s, s + strlen(s));
+    ok((v1 != v2), "check != operator for object in deep comparison");
+  }
+
+  {
+    picojson::value v1, v2;
+    const char *s;
+    string err;
+    s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
+    err = picojson::parse(v1, s, s + strlen(s));
+    picojson::object& o = v1.get<picojson::object>();
+    o.erase("b");
+    picojson::array& a = o["a"].get<picojson::array>();
+    picojson::array::iterator i;
+    i = std::remove(a.begin(), a.end(), picojson::value(std::string("three")));
+    a.erase(i, a.end());
+    s = "{ \"a\": [1,2], \"d\": 2 }";
+    err = picojson::parse(v2, s, s + strlen(s));
+    ok((v1 == v2), "check erase()");
+  }
+
+  ok(picojson::value(3.0).serialize() == "3",
+     "integral number should be serialized as a integer");
+
+  {
+    const char* s = "{ \"a\": [1,2], \"d\": 2 }";
+    picojson::null_parse_context ctx;
+    string err;
+    picojson::_parse(ctx, s, s + strlen(s), &err);
+    ok(err.empty(), "null_parse_context");
+  }
+
+  {
+    picojson::value v1, v2;
+    v1 = picojson::value(true);
+    swap(v1, v2);
+    ok(v1.is<picojson::null>(), "swap (null)");
+    ok(v2.get<bool>() == true, "swap (bool)");
+
+    v1 = picojson::value("a");
+    v2 = picojson::value(1.0);
+    swap(v1, v2);
+    ok(v1.get<double>() == 1.0, "swap (dobule)");
+    ok(v2.get<string>() == "a", "swap (string)");
+
+    v1 = picojson::value(picojson::object());
+    v2 = picojson::value(picojson::array());
+    swap(v1, v2);
+    ok(v1.is<picojson::array>(), "swap (array)");
+    ok(v2.is<picojson::object>(), "swap (object)");
+  }
+
+  return success ? 0 : 1;
+}
+
+#endif
diff --git a/modules/tizen-alarm/tizen-alarm_api.js b/modules/tizen-alarm/tizen-alarm_api.js
new file mode 100755 (executable)
index 0000000..188ff1f
--- /dev/null
@@ -0,0 +1,296 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+'use strict';
+
+function native_sync_call(method, parameter) {
+  var args = {};
+  args['cmd'] = method;
+  args = Object.assign(args, parameter);
+  try {
+    return JSON.parse(extension.internal.sendSyncMessage(JSON.stringify(args)));
+  } catch (e) {
+    console.log('recevied message parse error:' + e.message);
+    return {};
+  }
+}
+
+var async_message_id = 0;
+var async_map = new Map();
+
+function native_async_call(method, parameter, cb) {
+  var args = {};
+  args['cmd'] = method;
+  args = Object.assign(args, parameter);
+  var asyncid = async_message_id++;
+  args['asyncid'] = 'asyncid_' + asyncid;
+  async_map.set(args['asyncid'], cb);
+  extension.postMessage(JSON.stringify(args));
+}
+
+extension.setMessageListener(function(json) {
+  var msg = JSON.parse(json);
+  if (msg['asyncid'] && async_map.has(msg['asyncid'])) {
+    var cb = async_map.get(msg['asyncid']);
+    async_map.delete(msg['asyncid']);
+    if (cb instanceof Function) {
+      cb(msg);
+    } else {
+      console.log('cb is not function');
+    }
+  }
+});
+
+var AppControl = require('tizen-app-control');
+var util = require('util');
+
+
+/**
+ * The tizen alarm module
+ *
+ * ```
+ * var AppControl = require('tizen-app-control');
+ * var app = require('tizen-application');
+ * var alarm_manager = require('alarm-manager');
+ * var appcontrol = new AppControl(AppControl.OPERATION_VIEW);
+ * alarm_manager.scheduleAfter(app.id, appcontrol, 60);
+ *
+ * ```
+ * @module tizen-alarm
+ *
+ */
+
+
+
+var internal_id = Symbol();
+var internal_appcontrol = Symbol();
+/**
+ * @class Alarm
+ *
+ * The class of alarm data
+ */
+class Alarm {
+  constructor(id) {
+    this[internal_id] = id;
+  }
+
+  /**
+   * Identifier of alarm
+   * @attribute id
+   * @type {string}
+   * @readonly
+   */
+  get id() {
+    return this[internal_id];
+  }
+
+  /**
+   * @attribute isRecurrence
+   * @type {boolean}
+   * @readonly
+   */
+  get isRecurrence() {
+    return this.recurrenceWeeks.length > 0;
+  }
+
+  /**
+   * @attribute recurrenceWeeks
+   * @type {Array}
+   * @readonly
+   * * Sunday is 0 and saturday is 6
+   */
+  get recurrenceWeeks() {
+    return native_sync_call('recurrenceWeeks', {'id': this.id})['data'];
+  }
+
+  /**
+   * @attribute scheduleDate
+   * @type {Date}
+   * @readonly
+   */
+  get scheduleDate() {
+    var result = native_sync_call('scheduleDate', {'id': this.id});
+    if (result['result'] === 'OK') {
+      return new Date(result['data']);
+    }
+    return undefined;
+  }
+
+  /**
+   * @attribute appControl
+   * @type {AppControl}
+   * @readonly
+   */
+  get appControl() {
+    if (this[internal_appcontrol]) {
+      return this[internal_appcontrol];
+    }
+    var result = native_sync_call('appcontrol', {'id': this.id});
+    if (result['result'] === 'OK') {
+      var appcontrol = AppControl.FromJSON(result['data']);
+      Object.freeze(appcontrol);
+      this[internal_appcontrol] = appcontrol;
+      return appcontrol;
+    }
+    return undefined;
+  }
+};
+
+/**
+ * @class AlarmManager
+ * The AlarmManager class provides methods to manage alarms.
+ */
+class AlarmManager {
+  constructor() {
+
+  }
+
+  /**
+   * Sets an alarm to be triggered after a specific time.
+   * The alarm will go off @a delay seconds later.
+   * To cancel the alarm, call removeAlarm() with @a Alarm.
+   * @method scheduleAfter
+   * @param {string} targetAppId AppID to launch
+   * @param {AppControl} appcontrol
+   * @param {number} afterInSecond
+   * @param {string} optional targetAppID appid to receive alarm
+   * @return {Alarm}
+   * @privilege http://tizen.org/privilege/alarm.set
+   * @throws
+   *  * Error - Fail to add alarm
+   */
+  scheduleAfter(targetAppID, appcontrol, afterInSecond) {
+    if (!(appcontrol instanceof AppControl)) {
+      throw new TypeError('first argument was not AppControl instance');
+    }
+    if (!util.isNumber(afterInSecond)) {
+      throw new TypeError('second argument was not Number');
+    }
+    afterInSecond = Math.floor(afterInSecond);
+    var args = {};
+    args['target'] = targetAppID;
+    args['appcontrol'] = appcontrol.toJSON();
+    args['after'] = afterInSecond;
+
+    var result = native_sync_call('scheduleAfter', args);
+    if (result['result'] !== 'OK') {
+      throw Error(result['reason']);
+    }
+    return new Alarm(result['id']);
+  }
+
+  /**
+   * Sets an alarm to be triggered at a specific time.
+   * The @a date describes the time of the first occurrence.
+   * To cancel the alarm, call removeAlarm() with @a Alarm.
+   * @method scheduleAtdate
+   * @param {string} targetAppId AppID to launch
+   * @param {AppControl} appcontrol
+   * @param {Date} date fired date
+   * @param {Array} recurrenceWeeks weeks array to recurrence
+   *  * Sunday is 0 and saturday is 6
+   * @return {Alarm}
+   * @privilege http://tizen.org/privilege/alarm.set
+   * @throws
+   *  * Error - Fail to add alarm
+   */
+  scheduleAtDate(targetAppID, appcontrol, date, recurrenceWeeks) {
+    if (!(appcontrol instanceof AppControl)) {
+      throw new TypeError('first argument was not AppControl instance');
+    }
+    if (!util.isDate(date)) {
+      throw new TypeError('second argument was not Date');
+    }
+    var args = {};
+    args['target'] = targetAppID;
+    args['appcontrol'] = appcontrol.toJSON();
+    args['date'] = date.getTime();
+    if (util.isArray(recurrenceWeeks)) {
+      var weeks = [];
+      for (var i = 0; i < recurrenceWeeks.length; i++) {
+        if (recurrenceWeeks[i] >= 0 && recurrenceWeeks[i] <= 6)
+          weeks.push(recurrenceWeeks[i]);
+      }
+      if (weeks.length > 0)
+        args['weeks'] = weeks;
+    }
+    var result = native_sync_call('scheduleAtDate', args);
+    if (result['result'] !== 'OK') {
+      throw Error(result['reason']);
+    }
+    return new Alarm(result['id']);
+  }
+
+  /**
+   * Remove alarm
+   * @method removeAlarm
+   * @param {string | Alarm} alarm instance or alarm.id
+   * @privilege http://tizen.org/privilege/alarm.set
+   */
+  removeAlarm(alarm) {
+    var id = alarm;
+    if (alarm instanceof Alarm)
+      id = alarm.id;
+    native_sync_call('remove', {'id': id});
+  }
+
+  /**
+   * Remove all alarms
+   * @method removeAll
+   * @privilege http://tizen.org/privilege/alarm.set
+   */
+  removeAll() {
+    native_sync_call('removeAll');
+  }
+
+  /**
+   * Get alarm
+   * @method getAlarm
+   * @param {string} id
+   * @privilege http://tizen.org/privilege/alarm.set
+   * @return {Alarm} if does not existed, undefined was returned
+   */
+  getAlarm(id) {
+    var result = native_sync_call('validation', {'id': id});
+    if (result['result'] === 'OK')
+      return new Alarm(id);
+    else
+      return undefined;
+  }
+
+  /**
+   * Get all alarms
+   * @method getAlarms
+   * @privilege http://tizen.org/privilege/alarm.set
+   * @return {Promise | Alarm[]} Array of Alarm
+   */
+  getAlarms() {
+    return new Promise(function(resolve, reject) {
+      native_async_call('getAlarms', undefined, function(result) {
+        if (result['reason']) {
+          reject(new Error(result['reason']));
+          return;
+        }
+        var alarms = result['data'].map(function(id) {
+          return new Alarm(id);
+        });
+        resolve(alarms);
+      });
+    });
+  }
+};
+
+exports = new AlarmManager();