Ejemplo n.º 1
0
static json_t *monster_toJson(const void *vm)
{
	json_t		*monRoot;
	monster_t	*m = (monster_t *)vm;
	uint32_t	i;

	monRoot = json_object();

	JSON_BTSTRING(monRoot, "singular",	m->singular);
	JSON_BTSTRING(monRoot, "plural",	m->plural);
	JSON_BTSTRING(monRoot, "picture",	m->picture);
	JSON_STRING(monRoot, "pronoun",		getPronounString(m->pronoun));
	JSON_NUMBER(monRoot, "hpBase",		m->hpBase);
	JSON_NUMBER(monRoot, "hpRndDie",	m->hpRndDie);
	JSON_NUMBER(monRoot, "hpRndNDice",	m->hpRndNdice);
	JSON_NUMBER(monRoot, "reward",		m->reward);
	JSON_NUMBER(monRoot, "ac",		m->baseAC);
	JSON_NUMBER(monRoot, "breathSaveLo",	m->breathSaveLo);
	JSON_NUMBER(monRoot, "breathSaveHi",	m->breathSaveHi);
	JSON_NUMBER(monRoot, "spellSaveLo",	m->spellSaveLo);
	JSON_NUMBER(monRoot, "spellSaveHi",	m->spellSaveHi);
	JSON_NUMBER(monRoot, "toHitLo",		m->toHitLo);
	JSON_NUMBER(monRoot, "toHitHi",		m->toHitHi);
	JSON_NUMBER(monRoot, "priorityLo",	m->priorityLo);
	JSON_NUMBER(monRoot, "priorityHi",	m->priorityHi);
	JSON_BOOL(monRoot, "rndGroupSize",	m->rndGroupSize);
	JSON_NUMBER(monRoot, "groupSize",	m->groupSize);
	JSON_NUMBER(monRoot, "numAttacks",	m->numAttacks);
	/* XXX PRONOUN Handling */
	JSON_BOOL(monRoot, "willAdvance",	m->willAdvance);
	JSON_NUMBER(monRoot, "distance",	m->distance);
	JSON_NUMBER(monRoot, "advanceSpeed",	m->advSpeed);
	JSON_BOOL(monRoot, "isIllusion",	m->isIllusion);

	json_object_update(monRoot, repel_toJson(&m->repel));
	json_object_set_new(monRoot, "attacks", cnvList_toJsonArray(m->attacks));
	
	return monRoot;
}
/* ****************************************************************************
*
* toJson -
*/
std::string CompoundValueNode::toJson(bool isLastElement)
{
  std::string  out       = "";
  bool         jsonComma = siblingNo < (int) container->childV.size() - 1;
  std::string  tagName   = (container->valueType == orion::ValueTypeVector)? "item" : name;

  // No "comma after" if toplevel
  if (container == this)
  {
    jsonComma = false;
  }

  if (valueType == orion::ValueTypeString)
  {
    LM_T(LmtCompoundValueRender, ("I am a String (%s)", name.c_str()));
    if (container->valueType == orion::ValueTypeVector)
    {
      out = JSON_STR(stringValue);
    }
    else
    {
      out = JSON_STR(tagName) + ":" + JSON_STR(stringValue);
    }
  }
  else if (valueType == orion::ValueTypeNumber)
  {
    char  num[32];

    LM_T(LmtCompoundValueRender, ("I am a Number (%s)", name.c_str()));
    snprintf(num, sizeof(num), "%f", numberValue);

    if (container->valueType == orion::ValueTypeVector)
    {
      out = JSON_NUMBER(num);
    }
    else
    {
      out = JSON_STR(tagName) + ":" + JSON_NUMBER(num);
    }
  }
  else if (valueType == orion::ValueTypeBoolean)
  {
    LM_T(LmtCompoundValueRender, ("I am a Bool (%s)", name.c_str()));

    if (container->valueType == orion::ValueTypeVector)
    {
      out = JSON_BOOL(boolValue);
    }
    else
    {
      out = JSON_STR(tagName) + ":" + JSON_BOOL(boolValue);
    }
  }
  else if ((valueType == orion::ValueTypeVector) && (container == this))
  {
    //
    // NOTE: Here, the '[]' are already added in the calling function
    //
    LM_T(LmtCompoundValueRender, ("I am a Vector (%s) and my container is TOPLEVEL", name.c_str()));
    for (uint64_t ix = 0; ix < childV.size(); ++ix)
    {
      out += childV[ix]->toJson(ix == childV.size() - 1);
    }
  }
  else if ((valueType == orion::ValueTypeVector) && (container->valueType == orion::ValueTypeVector))
  {
    out += "[";

    for (uint64_t ix = 0; ix < childV.size(); ++ix)
    {
      out += childV[ix]->toJson(false);
    }

    out += "]";
  }
  else if (valueType == orion::ValueTypeVector)
  {
    LM_T(LmtCompoundValueRender, ("I am a Vector (%s)", name.c_str()));
    out += JSON_STR(name) + ":[";
    for (uint64_t ix = 0; ix < childV.size(); ++ix)
    {
      out += childV[ix]->toJson(false);
    }

    out += "]";
  }
  else if ((valueType == orion::ValueTypeObject) && (container->valueType == orion::ValueTypeVector))
  {
    LM_T(LmtCompoundValueRender, ("I am an Object (%s) and my container is a Vector", name.c_str()));
    out += "{";
    for (uint64_t ix = 0; ix < childV.size(); ++ix)
    {
      out += childV[ix]->toJson(ix == childV.size() - 1);
    }

    out += "}";
  }
  else if (valueType == orion::ValueTypeObject)
  {
    if (rootP != this)
    {
      LM_T(LmtCompoundValueRender, ("I am an Object (%s) and my container is NOT a Vector", name.c_str()));
      out += JSON_STR(name) + ":{";

      for (uint64_t ix = 0; ix < childV.size(); ++ix)
      {
        out += childV[ix]->toJson(ix == childV.size() - 1);
      }

      out += "}";
    }
    else
    {
      LM_T(LmtCompoundValueRender, ("I am the TREE ROOT (%s: %d children)", name.c_str(), childV.size()));
      for (uint64_t ix = 0; ix < childV.size(); ++ix)
      {
        out += childV[ix]->toJson(true);
      }
    }
  }

  out += jsonComma? "," : "";

  return out;
}
Ejemplo n.º 3
0
/*=========================================================================*\
  Get device info as allocated JSON object string
    (no unicode escaping of strings)
    caller must free result and should lock the device
    returns NULL on error
\*=========================================================================*/
static char *_ickDeviceStateJson( ickDevice_t *device, int indent )
{
  int   rc;
  char *result;
  char *wsi;
  char *message;
  debug( "_ickDeviceStateJson: %s", device->uuid );
  indent += JSON_INDENT;

/*------------------------------------------------------------------------*\
    Empty data?
\*------------------------------------------------------------------------*/
  if( !device )
    return strdup( "null" );

/*------------------------------------------------------------------------*\
    Create websocket information (if any)
\*------------------------------------------------------------------------*/
  /* fixme
  psd  = // no way to get user data from a wsi outside a lws callback scope
  jWsi = _ickWsiStateJson( psd );
  */
  wsi = strdup( JSON_BOOL(device->wsi) );
  if( !wsi ) {
    logerr( "_ickDeviceStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Create message information (if any)
\*------------------------------------------------------------------------*/
  message = _ickMessageStateJson( device->outQueue, indent+JSON_INDENT );
  if( !message ) {
    Sfree( wsi );
    logerr( "_ickDeviceStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Compile all debug info
\*------------------------------------------------------------------------*/
  rc = asprintf( &result,
                  "{\n"
                  "%*s\"name\": \"%s\",\n"
                  "%*s\"tCreation\": %f,\n"
                  "%*s\"UUID\": \"%s\",\n"
                  "%*s\"location\": \"%s\",\n"
                  "%*s\"upnpVersion\": %d,\n"
                  "%*s\"p2pLevel\": %d,\n"
                  "%*s\"lifetime\": %d,\n"
                  "%*s\"services\": %d,\n"
                  "%*s\"getXml\": \"%s\",\n"
                  "%*s\"doConnect\": %s,\n"
                  "%*s\"ssdpState\": \"%s\",\n"
                  "%*s\"bootId\": \"%ld\",\n"
                  "%*s\"configId\": \"%ld\",\n"
                  "%*s\"connectionState\": \"%s\",\n"
                  "%*s\"tXmlComplete\": %f,\n"
                  "%*s\"tConnect\": %f,\n"
                  "%*s\"tDisconnect\": %f,\n"
                  "%*s\"rx\": %d,\n"
                  "%*s\"rxSegmented\": %d,\n"
                  "%*s\"rxPending\": %d,\n"
                  "%*s\"tx\": %d,\n"
                  "%*s\"txPending\": %d,\n"
                  "%*s\"rxLast\": %f,\n"
                  "%*s\"txLast\": %f,\n"
                  "%*s\"wsi\": %s,\n"
                  "%*s\"message\": %s\n"
                  "%*s}",
                  indent, "", JSON_STRING( device->friendlyName ),
                  indent, "", JSON_REAL( device->tCreation ),
                  indent, "", JSON_STRING( device->uuid ),
                  indent, "", JSON_STRING( device->location ),
                  indent, "", JSON_INTEGER( device->ickUpnpVersion ),
                  indent, "", JSON_INTEGER( device->ickP2pLevel ),
                  indent, "", JSON_INTEGER( device->lifetime ),
                  indent, "", JSON_INTEGER( device->services ),
                  indent, "", JSON_STRING( device->wget?_ickWGetUri(device->wget) : NULL ),
                  indent, "", JSON_BOOL( device->doConnect ),
                  indent, "", JSON_STRING( _ickDeviceSsdpState2Str(device->ssdpState) ),
                  indent, "", JSON_LONG( device->ssdpBootId ),
                  indent, "", JSON_LONG( device->ssdpConfigId ),
                  indent, "", JSON_STRING( _ickDeviceConnState2Str(device->connectionState) ),
                  indent, "", JSON_REAL( device->tXmlComplete ),
                  indent, "", JSON_REAL( device->tConnect ),
                  indent, "", JSON_REAL( device->tDisconnect ),
                  indent, "", JSON_INTEGER( device->nRx ),
                  indent, "", JSON_INTEGER( device->nRxSegmented ),
                  indent, "", JSON_INTEGER( _ickDevicePendingInMessages(device) ),
                  indent, "", JSON_INTEGER( device->nTx ),
                  indent, "", JSON_INTEGER( _ickDevicePendingOutMessages(device) ),
                  indent, "", JSON_REAL( device->tLastRx ),
                  indent, "", JSON_REAL( device->tLastTx ),
                  indent, "", JSON_OBJECT( wsi ),
                  indent, "", JSON_OBJECT( message ),
                  indent-JSON_INDENT, ""
                );
  Sfree( wsi );
  Sfree( message );

/*------------------------------------------------------------------------*\
    Error
\*------------------------------------------------------------------------*/
  if( rc<0 ) {
    logerr( "_ickDeviceStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Return result
\*------------------------------------------------------------------------*/
  return result;
}
Ejemplo n.º 4
0
/*=========================================================================*\
  Get context info as allocated JSON object string including device list
    (no unicode escaping of strings)
    caller must free result
    returns NULL on error
\*=========================================================================*/
static char *_ickContextStateJson( ickP2pContext_t *ictx, int indent )
{
  ickDevice_t    *device;
  char           *devices    = NULL;
  ickInterface_t *interface;
  char           *interfaces = NULL;
  int             i;
  int             rc;
  char           *result;
  debug( "_ickContextStateJson (%p): %s", ictx, ictx->deviceUuid );
  indent += JSON_INDENT;

/*------------------------------------------------------------------------*\
    Compile array of interfaces
\*------------------------------------------------------------------------*/
  _ickLibInterfaceListLock( ictx );
  interfaces = strdup( "[" );
  for( i=0,interface=ictx->interfaces; interface&&interfaces; i++,interface=interface->next ) {
    char *interfaceInfo;

    // Get debug info
    interfaceInfo = _ickInterfaceStateJson( interface, indent+JSON_INDENT );
    if( !interfaceInfo ) {
      Sfree( interfaces );
      break;
    }

    // Add to list
    rc = asprintf( &result, "%s%s\n%*s%s", interfaces, i?",":"", indent+JSON_INDENT, "", interfaceInfo );
    Sfree( interfaces );
    Sfree( interfaceInfo );
    if( rc<0 )
      break;
    else
      interfaces = result;
  }
  _ickLibInterfaceListUnlock( ictx );

  // Close list
  if( interfaces ) {
    rc = asprintf( &result, "%s\n%*s]", interfaces, indent, "" );
    Sfree( interfaces );
    if( rc>0 )
      interfaces = result;
  }

  // Error ?
  if( !interfaces ) {
    logerr( "_ickContextStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Compile array of devices
\*------------------------------------------------------------------------*/
  _ickLibDeviceListLock( ictx );
  devices = strdup( "[" );
  for( i=0,device=ictx->deviceList; device&&devices; i++,device=device->next ) {
    char *deviceInfo;

    // Get debug info
    deviceInfo = _ickDeviceStateJson( device, indent+JSON_INDENT );
    if( !deviceInfo ) {
      Sfree( devices );
      break;
    }

    // Add to list
    rc = asprintf( &result, "%s%s\n%*s%s", devices, i?",":"", indent+JSON_INDENT, "", deviceInfo );
    Sfree( devices );
    Sfree( deviceInfo );
    if( rc<0 )
      break;
    else
      devices = result;

  }
  _ickLibDeviceListUnlock( ictx );

  // Close list
  if( devices ) {
    rc = asprintf( &result, "%s\n%*s]", devices, indent, "" );
    Sfree( devices );
    if( rc>0 )
      devices = result;
  }

  // Error ?
  if( !devices ) {
    Sfree( interfaces );
    logerr( "_ickContextStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Compile all context debug info
\*------------------------------------------------------------------------*/
  rc = asprintf( &result,
                  "{\n"
                  "%*s\"pid\": %d,\n"
                  "%*s\"uuid\": \"%s\",\n"
                  "%*s\"name\": \"%s\",\n"
                  "%*s\"services\": %d,\n"
                  "%*s\"upnpListenerPort\": %d,\n"
                  "%*s\"wsPort\": %d,\n"
                  "%*s\"folder\": \"%s\",\n"
                  "%*s\"lifetime\": %d,\n"
                  "%*s\"state\": \"%s\",\n"
                  "%*s\"loopback\": %s,\n"
                  "%*s\"customConnectMatrix\": %s,\n"
                  "%*s\"tCreation\": %f,\n"
                  "%*s\"tResume\": %f,\n"
                  "%*s\"osName\": \"%s\",\n"
                  "%*s\"p2pVersion\": \"%s\",\n"
                  "%*s\"p2pLevel\": %d,\n"
                  "%*s\"lwsVersion\": \"%s\",\n"
                  "%*s\"bootId\": %ld,\n"
                  "%*s\"configId\": %ld,\n"
                  "%*s\"interfaces\": %s\n"
                  "%*s\"devices\": %s\n"
                        "%*s}\n",
                  indent, "", JSON_INTEGER( getpid() ),
                  indent, "", JSON_STRING( ictx->deviceUuid ),
                  indent, "", JSON_STRING( ictx->deviceName ),
                  indent, "", JSON_INTEGER( ictx->ickServices ),
                  indent, "", JSON_INTEGER( ictx->upnpListenerPort ),
                  indent, "", JSON_INTEGER( ictx->lwsPort ),
                  indent, "", JSON_STRING( ictx->upnpFolder),
                  indent, "", JSON_INTEGER( ictx->lifetime ),
                  indent, "", JSON_STRING( ickLibState2Str(ictx->state) ),
                  indent, "", JSON_BOOL( ictx->upnpLoopback ),
                  indent, "", JSON_BOOL( ictx->lwsConnectMatrixCb==ickP2pDefaultConnectMatrixCb ),
                  indent, "", JSON_REAL( ictx->tCreation ),
                  indent, "", JSON_REAL( ictx->tResume ),
                  indent, "", JSON_STRING( ictx->osName ),
                  indent, "", JSON_STRING( ickP2pGetVersion(NULL,NULL) ),
                  indent, "", JSON_INTEGER( ICKP2PLEVEL_SUPPORTED ),
                  indent, "", JSON_STRING( lws_get_library_version() ),
                  indent, "", JSON_LONG( ictx->upnpBootId ),
                  indent, "", JSON_LONG( ictx->upnpConfigId ),
                  indent, "", JSON_OBJECT( interfaces ),
                  indent, "", JSON_OBJECT( devices ),
                  indent-JSON_INDENT, ""
                );
  Sfree( devices );
  Sfree( interfaces );

/*------------------------------------------------------------------------*\
    Error
\*------------------------------------------------------------------------*/
  if( rc<0 ) {
    logerr( "_ickContextStateJson: out of memory" );
    return NULL;
  }

/*------------------------------------------------------------------------*\
    Return result
\*------------------------------------------------------------------------*/
  return result;
}