void Graph::fromJSON( const JsonValue& root )
  {
    if ( !mSections.empty() )
      clear();

    SPARK_ASSERT( root.getTag() == JSON_TAG_OBJECT );

    JsonNode* node = root.toNode();
    while ( node )
    {
      if ( strcmp( node->key, cGraphSections ) == 0 )
      {
        SPARK_ASSERT( node->value.getTag() == JSON_TAG_ARRAY );
        JsonNode* sections = node->value.toNode();
        while ( sections )
        {
          GraphSection* section = new GraphSection();
          section->fromJSON( sections->value );
          mSections.push_back( section );
          sections = sections->next;
        }
      }
      node = node->next;
    }
  }
示例#2
0
bool isRegularObjArray( const JsonValue &a ) {
  // check if all JSON_OBJECTs in JSON_ARRAY have the same fields
  JsonValue o=a.toNode()->value; siz k, n; const char **keys;
  n=length(o); keys=new const char*[n];
  k=0; for(auto j:o) keys[k++]=j->key;
  for( auto i:a ) {
    if(length(i->value)!=n) return false; k=0;
    for(auto j:i->value) if(strcmp(j->key,keys[k++])) return false;
  }
  delete [] keys; return true;
}
  void GraphManager::fromJSON( const JsonValue& root )
  {
    SPARK_ASSERT( root.getTag() == JSON_TAG_ARRAY );

    JsonNode* node = root.toNode();
    while ( node )
    {
      Graph* graph = new Graph();
      graph->fromJSON( node->value );
      mGraphs.push_back( graph );
      node = node->next;
    }
  }
  bool VibratorPacket::parseFrom( const JsonValue& root )
  {
    if ( root.getTag() != JSON_TAG_OBJECT )
      return false;

    JsonNode* node = root.toNode();
    while ( node )
    {
      if ( strcmp( node->key, "i" ) == 0 )
      {
        if ( node->value.getTag() != JSON_TAG_NUMBER )
          return false;

        index = node->value.toNumber();
      }
      else if ( strcmp( node->key, "cmd" ) == 0 )
      {
        if ( node->value.getTag() != JSON_TAG_STRING )
          return false;

        if ( strcmp( node->value.toString(), "graph" ) == 0 )
          command = Command_Graph;
        else if ( strcmp( node->value.toString(), "pause" ) == 0 )
          command = Command_Pause;
        else if ( strcmp( node->value.toString(), "continue" ) == 0 )
          command = Command_Continue;
        else if ( strcmp( node->value.toString(), "stop" ) == 0 )
          command = Command_Stop;
        else
          return false;
      }
      else if ( strcmp( node->key, "graph" ) == 0 )
      {
        if ( node->value.getTag() != JSON_TAG_NUMBER )
          return false;

        graph = node->value.toNumber();
      }
      node = node->next;
    }

    return true;
  }
  void GraphSection::fromJSON( const JsonValue& root )
  {
    SPARK_ASSERT( root.getTag() == JSON_TAG_OBJECT );

    JsonNode* node = root.toNode();
    while ( node )
    {
      if ( strcmp( node->key, cSectionType ) == 0 )
      {
        SPARK_ASSERT( node->value.getTag() == JSON_TAG_STRING );
        for ( int i = 0; i < Interp_MAX; i++ )
          if ( strcmp( node->value.toString(), cInterpolationMethods[i] ) == 0 )
          {
            mInterpolation = (Interpolation)i;
            break;
          }
      }
      else if ( strcmp( node->key, cSectionData ) == 0 )
      {
        SPARK_ASSERT( node->value.getTag() == JSON_TAG_ARRAY );
        int i = 0;
        JsonNode* data = node->value.toNode();
        while ( data )
        {
          SPARK_ASSERT( data->value.getTag() == JSON_TAG_NUMBER );
          if ( i == 0 )
            mBegin = data->value.toNumber();
          else if ( i == 1 )
            mEnd = data->value.toNumber();
          else if ( i == 2 )
            mLength = data->value.toNumber();
          i++;
          data = data->next;
        }
      }
      node = node->next;
    }
  }
示例#6
0
siz length( const JsonValue &a ) {
  // get number of elements in JSON_ARRAY or JSON_OBJECT
  siz k=0; auto n=a.toNode(); while(n) { k++; n=n->next; } return k;
}
static void dumpValue(std::ostringstream& os, const JsonValue& o, int shiftWidth, const std::string& linefeed = "", int indent = 0) {
    switch (o.getTag()) {
    case JSON_NUMBER:
        char buffer[32];
        sprintf(buffer, "%f", o.toNumber());
        os << buffer;
        break;
    case JSON_TRUE:
        os << "true";
        break;        
    case JSON_FALSE:
        os << "false";
        break;
    case JSON_STRING:
        dumpString(os, o.toString());
        break;
    case JSON_ARRAY:
        // It is not necessary to use o.toNode() to check if an array or object
        // is empty before iterating over its members, we do it here to allow
        // nicer pretty printing.
        if (!o.toNode()) {
            os << "[]";
            break;
        }
        os << "[" << linefeed;
        for (auto i : o) {
            if (shiftWidth > 0)
                os << std::setw(indent + shiftWidth) << " " << std::setw(0);
            dumpValue(os, i->value, shiftWidth, linefeed, indent + shiftWidth);
            if (i->next)
                os << ",";
            os << linefeed;
        }
        if (indent > 0)
            os << std::setw(indent) << " " << std::setw(0);
        os.put(']');
        break;
    case JSON_OBJECT:
        if (!o.toNode()) {
            os << "{}";
            break;
        }
        os << "{" << linefeed;
        for (auto i : o) {
            if (shiftWidth > 0)
                os << std::setw(indent + shiftWidth) << " " << std::setw(0);
            dumpString(os, i->key);
            os << ":";
            dumpValue(os, i->value, shiftWidth, linefeed, indent + shiftWidth);
            if (i->next)
                os << ",";
            os << linefeed;
        }
        if (indent > 0)
            os << std::setw(indent) << " " << std::setw(0);
        os.put('}');
        break;
    case JSON_NULL:
        os << "null";
        break;
    }
}