summaryrefslogtreecommitdiff
path: root/lib/json.c
blob: 5be616ba2505cffff01043dafedd663c136ff89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * json.c
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "json.h"

#ifdef USEJSON

const char* charset = "utf-8";  // #TODO, move to configuration?

//***************************************************************************
// Copy JSON Object to Data Buffer
//***************************************************************************

int json2Data(json_t* obj, MemoryStruct* data, const char* encoding)
{
   int status = success;

   // will be freed by data's dtor

   data->memory = json_dumps(obj, JSON_PRESERVE_ORDER);
   data->size = strlen(data->memory);
   sprintf(data->contentType, "application/json; charset=%s", charset);

   // gzip content if supported by browser ...

   if (data->size && encoding && strstr(encoding, "gzip"))
      status = data->toGzip();

   return status;
}

//***************************************************************************
// Add field to json object
//***************************************************************************

int addFieldToJson(json_t* obj, cDbValue* value, int ignoreEmpty, const char* extName)
{
   char* name;

   if (!value || !value->getField())
      return fail;

   name = strdup(extName ? extName : value->getField()->getName());
   toCase(cLower, name);   // use always lower case names in json

   switch (value->getField()->getFormat())
   {
      case cDBS::ffAscii:
      case cDBS::ffText:
      case cDBS::ffMText:
         if (!ignoreEmpty || !isEmpty(value->getStrValue()))
            json_object_set_new(obj, name, json_string(value->getStrValue()));
         break;

      case cDBS::ffInt:
      case cDBS::ffUInt:
         json_object_set_new(obj, name, json_integer(value->getIntValue()));
         break;

      case cDBS::ffFloat:
         json_object_set_new(obj, name, json_real(value->getFloatValue()));
         break;

      default:
         break;
   }

   free(name);

   return success;
}

//***************************************************************************
// Add field to json object
//***************************************************************************

int addFieldToJson(json_t* obj, cDbTable* table, const char* fname,
                   int ignoreEmpty, const char* extName)
{
   return addFieldToJson(obj, table->getValue(fname), ignoreEmpty, extName);
}

//***************************************************************************
// Get Field From Json
//   - if a default is required put it into the row
//     before calling this function
//***************************************************************************

int getFieldFromJson(json_t* obj, cDbRow* row, const char* fname, const char* extName)
{
   cDbValue* value = row->getValue(fname);
   char* jname;

   if (!value)
      return fail;

   jname = strdup(!isEmpty(extName) ? extName : value->getField()->getName());
   toCase(cLower, jname);   // use always lower case names in json

   switch (value->getField()->getFormat())
   {
      case cDBS::ffAscii:
      case cDBS::ffText:
      case cDBS::ffMText:
      {
         const char* v = getStringFromJson(obj, jname, "");
         if (!isEmpty(v) || !value->isEmpty())
            value->setValue(v);
         break;
      }

      case cDBS::ffInt:
      case cDBS::ffUInt:
      {
         int v = getIntFromJson(obj, jname, 0);
         const char* s = getStringFromJson(obj, jname, "_not_set_");

         if (s && strcmp(s, "_not_set_") == 0)
            break;

         if (s && strcmp(s, "null") == 0)
            value->setNull();
         else
            value->setValue(v);

         break;
      }

//    case cDBS::ffFloat:    #TODO to be implemented
//    {
//       double v = getFloatFromJson(obj, jname, na);
//       if (v != na) value->setValue(v);
//       break;
//    }

      default:
         break;
   }

   return success;
}

//***************************************************************************
// Get Elements
//***************************************************************************

const char* getStringFromJson(json_t* obj, const char* name, const char* def)
{
   json_t* o = json_object_get(obj, name);

   if (!o)
      return def;

   return json_string_value(o);
}

int getIntFromJson(json_t* obj, const char* name, int def)
{
   json_t* o = json_object_get(obj, name);

   if (!o)
      return def;

   return json_integer_value(o);
}

//***************************************************************************
#endif // USEJSON