Exemple #1
0
    /**
     * Reads the description from the string
     * @param json the json description as a string
     * @param kd the resulting knapsack data
     */
    static void readJson(const std::string& json, KnapData< T >& kd) {

        struct {
            bool mNSet;
            bool mCSet;
            bool mWSet;
            bool mPSet;
        } checklist = {false, false, false, false};

        JSONNode nd = libjson::parse(json);
        JSONNode::const_iterator i = nd.begin();
        kd.mN = 0;
        kd.mTP = 0;
        kd.mTW = 0;
        kd.mCoe = NULL;


        while (i != nd.end()) {
            std::string name = i->name();
            //std::cout << "node \"" << name << "\"\n";
            if (name == JSON_KNAP_N) {
                kd.mN = i->as_int();
                kd.mCoe = new KnapRecord<T>[kd.mN];
                BNB_ASSERT(kd.mCoe);
                checklist.mNSet = true;
            } else if (name == JSON_KNAP_C) {
                kd.mC = i->as_int();
                checklist.mCSet = true;
            } else if (name == JSON_KNAP_W) {
                BNB_ASSERT(kd.mCoe);
                parseW(*i, kd, checklist.mPSet);
                checklist.mWSet = true;
            } else if (name == JSON_KNAP_P) {
                BNB_ASSERT(kd.mCoe);
                parseP(*i, kd, checklist.mWSet);
                checklist.mPSet = true;
            }
            i++;
        }
    }
Exemple #2
0
const vector<uint64_t> JSONModelGetter::operator()(const string &rel_key){
    vector<uint64_t> ret;
    JSONNode::const_iterator it = data->find(rel_key);
    if(it==data->end()){//||it->type()!=JSON_ARRAY) {
        return ret;
    }
    JSONNode fns_node = it->as_array();
    if(fns_node.size()>0){
        JSONNode::const_iterator stit = fns_node.begin();
        while(stit!=fns_node.end()){
            uint64_t fn = 0UL;
            if(stit->type()==JSON_NUMBER){
                fn =  stit->as_int();
            }else if(it->type()==JSON_STRING){
                string fs = it->as_string();
                fn = strtoul(fs.c_str(), NULL, 10);
            }
            ret.push_back(fn);
            ++stit;
        }
    }
    return ret;
}
Handle<Object> AtJsonParser::parse( const JSONNode &n ) {
    Isolate* isolate = Isolate::GetCurrent();
    Handle<Object> ret = Object::New( isolate );

    JSONNode::const_iterator i = n.begin();
    while ( i != n.end() ) {
        std::string node_name = i->name();
        if ( node_name == "serverTime" ||
             node_name == "response" ||
             node_name == "data" ||
             node_name == "tick" ||
             node_name == "ATBarHistory" ||
             node_name == "ATSymbol" ||
             node_name == "ATLoginResponse" ||
             node_name == "time" ||
             node_name == "open" ||
             node_name == "high" ||
             node_name == "low" ||
             node_name == "close" ||
             node_name == "ATTickHistory" ||
             node_name == "offsetDatabaseDate" ||
             node_name == "tradeLastPrice" ||
             node_name == "quoteBidPrice" ||
             node_name == "quoteAskPrice" ||
             node_name == "ATMarketMoversDb" ||
             node_name == "recordSymbol" ||
             node_name == "itemSymbol" ||
             node_name == "itemLastPrice" ||
             node_name == "ATQuoteDb" ||
             node_name == "priceData" ||
             node_name == "dateTime" ||
             node_name == "ATQuoteStream" ||
             node_name == "lastPrice" ||
             node_name == "bidPrice" ||
             node_name == "askPrice" ||
             node_name == "prevClose" ||
             node_name == "afterMarketClose" ||
             node_name == "lastUpdateTime" ||
             node_name == "marketMovers" ||
             node_name == "closePrice" ||
             node_name == "lastDateTime" ||
             node_name == "beginDateTime" ||
             node_name == "endDateTime"
            ) {
            ret->Set( String::NewFromUtf8( isolate, node_name.c_str() ),
                      parse( *i ) );
        }
        else if ( filterAsString( node_name ) ) {
            ret->Set( String::NewFromUtf8( isolate, node_name.c_str() ),
                      String::NewFromUtf8( isolate, i->as_string().c_str() ) );
        }
        else if ( filterAsNumber( node_name ) ) {
            ret->Set( String::NewFromUtf8( isolate, node_name.c_str() ),
                      Number::New( isolate, i->as_float() ) );
        }
        else if ( filterAsInteger( node_name ) ) {
            ret->Set( String::NewFromUtf8( isolate, node_name.c_str() ),
                      Integer::New( isolate, i->as_int() ) );
        }
        else if ( filterAsBoolean( node_name ) ) {
            ret->Set( String::NewFromUtf8( isolate, node_name.c_str() ),
                      Boolean::New( isolate, i->as_bool() ) );
        }
        else if ( i->type() == JSON_ARRAY ) {
            JSONNode::const_iterator j = i->begin();
            std::vector< Handle<Object> > vec;
            while ( j != i->end() ) {
                vec.push_back( parse( *j ) );
                ++j;
            }
            Handle<Array> arr = Array::New( isolate, vec.size() );
            for ( size_t idx = 0; idx < vec.size(); idx++ ) {
                arr->Set( idx, vec[idx] );
            }
            ret->Set( String::NewFromUtf8( isolate, node_name.c_str() ), arr );
        }
        ++i;
    }
    return ret;

}