Merge pull request #172 from ceph/wip-ceph-json

Wip ceph json

Reviewed-by: Sage Weil <sage@inktank.com>
This commit is contained in:
Sage Weil 2013-03-29 08:37:04 -07:00
commit 999b307af5
2 changed files with 71 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <include/types.h>
#include <limits.h>
#include <errno.h>
#include "common/ceph_json.h"
@ -325,6 +326,62 @@ void decode_json_obj(unsigned long& val, JSONObj *obj)
}
}
void decode_json_obj(long long& val, JSONObj *obj)
{
string s = obj->get_data();
const char *start = s.c_str();
char *p;
errno = 0;
val = strtoll(start, &p, 10);
/* Check for various possible errors */
if ((errno == ERANGE && (val == LLONG_MAX || val == LLONG_MIN)) ||
(errno != 0 && val == 0)) {
throw JSONDecoder::err("failed to parse number");
}
if (p == start) {
throw JSONDecoder::err("failed to parse number");
}
while (*p != '\0') {
if (!isspace(*p)) {
throw JSONDecoder::err("failed to parse number");
}
p++;
}
}
void decode_json_obj(unsigned long long& val, JSONObj *obj)
{
string s = obj->get_data();
const char *start = s.c_str();
char *p;
errno = 0;
val = strtoull(start, &p, 10);
/* Check for various possible errors */
if ((errno == ERANGE && val == ULLONG_MAX) ||
(errno != 0 && val == 0)) {
throw JSONDecoder::err("failed to number");
}
if (p == start) {
throw JSONDecoder::err("failed to parse number");
}
while (*p != '\0') {
if (!isspace(*p)) {
throw JSONDecoder::err("failed to parse number");
}
p++;
}
}
void decode_json_obj(int& val, JSONObj *obj)
{
long l;
@ -397,6 +454,16 @@ void encode_json(const char *name, unsigned long val, Formatter *f)
f->dump_int(name, val);
}
void encode_json(const char *name, unsigned long long val, Formatter *f)
{
f->dump_int(name, val);
}
void encode_json(const char *name, long long val, Formatter *f)
{
f->dump_int(name, val);
}
void encode_json(const char *name, const utime_t& val, Formatter *f)
{
f->dump_stream(name) << val;

View File

@ -127,6 +127,8 @@ static inline void decode_json_obj(string& val, JSONObj *obj)
val = obj->get_data();
}
void decode_json_obj(unsigned long long& val, JSONObj *obj);
void decode_json_obj(long long& val, JSONObj *obj);
void decode_json_obj(unsigned long& val, JSONObj *obj);
void decode_json_obj(long& val, JSONObj *obj);
void decode_json_obj(unsigned& val, JSONObj *obj);
@ -252,6 +254,8 @@ void encode_json(const char *name, long val, Formatter *f);
void encode_json(const char *name, unsigned long val, Formatter *f);
void encode_json(const char *name, const utime_t& val, Formatter *f);
void encode_json(const char *name, const bufferlist& bl, Formatter *f);
void encode_json(const char *name, long long val, Formatter *f);
void encode_json(const char *name, long long unsigned val, Formatter *f);
template<class K, class V>
void encode_json_map(const char *name, const char *index_name,