Add support for JSON Booleans
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/f0abe457 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/f0abe457 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/f0abe457 Branch: refs/heads/master Commit: f0abe457859b1b19825fe6f0dc32899444249fdc Parents: c851ebb Author: Nick Wellnhofer <[email protected]> Authored: Sat Mar 12 18:29:39 2016 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Fri Jul 15 22:09:57 2016 +0200 ---------------------------------------------------------------------- compiler/src/CFCJson.c | 42 ++++++++++++++++++++++++++++++++++++++++++ compiler/src/CFCJson.h | 4 ++++ 2 files changed, 46 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/f0abe457/compiler/src/CFCJson.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCJson.c b/compiler/src/CFCJson.c index b186492..262b943 100644 --- a/compiler/src/CFCJson.c +++ b/compiler/src/CFCJson.c @@ -23,6 +23,7 @@ struct CFCJson { int type; char *string; + int bool_val; struct CFCJson **kids; size_t num_kids; }; @@ -36,6 +37,9 @@ S_parse_json_string(const char **json); static CFCJson* S_parse_json_null(const char **json); +static CFCJson* +S_parse_json_bool(const char **json); + static void S_skip_whitespace(const char **json); @@ -115,6 +119,9 @@ S_parse_json_hash(const char **json) { else if (*text == 'n') { value = S_parse_json_null(&text); } + else if (*text == 't' || *text == 'f') { + value = S_parse_json_bool(&text); + } if (!value) { CFCJson_destroy(node); return NULL; @@ -186,6 +193,33 @@ S_parse_json_null(const char **json) { return node; } +// Parse a JSON Boolean. +static CFCJson* +S_parse_json_bool(const char **json) { + static const char true_str[] = "true"; + static const char false_str[] = "false"; + + int val; + + if (strncmp(*json, true_str, sizeof(true_str) - 1) == 0) { + val = 1; + *json += sizeof(true_str) - 1; + } + else if (strncmp(*json, false_str, sizeof(false_str) - 1) == 0) { + val = 0; + *json += sizeof(false_str) - 1; + } + else { + return NULL; + } + + CFCJson *node = (CFCJson*)CALLOCATE(1, sizeof(CFCJson)); + node->type = CFCJSON_BOOL; + node->bool_val = val; + + return node; +} + static void S_skip_whitespace(const char **json) { while (CFCUtil_isspace(json[0][0])) { *json = *json + 1; } @@ -219,6 +253,14 @@ CFCJson_get_string(CFCJson *self) { return self->string; } +int +CFCJson_get_bool(CFCJson *self) { + if (self->type != CFCJSON_BOOL) { + CFCUtil_die("Not a JSON Boolean"); + } + return self->bool_val; +} + size_t CFCJson_get_num_children(CFCJson *self) { if (self->type != CFCJSON_HASH) { http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/f0abe457/compiler/src/CFCJson.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCJson.h b/compiler/src/CFCJson.h index 8d75a90..97d7081 100644 --- a/compiler/src/CFCJson.h +++ b/compiler/src/CFCJson.h @@ -24,6 +24,7 @@ extern "C" { #define CFCJSON_STRING 1 #define CFCJSON_HASH 2 #define CFCJSON_NULL 3 +#define CFCJSON_BOOL 4 typedef struct CFCJson CFCJson; @@ -39,6 +40,9 @@ CFCJson_get_type(CFCJson *self); const char* CFCJson_get_string(CFCJson *self); +int +CFCJson_get_bool(CFCJson *self); + size_t CFCJson_get_num_children(CFCJson *self);
