Esempio n. 1
0
static int json_write_minified_get_object_size(const struct json_object_s* object, size_t* size) {
  struct json_object_element_s* element;

  *size += 2; // '{' and '}'

  *size += object->length; // ':'s seperate each name/value pair

  if (1 < object->length) {
    *size += object->length - 1; // ','s seperate each element
  }

  for (element = object->start; 0 != element; element = element->next) {
    if (json_write_minified_get_string_size(element->name, size)) {
      // string was malformed!
      return 1;
    }

    if (json_write_minified_get_value_size(element->value, size)) {
      // value was malformed!
      return 1;
    }
  }

  return 0;
}
Esempio n. 2
0
static int json_write_minified_get_value_size(const struct json_value_s *value,
                                              size_t *size) {
  switch (value->type) {
  default:
    // unknown value type found!
    return 1;
  case json_type_number:
    return json_write_minified_get_number_size(
        (struct json_number_s *)value->payload, size);
  case json_type_string:
    return json_write_minified_get_string_size(
        (struct json_string_s *)value->payload, size);
  case json_type_array:
    return json_write_minified_get_array_size(
        (struct json_array_s *)value->payload, size);
  case json_type_object:
    return json_write_minified_get_object_size(
        (struct json_object_s *)value->payload, size);
  case json_type_true:
    *size += 4; // the string "true"
    return 0;
  case json_type_false:
    *size += 5; // the string "false"
    return 0;
  case json_type_null:
    *size += 4; // the string "null"
    return 0;
  }
}