Browse Source

dyntype seems to work for int now, incl. json

master
Georg Hopp 14 years ago
parent
commit
beb2017dc8
  1. 17
      bigpoint_dyntype.c
  2. 2
      bigpoint_dyntype.h
  3. 32
      test4.c

17
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;
}

2
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;

32
test4.c

@ -0,0 +1,32 @@
#include <stdio.h>
#include <string.h>
#include <json/json.h>
#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:
Loading…
Cancel
Save