diff --git a/bigpoint_dyntype.c b/bigpoint_dyntype.c index 7e4e190..05f2462 100644 --- a/bigpoint_dyntype.c +++ b/bigpoint_dyntype.c @@ -30,8 +30,8 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json) switch (json_object_get_type(json)) { case json_type_int: _this->type = BIGPOINT_DYNTYPE_INT; - _this->size = sizeof(long); - (_this->data)._int = (long)json_object_get_int(json); + _this->size = sizeof(int); + (_this->data)._int = (int)json_object_get_int(json); break; default: @@ -56,10 +56,15 @@ __toJson(struct BIGPOINT_DYNTYPE * _this) { struct json_object * json = NULL; - /** - * @TODO: make a smart implementation here base on the type of the - * actual object. - */ + switch(_this->type) { + case BIGPOINT_DYNTYPE_INT: + json = json_object_new_int((_this->data)._int); + break; + + default: + json = NULL; + } + return json; } diff --git a/bigpoint_dyntype.h b/bigpoint_dyntype.h index 18a016e..86fe3a4 100644 --- a/bigpoint_dyntype.h +++ b/bigpoint_dyntype.h @@ -23,7 +23,7 @@ struct BIGPOINT_DYNTYPE { size_t size; union _data { unsigned char _boolean; - long _int; + int _int; double _float; char * _string; void * _object; diff --git a/test4.c b/test4.c new file mode 100644 index 0000000..f4453ae --- /dev/null +++ b/test4.c @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "bigpoint_cclass.h" +#include "bigpoint_dyntype.h" + + +int +main(int argc, char * argv[]) +{ + struct json_object * json = json_object_new_int(123); + + struct BIGPOINT_DYNTYPE * dyn = newFromJson(BIGPOINT_DYNTYPE, json); + + printf("%d\n", (dyn->data)._int); + + delete(dyn); + json_object_put(json); + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), 321); + json = toJson(dyn); + + printf("%s\n", json_object_to_json_string(json)); + + delete(dyn); + json_object_put(json); + + return 0; +} + +// vim: set et ts=4 sw=4: