Exemplo n.º 1
0
// Render an object to text.
int
aJsonClass::printObject(aJsonObject *item, FILE* stream)
{
  if (item == NULL)
    {
      //nothing to do
      return 0;
    }
  aJsonObject *child = item->child;
  if (fputc('{', stream) == EOF)
    {
      return EOF;
    }
  while (child)
    {
      if (printStringPtr(child->name, stream) == EOF)
        {
          return EOF;
        }
      if (fputc(':', stream) == EOF)
        {
          return EOF;
        }
      if (printValue(child, stream) == EOF)
        {
          return EOF;
        }
      child = child->next;
      if (child)
        {
          if (fputc(',', stream) == EOF)
            {
              return EOF;
            }
        }
    }
  if (fputc('}', stream) == EOF)
    {
      return EOF;
    }
  return 0;
}
Exemplo n.º 2
0
// Render an object to text.
char*
aJsonClass::printObject(aJsonObject *item)
{
  char **entries = 0, **names = 0;
  char *out = 0, *ptr, *ret, *str;
  int len = 7;
  unsigned char i = 0;
  aJsonObject *child = item->child;
  unsigned char numentries = 0, fail = 0;
  // Count the number of entries.
  while (child)
    numentries++, child = child->next;
  // Allocate space for the names and the objects
  entries = (char**) malloc(numentries * sizeof(char*));
  if (!entries)
    return 0;
  names = (char**) malloc(numentries * sizeof(char*));
  if (!names)
    {
      free(entries);
      return 0;
    }
  memset(entries, 0, sizeof(char*) * numentries);
  memset(names, 0, sizeof(char*) * numentries);

  // Collect all the results into our arrays:
  child = item->child;
  while (child)
    {
      names[i] = str = printStringPtr(child->name);
      entries[i++] = ret = printValue(child);
      if (str && ret)
        len += strlen(ret) + strlen(str) + 2;
      else
        fail = 1;
      child = child->next;
    }

  // Try to allocate the output string
  if (!fail)
    out = (char*) malloc(len);
  if (!out)
    fail = 1;

  // Handle failure
  if (fail)
    {
      for (i = 0; i < numentries; i++)
        {
          if (names[i])
            free(names[i]);
          if (entries[i])
            free(entries[i]);
        }
      free(names);
      free(entries);
      return 0;
    }

  // Compose the output:
  *out = '{';
  ptr = out + 1;
  *ptr = 0;
  for (i = 0; i < numentries; i++)
    {
      strcpy(ptr, names[i]);
      ptr += strlen(names[i]);
      *ptr++ = ':';
      strcpy(ptr, entries[i]);
      ptr += strlen(entries[i]);
      if (i != numentries - 1)
        *ptr++ = ',';
      *ptr = 0;
      free(names[i]);
      free(entries[i]);
    }

  free(names);
  free(entries);
  *ptr++ = '}';
  *ptr++ = 0;
  return out;
}
Exemplo n.º 3
0
// Invote print_string_ptr (which is useful) on an item.
int
aJsonClass::printString(aJsonObject *item, FILE* stream)
{
  return printStringPtr(item->valuestring, stream);
}
Exemplo n.º 4
0
// Invote print_string_ptr (which is useful) on an item.
char*
aJsonClass::printString(aJsonObject *item)
{
  return printStringPtr(item->value.valuestring);
}