Skip to content

Commit

Permalink
Add integer support.
Browse files Browse the repository at this point in the history
  • Loading branch information
benlaurie committed Nov 26, 2015
1 parent f32bfa7 commit 88a320a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 10 additions & 2 deletions objecthash.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ static bool object_hash_dict(/*const*/ json_object * const d, hash h) {
return true;
}

static bool object_hash_int(int64_t i, hash h) {
char buf[100];

sprintf(buf, "%ld", i);
hash_bytes('i', (byte *)buf, strlen(buf), h);
return true;
}

static void float_normalize(double f, char out[1000]) {
const char * const base = out;

Expand Down Expand Up @@ -175,7 +183,7 @@ bool object_hash(/*const*/ json_object *j, byte hash[HASH_SIZE]) {
case json_type_double:
return object_hash_float(json_object_get_double(j), hash);
case json_type_int:
assert(false);
return object_hash_int(json_object_get_int64(j), hash);
case json_type_string: {
const char *s = json_object_get_string(j);
return object_hash_str(s, strlen(s), hash);
Expand All @@ -194,7 +202,7 @@ bool object_hash(/*const*/ json_object *j, byte hash[HASH_SIZE]) {
assert(false);
}

bool common_json_hash(const char * const json, hash hash) {
bool python_json_hash(const char * const json, hash hash) {
json_object * const j = json_tokener_parse(json);
return object_hash(j, hash);
}
4 changes: 3 additions & 1 deletion objecthash_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void check_hash(const hash h1, const hash h2) {

static void run_test(const char * const json, const char * const h) {
hash r;
common_json_hash(json, r);
python_json_hash(json, r);

hash e;
a_to_hash(h, e);
Expand All @@ -40,6 +40,8 @@ int main(int argc, char **argv) {
"32ae896c413cfdc79eec68be9139c86ded8b279238467c216cf2bec4d5f1e4a2");
run_test("[\"foo\", {\"bar\": [\"baz\", null, 1.0, 1.5, 0.0001, 1000.0, 2.0, -23.1234, 2.0]}]",
"783a423b094307bcb28d005bc2f026ff44204442ef3513585e7e73b66e3c2213");
run_test("[\"foo\", {\"bar\": [\"baz\", null, 1, 1.5, 0.0001, 1000, 2, -23.1234, 2]}]",
"726e7ae9e3fadf8a2228bf33e505a63df8db1638fa4f21429673d387dbd1c52a");

puts("all tests passed");
return 0;
Expand Down

0 comments on commit 88a320a

Please sign in to comment.