+// write_json(data[, styled]) -> string or nil and error message
+int ModApiUtil::l_write_json(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ bool styled = false;
+ if (!lua_isnone(L, 2)) {
+ styled = lua_toboolean(L, 2);
+ lua_pop(L, 1);
+ }
+
+ Json::Value root;
+ try {
+ read_json_value(L, root, 1);
+ } catch (SerializationError &e) {
+ lua_pushnil(L);
+ lua_pushstring(L, e.what());
+ return 2;
+ }
+
+ std::string out;
+ if (styled) {
+ Json::StyledWriter writer;
+ out = writer.write(root);
+ } else {
+ Json::FastWriter writer;
+ out = writer.write(root);
+ }
+ lua_pushlstring(L, out.c_str(), out.size());
+ return 1;
+}
+