bool JSONObject::load(JSONHelper &reader) { while (true) { Token token=reader.nextToken(); if (token==TokenObjectClose) break; if (token!=TokenString) //error: object doesn't start with key return false; QString key=reader.readString(); if (key.length()==0) //error: empty key return false; if (reader.nextToken()!=TokenKeySeparator) //error: no colon after key return false; JSON *child=parse(reader); if (!child) //error: child failed to parse return false; children.insert(key,child); token=reader.nextToken(); if (token==TokenObjectClose) break; if (token!=TokenValueSeparator) //error: no comma return false; } return true; }
bool JSONArray::load(JSONHelper &reader) { Token token=reader.peekToken(); if (token==TokenArrayClose) { reader.nextToken(); return true; } while (true) { JSON *child=parse(reader); if (!child) //error: child failed to parse return false; children.append(child); token=reader.nextToken(); if (token==TokenArrayClose) break; if (token!=TokenValueSeparator) //error: no comma return false; } return true; }
BasketGeometricOpenCLOption::BasketGeometricOpenCLOption(JSONHelper ¶meters) : ClosedFormOpenCLOption(parameters), cl_start_prices(NULL), cl_asset_volatilities(NULL), cl_correlations(NULL) { unsigned int tmp_size; strike_price = parameters.get_float("strike_price"); maturity = parameters.get_float("maturity"); risk_free_rate = parameters.get_float("risk_free_rate"); start_prices = parameters.get_vector("start_price", &number_of_assets); asset_volatilities = parameters.get_vector("volatility", &tmp_size); if(tmp_size < number_of_assets) { number_of_assets = tmp_size; } correlations = parameters.get_matrix("correlation", &tmp_size); if(tmp_size < number_of_assets) { number_of_assets = tmp_size; } assert(start_prices != NULL); assert(asset_volatilities != NULL); assert(correlations != NULL); assert(number_of_assets > 0); assert(strike_price >= 0); assert(maturity > 0); assert(risk_free_rate >= 0); for(unsigned int i = 0; i < number_of_assets; i++) { assert(start_prices[i] >= 0); assert(asset_volatilities[i] >= 0); for(unsigned int j = 0; j < number_of_assets; j++) { assert(correlations[i*number_of_assets + j] >= 0); assert(correlations[i*number_of_assets + j] <= 1); } } }
JSONArray::JSONArray(JSONHelper &reader) { while (true) { Token type = reader.nextToken(); if (type == TokenArrayClose) break; JSONData *value; switch (type) { case TokenNULL: value = NULL; break; case TokenTRUE: value = new JSONBool(true); break; case TokenFALSE: value = new JSONBool(false); break; case TokenString: value = new JSONString(reader.readString()); break; case TokenNumber: value = new JSONNumber(reader.readDouble()); break; case TokenObject: value = new JSONObject(reader); break; case TokenArray: value = new JSONArray(reader); break; default: throw JSONParseException("Expected Value", reader.location()); } data.append(value); type = reader.nextToken(); // comma or end if (type == TokenArrayClose) break; if (type != TokenValueSeparator) throw JSONParseException("Expected ',' or ']'", reader.location()); } }
JSONObject::JSONObject(JSONHelper &reader) { while (true) { Token type = reader.nextToken(); if (type == TokenObjectClose) break; if (type != TokenString) throw JSONParseException("Expected quoted string", reader.location()); QString key = reader.readString(); if (key.length() == 0) throw JSONParseException("Empty object key", reader.location()); if (reader.nextToken() != TokenKeySeparator) throw JSONParseException("Expected ':'", reader.location()); JSONData *value; switch (reader.nextToken()) { case TokenNULL: value = NULL; break; case TokenTRUE: value = new JSONBool(true); break; case TokenFALSE: value = new JSONBool(false); break; case TokenString: value = new JSONString(reader.readString()); break; case TokenNumber: value = new JSONNumber(reader.readDouble()); break; case TokenObject: value = new JSONObject(reader); break; case TokenArray: value = new JSONArray(reader); break; default: throw JSONParseException("Expected value", reader.location()); } children[key] = value; type = reader.nextToken(); // comma or end if (type == TokenObjectClose) break; if (type != TokenValueSeparator) throw JSONParseException("Expected ',' or '}'", reader.location()); } }