Commit beb2017dc82d3aac80d858d81f0997a5b77e6b82

Authored by Georg Hopp
1 parent e7ed789d

dyntype seems to work for int now, incl. json

@@ -30,8 +30,8 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json) @@ -30,8 +30,8 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json)
30 switch (json_object_get_type(json)) { 30 switch (json_object_get_type(json)) {
31 case json_type_int: 31 case json_type_int:
32 _this->type = BIGPOINT_DYNTYPE_INT; 32 _this->type = BIGPOINT_DYNTYPE_INT;
33 - _this->size = sizeof(long);  
34 - (_this->data)._int = (long)json_object_get_int(json); 33 + _this->size = sizeof(int);
  34 + (_this->data)._int = (int)json_object_get_int(json);
35 break; 35 break;
36 36
37 default: 37 default:
@@ -56,10 +56,15 @@ __toJson(struct BIGPOINT_DYNTYPE * _this) @@ -56,10 +56,15 @@ __toJson(struct BIGPOINT_DYNTYPE * _this)
56 { 56 {
57 struct json_object * json = NULL; 57 struct json_object * json = NULL;
58 58
59 - /**  
60 - * @TODO: make a smart implementation here base on the type of the  
61 - * actual object.  
62 - */ 59 + switch(_this->type) {
  60 + case BIGPOINT_DYNTYPE_INT:
  61 + json = json_object_new_int((_this->data)._int);
  62 + break;
  63 +
  64 + default:
  65 + json = NULL;
  66 + }
  67 +
63 return json; 68 return json;
64 } 69 }
65 70
@@ -23,7 +23,7 @@ struct BIGPOINT_DYNTYPE { @@ -23,7 +23,7 @@ struct BIGPOINT_DYNTYPE {
23 size_t size; 23 size_t size;
24 union _data { 24 union _data {
25 unsigned char _boolean; 25 unsigned char _boolean;
26 - long _int; 26 + int _int;
27 double _float; 27 double _float;
28 char * _string; 28 char * _string;
29 void * _object; 29 void * _object;
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <json/json.h>
  4 +
  5 +#include "bigpoint_cclass.h"
  6 +#include "bigpoint_dyntype.h"
  7 +
  8 +
  9 +int
  10 +main(int argc, char * argv[])
  11 +{
  12 + struct json_object * json = json_object_new_int(123);
  13 +
  14 + struct BIGPOINT_DYNTYPE * dyn = newFromJson(BIGPOINT_DYNTYPE, json);
  15 +
  16 + printf("%d\n", (dyn->data)._int);
  17 +
  18 + delete(dyn);
  19 + json_object_put(json);
  20 +
  21 + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), 321);
  22 + json = toJson(dyn);
  23 +
  24 + printf("%s\n", json_object_to_json_string(json));
  25 +
  26 + delete(dyn);
  27 + json_object_put(json);
  28 +
  29 + return 0;
  30 +}
  31 +
  32 +// vim: set et ts=4 sw=4:
Please register or login to post a comment