aboutsummaryrefslogtreecommitdiff
path: root/src/Utility/JsonHelper.c
blob: 1eb40985d4733ca6b97c4bcad7cd0334e4bb5730 (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
#include "JsonHelper.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int is_whitespace_json(char c) {
  return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

int json_parse_float_map(const char *json, const char *target_key,
                         JsonFloatMap *result) {
  memset(result, 0, sizeof(JsonFloatMap));

  if (!json || !target_key)
    return 0;

  const char *obj_start = strchr(json, '{');
  if (!obj_start)
    return 0;

  const char *rates_start = strstr(obj_start, target_key);
  if (!rates_start)
    return 0;

  rates_start = strchr(rates_start, '{');
  if (!rates_start)
    return 0;

  const char *p = rates_start + 1;
  while (*p && *p != '}') {
    while (*p && (*p == ' ' || *p == '\n' || *p == '\t' || *p == ','))
      p++;

    if (*p == '}')
      break;

    if (*p != '"')
      break;
    p++;

    char key[32] = {0};
    size_t key_len = 0;
    while (*p && *p != '"' && key_len < 31) {
      key[key_len++] = *p++;
    }
    if (*p != '"')
      break;
    p++;

    while (*p && *p != ':')
      p++;
    if (*p != ':')
      break;
    p++;

    while (*p && is_whitespace_json(*p))
      p++;

    double value = 0;
    int has_digit = 0;
    while (*p >= '0' && *p <= '9') {
      value = value * 10 + (*p - '0');
      has_digit = 1;
      p++;
    }
    if (*p == '.') {
      p++;
      double frac = 0.1;
      while (*p >= '0' && *p <= '9') {
        value += (*p - '0') * frac;
        frac *= 0.1;
        has_digit = 1;
        p++;
      }
    }

    if (has_digit && key_len > 0) {
      memcpy(result->keys[result->count], key, key_len);
      result->keys[result->count][key_len] = '\0';
      result->values[result->count] = value;
      result->count++;
      if (result->count >= 256)
        break;
    }
  }

  return result->count > 0;
}

double json_get_float(const char *json, const char *key) {
  if (!json || !key)
    return 0;

  const char *key_pos = strstr(json, key);
  if (!key_pos)
    return 0;

  const char *colon = strchr(key_pos, ':');
  if (!colon)
    return 0;

  colon++;
  while (*colon && is_whitespace_json(*colon))
    colon++;

  return strtod(colon, NULL);
}

char *json_get_string(const char *json, const char *key) {
  if (!json || !key)
    return NULL;

  static char buffer[256];

  const char *key_pos = strstr(json, key);
  if (!key_pos)
    return NULL;

  const char *colon = strchr(key_pos, ':');
  if (!colon)
    return NULL;

  colon++;
  while (*colon && is_whitespace_json(*colon))
    colon++;

  if (*colon != '"')
    return NULL;
  colon++;

  size_t len = 0;
  while (*colon && *colon != '"' && len < 255) {
    buffer[len++] = *colon++;
  }
  buffer[len] = '\0';

  return buffer;
}