ext/common/Utils/JsonUtils.h in passenger-5.0.0.beta2 vs ext/common/Utils/JsonUtils.h in passenger-5.0.0.beta3
- old
+ new
@@ -1,8 +1,8 @@
/*
* Phusion Passenger - https://www.phusionpassenger.com/
- * Copyright (c) 2014 Phusion
+ * Copyright (c) 2014-2015 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,10 +24,12 @@
*/
#ifndef _PASSENGER_UTILS_JSON_UTILS_H_
#define _PASSENGER_UTILS_JSON_UTILS_H_
#include <string>
+#include <cstdio>
+#include <cstddef>
#include <StaticString.h>
#include <Utils/json.h>
#include <Utils/StrIntUtils.h>
namespace Passenger {
@@ -64,9 +66,45 @@
buf[len - 1] = '\0';
}
doc["local"] = buf;
doc["relative"] = distanceOfTimeInWords(time) + " ago";
+ return doc;
+}
+
+inline string
+formatFloat(double val) {
+ char buf[64];
+ int size = snprintf(buf, sizeof(buf), "%.1f", val);
+ return string(buf, size);
+}
+
+inline Json::Value
+byteSizeToJson(size_t size) {
+ Json::Value doc;
+ doc["bytes"] = (Json::UInt64) size;
+ if (size < 1024) {
+ doc["human_readable"] = toString(size) + " bytes";
+ } else if (size < 1024 * 1024) {
+ doc["human_readable"] = formatFloat(size / 1024.0) + " KB";
+ } else {
+ doc["human_readable"] = formatFloat(size / 1024.0 / 1024.0) + " MB";
+ }
+ return doc;
+}
+
+inline Json::Value
+signedByteSizeToJson(long long size) {
+ Json::Value doc;
+ long long absSize = (size < 0) ? -size : size;
+ doc["bytes"] = (Json::Int64) size;
+ if (absSize < 1024) {
+ doc["human_readable"] = toString(size) + " bytes";
+ } else if (absSize < 1024 * 1024) {
+ doc["human_readable"] = formatFloat(size / 1024.0) + " KB";
+ } else {
+ doc["human_readable"] = formatFloat(size / 1024.0 / 1024.0) + " MB";
+ }
return doc;
}
} // namespace Passenger