void CTCPServer::Announce(EAnnouncementFlag flag, const char *sender, const char *message, const CVariant &data) { Value root; root["jsonrpc"] = "2.0"; root["method"] = "Announcement"; root["params"]["sender"] = sender; root["params"]["message"] = message; if (!data.isNull()) data.toJsonValue(root["params"]["data"]); StyledWriter writer; std::string str = writer.write(root); for (unsigned int i = 0; i < m_connections.size(); i++) { { CSingleLock lock (m_connections[i].m_critSection); if ((m_connections[i].GetAnnouncementFlags() & flag) == 0) continue; } unsigned int sent = 0; do { CSingleLock lock (m_connections[i].m_critSection); sent += send(m_connections[i].m_socket, str.c_str(), str.size() - sent, sent); } while (sent < str.size()); } }
CStdString CJSONRPC::MethodCall(const CStdString &inputString, ITransportLayer *transport, IClient *client) { Value inputroot, outputroot, result; JSON_STATUS errorCode = OK; Reader reader; if (reader.parse(inputString, inputroot) && IsProperJSONRPC(inputroot)) { CStdString method = inputroot.get("method", "").asString(); method = method.ToLower(); errorCode = InternalMethodCall(method, inputroot, result, transport, client); } else { CLog::Log(LOGERROR, "JSONRPC: Failed to parse '%s'\n", inputString.c_str()); errorCode = ParseError; } outputroot["jsonrpc"] = "2.0"; outputroot["id"] = inputroot.get("id", 0); switch (errorCode) { case OK: outputroot["result"] = result; break; case ACK: outputroot["result"] = "OK"; break; case InvalidParams: outputroot["error"]["code"] = InvalidParams; outputroot["error"]["message"] = "Invalid params."; break; case MethodNotFound: outputroot["error"]["code"] = MethodNotFound; outputroot["error"]["message"] = "Method not found."; break; case ParseError: outputroot["error"]["code"] = ParseError; outputroot["error"]["message"] = "Parse error."; break; case BadPermission: outputroot["error"]["code"] = BadPermission; outputroot["error"]["message"] = "Bad client permission."; break; case FailedToExecute: outputroot["error"]["code"] = FailedToExecute; outputroot["error"]["message"] = "Failed to execute method."; break; default: outputroot["error"]["code"] = InternalError; outputroot["error"]["message"] = "Internal error."; break; } StyledWriter writer; CStdString str = writer.write(outputroot); return str; }
TEST_F(JsonCpp, StyledWriter) { for (int i = 0; i < kTrialCount; i++) { StyledWriter writer; std::string str = writer.write(root_); //if (i == 0) // std::cout << str.length() << std::endl; } }
measurements benchmark_jsoncpp(const char *input_filename, const char* output_filename) { size_t start_memory_used; size_t end_memory_used; size_t time_to_read; size_t time_to_write; { start_memory_used = memory_measurer::virtual_memory_currently_used_by_current_process(); Value root; { try { auto start = high_resolution_clock::now(); std::ifstream is(input_filename); is >> root; //Reader reader; //if (!reader.parse(input_filename, root)) //{ // std::cerr << "jsoncpp failed." << std::endl; //} auto end = high_resolution_clock::now(); time_to_read = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); } catch (const std::exception& e) { std::cout << e.what() << std::endl; exit(1); } } end_memory_used = memory_measurer::virtual_memory_currently_used_by_current_process(); { char writeBuffer[65536]; std::ofstream os; //(output_filename/*,std::ofstream::binary*/); os.rdbuf()->pubsetbuf(writeBuffer, sizeof(writeBuffer)); os.open(output_filename, std::ios_base::out | std::ios_base::binary); auto start = high_resolution_clock::now(); //os << root; StyledWriter styledWriter; //std::ofstream writer(filename, std::ifstream::binary); os << styledWriter.write(root); auto end = high_resolution_clock::now(); time_to_write = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); } } size_t final_memory_used = memory_measurer::virtual_memory_currently_used_by_current_process(); measurements results; results.library_name = library_name; results.memory_used = (end_memory_used - start_memory_used)/1000000; results.time_to_read = time_to_read; results.time_to_write = time_to_write; return results; }
//-------------------------------------------------------------- string JSONElement::getRawString(bool pretty) { string raw; if(pretty) { StyledWriter writer; raw = writer.write(*this); } else { FastWriter writer; raw = writer.write(*this); } return raw; }
//-------------------------------------------------------------- bool JSONElement::save(string filename, bool pretty) { ofstream file_key(filename.c_str()); if (!file_key.is_open()) { cout << "Unable to open " << filename << endl; return false; } if(pretty) { StyledWriter writer; file_key << writer.write( *this ) << endl; } else { FastWriter writer; file_key << writer.write( *this ) << endl; } file_key.close(); return true; }
//-------------------------------------------------------------- bool ofxJSONElement::save(string filename, bool pretty) { filename = ofToDataPath(filename, true); ofstream file_key(filename.c_str()); if (!file_key.is_open()) { ofLog(OF_LOG_WARNING, "Unable to open "+filename); return false; } if(pretty) { StyledWriter writer; file_key << writer.write( *this ) << endl; } else { FastWriter writer; file_key << writer.write( *this ) << endl; } ofLog(OF_LOG_NOTICE, "JSON saved to "+filename); file_key.close(); return true; }
std::string Value::toStyledString() const { StyledWriter writer; return writer.write( *this ); }
JSONCPP_STRING Value::toStyledString() const { StyledWriter writer; return writer.write(*this); }
void JSONResponse::sendJSON(struct mg_connection *conn, const Value& value) { StyledWriter writer; //todo(njoubert): Replace with FastWriter for production string out = writer.write(value); sendHTTPResponse(conn, out.c_str(), out.length()); }