Skip to content

Commit

Permalink
Added null json value.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-eremin committed Oct 22, 2015
1 parent 571b313 commit 5571f91
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 33 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ jfes_value_t *jfes_create_object_value(jfes_config_t *config);
jfes_value_t *jfes_get_child(jfes_value_t *value, const char *key, jfes_size_t key_length);
jfes_object_map_t *jfes_get_mapped_child(jfes_value_t *value, const char *key, jfes_size_t key_length);
jfes_status_t jfes_add_to_array(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item);
jfes_status_t jfes_add_to_array_at(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, jfes_size_t place_at);
jfes_status_t jfes_place_to_array(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item);
jfes_status_t jfes_place_to_array_at(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, jfes_size_t place_at);
jfes_status_t jfes_remove_from_array(jfes_config_t *config, jfes_value_t *value, jfes_size_t index);
jfes_status_t jfes_set_object_child(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, const char *key, jfes_size_t key_length);
jfes_status_t jfes_remove_object_child(jfes_config_t *config, jfes_value_t *value, const char *key, jfes_size_t key_length);
jfes_status_t jfes_set_object_property(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, const char *key, jfes_size_t key_length);
jfes_status_t jfes_remove_object_property(jfes_config_t *config, jfes_value_t *value, const char *key, jfes_size_t key_length);
```

### Serializing to json string
Expand Down
2 changes: 1 addition & 1 deletion examples/example_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int example_1_entry(int argc, char **argv) {
long buffer_size = 2048;
char *json_data = malloc(buffer_size * sizeof(char));

if (!get_file_content("samples/example_1.json", json_data, &buffer_size)) {
if (!get_file_content("json/example_1.json", json_data, &buffer_size)) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/example_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int example_2_entry(int argc, char **argv) {
long buffer_size = 2048;
char *json_data = malloc(buffer_size * sizeof(char));

if (!get_file_content("samples/example_2.json", json_data, &buffer_size)) {
if (!get_file_content("json/example_2.json", json_data, &buffer_size)) {
return -1;
}

Expand Down
23 changes: 16 additions & 7 deletions examples/example_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int example_3_entry(int argc, char **argv) {
long buffer_size = 2048;
char *json_data = malloc(buffer_size * sizeof(char));

if (!get_file_content("samples/example_3.json", json_data, &buffer_size)) {
if (!get_file_content("json/example_3.json", json_data, &buffer_size)) {
return -1;
}

Expand All @@ -32,16 +32,25 @@ int example_3_entry(int argc, char **argv) {
if (children) {
jfes_value_t *child = jfes_create_object_value(&config);

jfes_set_object_child(&config, child, jfes_create_string_value(&config, "Paul", 0), "first_name", 0);
jfes_set_object_child(&config, child, jfes_create_string_value(&config, "Smith", 0), "middle_name", 0);
jfes_set_object_child(&config, child, jfes_create_integer_value(&config, 1), "age", 0);
/*
Below we create a property with a key "first_name" and json value "Paul".
We place this property into the `child` variable.
*/
jfes_set_object_property(&config, child, jfes_create_string_value(&config, "Paul", 0), "first_name", 0);

/* The same thing with "middle_name" and "age". */
jfes_set_object_property(&config, child, jfes_create_string_value(&config, "Smith", 0), "middle_name", 0);
jfes_set_object_property(&config, child, jfes_create_integer_value(&config, 1), "age", 0);

/* And now we wants to rewrite age value with 2. */
jfes_set_object_child(&config, child, jfes_create_integer_value(&config, 2), "age", 0);
jfes_set_object_property(&config, child, jfes_create_integer_value(&config, 2), "age", 0);

jfes_remove_object_child(&config, child, "middle_name", 0);
jfes_remove_object_property(&config, child, "middle_name", 0);

status = jfes_add_to_array_at(&config, children, child, 1);
/* Place value `child` in `children` json array at index 1. */
status = jfes_place_to_array_at(&config, children, child, 1);

jfes_set_object_property(&config, &value, jfes_create_null_value(&config), "null_property", 0);

/* And now we dumps out new object to the memory. */
char beauty_dump[1024];
Expand Down
49 changes: 42 additions & 7 deletions jfes.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,28 @@ static jfes_size_t jfes_strlen(const char *data) {
}

/**
Analyzes input string on the subject of whether it is an boolean.
Analyzes input string on the subject of whether it is null.
\param[in] data Input string.
\param[in] length Length if the input string.
\return Zero, if input string not an boolean. Otherwise anything.
\return Zero, if input string not null. Otherwise anything.
*/
static int jfes_is_null(const char *data, jfes_size_t length) {
if (!data || length != 4) {
return 0;
}

return jfes_memcmp(data, "null", 4) == 0;
}

/**
Analyzes input string on the subject of whether it is boolean.
\param[in] data Input string.
\param[in] length Length if the input string.
\return Zero, if input string not boolean. Otherwise anything.
*/
static int jfes_is_boolean(const char *data, jfes_size_t length) {
if (!data || length < 4) {
Expand Down Expand Up @@ -536,6 +552,9 @@ static jfes_token_type_t jfes_get_token_type(const char *data, jfes_size_t lengt
}

jfes_token_type_t type = jfes_undefined;
if (jfes_is_null(data, length)) {
return jfes_null;
}
if (jfes_is_boolean(data, length)) {
return jfes_boolean;
}
Expand Down Expand Up @@ -885,6 +904,9 @@ jfes_status_t jfes_create_node(jfes_tokens_data_t *tokens_data, jfes_value_t *va
value->type = (jfes_value_type_t)token->type;

switch (token->type) {
case jfes_null:
break;

case jfes_boolean:
value->data.bool_val = jfes_string_to_boolean(tokens_data->json_data + token->start,
token->end - token->start);
Expand Down Expand Up @@ -1059,6 +1081,17 @@ jfes_status_t jfes_free_value(jfes_config_t *config, jfes_value_t *value) {
return jfes_success;
}

jfes_value_t *jfes_create_null_value(jfes_config_t *config) {
if (!config) {
return JFES_NULL;
}

jfes_value_t *result = (jfes_value_t*)config->jfes_malloc(sizeof(jfes_value_t));
result->type = jfes_null;

return result;
}

jfes_value_t *jfes_create_boolean_value(jfes_config_t *config, int value) {
if (!config) {
return JFES_NULL;
Expand Down Expand Up @@ -1177,15 +1210,15 @@ jfes_object_map_t *jfes_get_mapped_child(jfes_value_t *value, const char *key, j
return JFES_NULL;
}

jfes_status_t jfes_add_to_array(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item) {
jfes_status_t jfes_place_to_array(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item) {
if (!config || !value || !item || value->type != jfes_array) {
return jfes_invalid_arguments;
}

return jfes_add_to_array_at(config, value, item, value->data.array_val->count);
return jfes_place_to_array_at(config, value, item, value->data.array_val->count);
}

jfes_status_t jfes_add_to_array_at(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, jfes_size_t place_at) {
jfes_status_t jfes_place_to_array_at(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, jfes_size_t place_at) {
if (!config || !value || !item || value->type != jfes_array) {
return jfes_invalid_arguments;
}
Expand Down Expand Up @@ -1234,7 +1267,7 @@ jfes_status_t jfes_remove_from_array(jfes_config_t *config, jfes_value_t *value,
return jfes_success;
}

jfes_status_t jfes_set_object_child(jfes_config_t *config, jfes_value_t *value,
jfes_status_t jfes_set_object_property(jfes_config_t *config, jfes_value_t *value,
jfes_value_t *item, const char *key, jfes_size_t key_length) {
if (!config || !value || !item || !key || value->type != jfes_object) {
return jfes_invalid_arguments;
Expand Down Expand Up @@ -1275,7 +1308,7 @@ jfes_status_t jfes_set_object_child(jfes_config_t *config, jfes_value_t *value,
return jfes_success;
}

jfes_status_t jfes_remove_object_child(jfes_config_t *config, jfes_value_t *value,
jfes_status_t jfes_remove_object_property(jfes_config_t *config, jfes_value_t *value,
const char *key, jfes_size_t key_length) {
if (!config || !value || value->type != jfes_object || !key) {
return jfes_invalid_arguments;
Expand Down Expand Up @@ -1477,6 +1510,8 @@ jfes_status_t jfes_value_to_stream_helper(jfes_value_t *value, jfes_stringstream
}

switch (value->type) {
case jfes_null:
return jfes_add_to_stringstream(stream, "null", 0);
case jfes_boolean:
return jfes_add_to_stringstream(stream, jfes_boolean_to_string(value->data.bool_val), 0);

Expand Down
31 changes: 21 additions & 10 deletions jfes.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ typedef struct jfes_string {
typedef enum jfes_token_type {
jfes_undefined = 0x00, /**< Undefined token type. */

jfes_boolean = 0x01, /**< Boolean token type. */
jfes_integer = 0x02, /**< Integer token type. */
jfes_double = 0x03, /**< Double token type. */
jfes_string = 0x04, /**< String token type. */
jfes_null = 0x01, /**< Null token type. */

jfes_array = 0x05, /**< Array token type. */
jfes_object = 0x06 /**< Object token type. */
jfes_boolean = 0x02, /**< Boolean token type. */
jfes_integer = 0x03, /**< Integer token type. */
jfes_double = 0x04, /**< Double token type. */
jfes_string = 0x05, /**< String token type. */

jfes_array = 0x06, /**< Array token type. */
jfes_object = 0x07, /**< Object token type. */
} jfes_token_type_t;

/** Json value type is the same as token type. */
Expand Down Expand Up @@ -216,6 +218,15 @@ jfes_status_t jfes_parse_to_value(jfes_config_t *config, const char *json,
*/
jfes_status_t jfes_free_value(jfes_config_t *config, jfes_value_t *value);

/**
Allocates new null value.
\param[in] config JFES configuration.
\return Allocated JFES value or JFES_NULL, if something went wrong.
*/
jfes_value_t *jfes_create_null_value(jfes_config_t *config);

/**
Allocates new boolean value.
Expand Down Expand Up @@ -310,7 +321,7 @@ jfes_object_map_t *jfes_get_mapped_child(jfes_value_t *value, const char *key, j
\return jfes_success if everything is OK.
*/
jfes_status_t jfes_add_to_array(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item);
jfes_status_t jfes_place_to_array(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item);

/**
Adds new item to the given array value on the given place.
Expand All @@ -322,7 +333,7 @@ jfes_status_t jfes_add_to_array(jfes_config_t *config, jfes_value_t *value, jfes
\return jfes_success if everything is OK.
*/
jfes_status_t jfes_add_to_array_at(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, jfes_size_t place_at);
jfes_status_t jfes_place_to_array_at(jfes_config_t *config, jfes_value_t *value, jfes_value_t *item, jfes_size_t place_at);

/**
Removes an item with fiven index from array.
Expand All @@ -347,7 +358,7 @@ jfes_status_t jfes_remove_from_array(jfes_config_t *config, jfes_value_t *value,
\return jfes_success if everything is OK.
*/
jfes_status_t jfes_set_object_child(jfes_config_t *config, jfes_value_t *value,
jfes_status_t jfes_set_object_property(jfes_config_t *config, jfes_value_t *value,
jfes_value_t *item, const char *key, jfes_size_t key_length);

/**
Expand All @@ -361,7 +372,7 @@ jfes_status_t jfes_set_object_child(jfes_config_t *config, jfes_value_t *value,
\return jfes_success if everything is OK.
*/
jfes_status_t jfes_remove_object_child(jfes_config_t *config, jfes_value_t *value,
jfes_status_t jfes_remove_object_property(jfes_config_t *config, jfes_value_t *value,
const char *key, jfes_size_t key_length);

/**
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions samples/example_3.json → json/example_3.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"first_name": "John",
"last_name": "Black",
"first_name": "John",
"last_name": "Black",
"age": 35,
"children": [
{ "first_name": "Alice", "age": 5 },
{ "first_name": "Robert", "age": 8 },
],

"wife": null,

"simple" : [ 12, 15, 76, 34, 75, "Test", 23.1, 65.3, false, true, false ]
}
}

0 comments on commit 5571f91

Please sign in to comment.