Exemplo n.º 1
0
void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] )
{
  char action[1024]; if(!nr) mexErrMsgTxt("Inputs expected.");
  mxGetString(pr[0],action,1024); nr--; pr++;
  char *endptr; JsonValue val; JsonAllocator allocator;
  if( nl>1 ) mexErrMsgTxt("One output expected.");
  
  if(!strcmp(action,"convert")) {
    if( nr!=1 ) mexErrMsgTxt("One input expected.");
    if( mxGetClassID(pr[0])==mxCHAR_CLASS ) {
      // object = mexFunction( string )
      char *str = mxArrayToStringRobust(pr[0]);
      int status = jsonParse(str, &endptr, &val, allocator);
      if( status != JSON_OK) mexErrMsgTxt(jsonStrError(status));
      pl[0] = json(val); mxFree(str);
    } else {
      // string = mexFunction( object )
      ostrm S; S << std::setprecision(12); json(S,pr[0]);
      pl[0]=mxCreateStringRobust(S.str().c_str());
    }
    
  } else if(!strcmp(action,"split")) {
    // strings = mexFunction( string, k )
    if( nr!=2 ) mexErrMsgTxt("Two input expected.");
    char *str = mxArrayToStringRobust(pr[0]);
    int status = jsonParse(str, &endptr, &val, allocator);
    if( status != JSON_OK) mexErrMsgTxt(jsonStrError(status));
    if( val.getTag()!=JSON_ARRAY ) mexErrMsgTxt("Array expected");
    siz i=0, t=0, n=length(val), k=(siz) mxGetScalar(pr[1]);
    k=(k>n)?n:(k<1)?1:k; k=ceil(n/ceil(double(n)/k));
    pl[0]=mxCreateCellMatrix(1,k); ostrm S; S<<std::setprecision(12);
    for(auto o:val) {
      if(!t) { S.str(std::string()); S << "["; t=ceil(double(n)/k); }
      json(S,&o->value); t--; if(!o->next) t=0; S << (t ? "," : "]");
      if(!t) mxSetCell(pl[0],i++,mxCreateStringRobust(S.str().c_str()));
    }
    
  } else if(!strcmp(action,"merge")) {
    // string = mexFunction( strings )
    if( nr!=1 ) mexErrMsgTxt("One input expected.");
    if(!mxIsCell(pr[0])) mexErrMsgTxt("Cell array expected.");
    siz n = mxGetNumberOfElements(pr[0]);
    ostrm S; S << std::setprecision(12); S << "[";
    for( siz i=0; i<n; i++ ) {
      char *str = mxArrayToStringRobust(mxGetCell(pr[0],i));
      int status = jsonParse(str, &endptr, &val, allocator);
      if( status != JSON_OK) mexErrMsgTxt(jsonStrError(status));
      if( val.getTag()!=JSON_ARRAY ) mexErrMsgTxt("Array expected");
      for(auto j:val) json(S,&j->value) << (j->next ? "," : "");
      mxFree(str); if(i<n-1) S<<",";
    }
    S << "]"; pl[0]=mxCreateStringRobust(S.str().c_str());
    
  } else mexErrMsgTxt("Invalid action.");
}
Exemplo n.º 2
0
  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;
    }
  }
Exemplo n.º 3
0
  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;
    }
  }
Exemplo n.º 4
0
  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;
  }
Exemplo n.º 5
0
    void OnData(
        const happyhttp::Response* r,
        void* userdata,
        const unsigned char* data,
        int n
        )
    {
        std::cout << "reading...\n";

        char *endptr;
        JsonValue value;
        JsonAllocator allocator;
        JsonParseStatus status = jsonParse((char*)data, &endptr, &value, allocator);
        if (status != JSON_PARSE_OK) {
            fprintf(stderr, "error at %zd, status: %d\n", endptr - (char*)data, status);
            exit(EXIT_FAILURE);
        } else { // SUCCESS
            switch (value.getTag()) {
            case JSON_TAG_NUMBER:
                printf("%g\n", value.toNumber());
                break;
            case JSON_TAG_BOOL:
                printf("%s\n", value.toBool() ? "true" : "false");
                break;
            case JSON_TAG_STRING:
                printf("\"%s\"\n", value.toString());
                break;
            case JSON_TAG_ARRAY:
                for (auto i : value) {
                    auto bleh = i->value;
                }
                break;
            case JSON_TAG_OBJECT:
                for (auto i : value) {
                    printf("%s = ", i->key);
                }
                break;
            case JSON_TAG_NULL:
                printf("null\n");
                break;
            }
        }
    }
static void GenStat(Stat& stat, const JsonValue& v) {
    switch (v.getTag()) {
    case JSON_ARRAY:
        for (auto const& i : v) {
            GenStat(stat, i->value);
            stat.elementCount++;
        }
        stat.arrayCount++;
        break;

    case JSON_OBJECT:
        for (auto const& i : v) {
            GenStat(stat, i->value);
            stat.memberCount++;
            stat.stringLength += strlen(i->key);
            stat.stringCount++;   // Key
        }
        stat.objectCount++;
        break;

    case JSON_STRING: 
        stat.stringCount++;
        stat.stringLength += strlen(v.toString());
        break;

    case JSON_NUMBER:
        stat.numberCount++;
        break;

    case JSON_TRUE:
        stat.trueCount++;
        break;

    case JSON_FALSE:
        stat.falseCount++;
        break;

    case JSON_NULL:
        stat.nullCount++;
        break;
    }
}
Exemplo n.º 7
0
double traverse_gason(JsonValue o)
{
	double x = 0;
	switch (o.getTag())
	{
		case JSON_TAG_NUMBER:
			x += o.toNumber();
			break;
		case JSON_TAG_ARRAY:
		case JSON_TAG_OBJECT:
			for (auto i : o)
			{
				x += traverse_gason(i->value);
			}
			break;
		default:
			return 0;
	}
	return x;
}
Exemplo n.º 8
0
void NewsScene::printReturn(JsonValue o)
{
    switch (o.getTag())
    {
        case JSON_TAG_NUMBER:
            printf("%g\n", o.toNumber());
            //sum += o.toNumber();
            break;
        case JSON_TAG_BOOL:
            printf("%s\n", o.toBool() ? "true" : "false");
            break;
        case JSON_TAG_STRING:
        {
            string theValue =o.toString();
            setValue(theValue);
        }
            break;
        case JSON_TAG_ARRAY:
            for (auto i : o)
            {
               // tmpNews = new News();
                listNews.push_back(new News());
                printReturn(i->value);
            }
            
            break;
        case JSON_TAG_OBJECT:
            for (auto i : o)
            {
                printf("%s = ", i->key);
                lastKey = i->key;
                printReturn(i->value);
            }
            break;
        case JSON_TAG_NULL:
            printf("null\n");
            break;
    }
}
Exemplo n.º 9
0
  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;
    }
  }
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;
    }
}