Example usage for org.json.simple.parser ParseException toString

List of usage examples for org.json.simple.parser ParseException toString

Introduction

In this page you can find the example usage for org.json.simple.parser ParseException toString.

Prototype

public String toString() 

Source Link

Usage

From source file:com.nubits.nubot.trading.wrappers.BittrexWrapper.java

private Trade parseTrade(JSONObject in) {
    Trade out = new Trade();

    out.setExchangeName(Global.exchange.getName());
    //Bittrex returns the currencies inverted. turn them round here.
    CurrencyPair orderPair = CurrencyPair.getCurrencyPairFromString(in.get("Exchange").toString(), "-");
    CurrencyPair pair = new CurrencyPair(orderPair.getPaymentCurrency(), orderPair.getOrderCurrency());
    out.setPair(pair);/*  w w w .  java 2s  . co m*/
    out.setType(in.get("OrderType").toString().contains("SELL") ? Constant.SELL : Constant.BUY);
    Amount amount = new Amount(Utils.getDouble(in.get("Quantity")), pair.getOrderCurrency());
    out.setAmount(amount);
    Amount price = new Amount(Utils.getDouble(in.get("Price")), pair.getPaymentCurrency());
    out.setPrice(price);
    //2014-07-09T03:55:48.77
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
    Date date = null;
    try {
        if (in.get("Closed") != null) {
            date = sdf.parse(in.get("Closed").toString());
        }
    } catch (java.text.ParseException pe) {
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            if (in.get("Opened") != null) {
                date = sdf.parse(in.get("Closed").toString());
            }
        } catch (java.text.ParseException pe1) {
            LOG.error(pe1.toString());
        }
    }
    if (date != null) {
        long timeStamp = date.getTime();
        Date insertDate = new Date(timeStamp);
        out.setDate(insertDate);
    }

    return out;
}

From source file:com.nubits.nubot.trading.wrappers.BitcoinCoIDWrapper.java

private ApiResponse getQuery(String url, String method, HashMap<String, String> query_args, boolean needAuth,
        boolean isGet) {
    ApiResponse apiResponse = new ApiResponse();
    String queryResult = query(url, method, query_args, needAuth, isGet);
    if (queryResult == null) {
        apiResponse.setError(errors.nullReturnError);
        return apiResponse;
    }// w w  w  . java2  s .  c om
    if (queryResult.equals(TOKEN_BAD_RETURN)) {
        apiResponse.setError(errors.noConnectionError);
        return apiResponse;
    }
    JSONParser parser = new JSONParser();

    try {
        JSONObject httpAnswerJson = (JSONObject) (parser.parse(queryResult));
        int code = 0;
        try {
            code = Integer.parseInt(httpAnswerJson.get(TOKEN_CODE).toString());
        } catch (ClassCastException cce) {
            apiResponse.setError(errors.genericError);
        }

        if (code == 0) {
            String errorMessage = (String) httpAnswerJson.get(TOKEN_ERR);
            ApiError apiError = errors.apiReturnError;
            apiError.setDescription(errorMessage);
            //LOG.error("AllCoin API returned an error : " + errorMessage);
            apiResponse.setError(apiError);
        } else {
            apiResponse.setResponseObject(httpAnswerJson);
        }
    } catch (ClassCastException cce) {
        //if casting to a JSON object failed, try a JSON Array
        try {
            JSONArray httpAnswerJson = (JSONArray) (parser.parse(queryResult));
            apiResponse.setResponseObject(httpAnswerJson);
        } catch (ParseException pe) {
            LOG.error("httpResponse: " + queryResult + " \n" + pe.toString());
            apiResponse.setError(errors.parseError);
        }
    } catch (ParseException pe) {
        LOG.error("httpResponse: " + queryResult + " \n" + pe.toString());
        apiResponse.setError(errors.parseError);
        return apiResponse;
    }
    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.BittrexWrapper.java

private Order parseOrder(JSONObject in) {
    Order out = new Order();

    out.setId(in.get("OrderUuid").toString());
    //Bittrex returns the currencies inverted. turn them round here.
    CurrencyPair orderPair = CurrencyPair.getCurrencyPairFromString(in.get("Exchange").toString(), "-");
    CurrencyPair pair = new CurrencyPair(orderPair.getPaymentCurrency(), orderPair.getOrderCurrency());
    out.setPair(pair);/*from w w  w  .  j  a v a 2 s. c o  m*/
    out.setType(in.get("OrderType").toString().contains("SELL") ? Constant.SELL : Constant.BUY);
    Amount amount = new Amount(Utils.getDouble(in.get("Quantity")), pair.getOrderCurrency());
    out.setAmount(amount);
    Amount price = new Amount(Utils.getDouble(in.get("Price")), pair.getPaymentCurrency());
    out.setPrice(price);
    //2014-07-09T03:55:48.77
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
    Date date = null;
    try {
        if (in.get("Opened") != null) {
            date = sdf.parse(in.get("Opened").toString());
        }
    } catch (java.text.ParseException pe) {
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            if (in.get("Opened") != null) {
                date = sdf.parse(in.get("Opened").toString());
            }
        } catch (java.text.ParseException pe1) {
            LOG.error(pe1.toString());
        }
    }
    if (date != null) {
        long timeStamp = date.getTime();
        Date insertDate = new Date(timeStamp);
        out.setInsertedDate(insertDate);
    }
    sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
    date = null;
    try {
        if (in.get("Closed") != null) {
            date = sdf.parse(in.get("Closed").toString());
        }
    } catch (java.text.ParseException pe) {
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            if (in.get("Opened") != null) {
                date = sdf.parse(in.get("Closed").toString());
            }
        } catch (java.text.ParseException pe1) {
            LOG.error(pe1.toString());
        }
    }
    if (date != null) {
        long timeStamp = date.getTime();
        Date insertDate = new Date(timeStamp);
        out.setExecutedDate(insertDate);
    }
    out.setCompleted(false);
    if (Utils.getDouble(in.get("QuantityRemaining")) == 0) {
        out.setCompleted(true);
    }

    return out;
}

From source file:com.nubits.nubot.trading.wrappers.AltsTradeWrapper.java

private ApiResponse getQuery(String url, HashMap<String, String> query_args, boolean needAuth, boolean isGet) {
    ApiResponse apiResponse = new ApiResponse();
    String queryResult = query(url, "", query_args, needAuth, isGet);
    if (queryResult == null) {
        apiResponse.setError(errors.nullReturnError);
        return apiResponse;
    }/*www. j  a  v  a2s .  c om*/
    if (queryResult.equals(TOKEN_BAD_RETURN)) {
        apiResponse.setError(errors.noConnectionError);
        return apiResponse;
    }

    JSONParser parser = new JSONParser();
    try {
        JSONObject httpAnswerJson = (JSONObject) (parser.parse(queryResult));
        boolean status = (boolean) httpAnswerJson.get("status");
        if (!status) {
            ApiError error = errors.apiReturnError;
            error.setDescription(httpAnswerJson.get("error").toString());
            apiResponse.setError(error);
        } else {
            apiResponse.setResponseObject(httpAnswerJson);
        }
    } catch (ClassCastException cce) {
        //if casting to a JSON object failed, try a JSON Array
        try {
            JSONArray httpAnswerJson = (JSONArray) (parser.parse(queryResult));
            apiResponse.setResponseObject(httpAnswerJson);
        } catch (ParseException pe) {
            // Alts.trade return the order id as an integer
            LOG.error("httpResponse: " + queryResult + " \n" + pe.toString());
            apiResponse.setError(errors.parseError);
        }
    } catch (ParseException pe) {
        LOG.error("httpResponse: " + queryResult + " \n" + pe.toString());
        apiResponse.setError(errors.parseError);
        return apiResponse;
    }
    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.BittrexWrapper.java

private ApiResponse getQuery(String url, String method, HashMap<String, String> query_args, boolean needAuth,
        boolean isGet) {
    ApiResponse apiResponse = new ApiResponse();
    String queryResult = query(url, method, query_args, needAuth, isGet);
    if (queryResult == null) {
        apiResponse.setError(errors.nullReturnError);
        return apiResponse;
    }/*  w w w .  j  a v  a  2s. c  o m*/
    if (queryResult.equals(TOKEN_BAD_RETURN)) {
        apiResponse.setError(errors.noConnectionError);
        return apiResponse;
    }

    JSONParser parser = new JSONParser();

    try {
        JSONObject httpAnswerJson = (JSONObject) (parser.parse(queryResult));
        if (!(boolean) httpAnswerJson.get("success")) {
            ApiError error = errors.apiReturnError;
            error.setDescription(httpAnswerJson.get("message").toString());
            apiResponse.setError(error);
            return apiResponse;
        }
        apiResponse.setResponseObject(httpAnswerJson);
    } catch (ClassCastException cce) {
        //if casting to a JSON object failed, try a JSON Array
        try {
            JSONArray httpAnswerJson = (JSONArray) (parser.parse(queryResult));
            apiResponse.setResponseObject(httpAnswerJson);
        } catch (ParseException pe) {
            LOG.error("httpResponse: " + queryResult + " \n" + pe.toString());
            apiResponse.setError(errors.parseError);
        }
    } catch (ParseException pe) {
        LOG.error("httpResponse: " + queryResult + " \n" + pe.toString());
        apiResponse.setError(errors.parseError);
        return apiResponse;
    }
    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.CcexWrapper.java

private Date parseDate(String dateStr) {
    Date toRet = null;//from  ww  w  .  j  a v  a 2s  .co m
    //Parse the date
    //Sample 2014-02-19 04:55:44

    String datePattern = "yyyy-MM-dd HH:mm:ss";
    DateFormat df = new SimpleDateFormat(datePattern, Locale.ENGLISH);
    try {
        toRet = df.parse(dateStr);
    } catch (java.text.ParseException ex) {
        LOG.error(ex.toString());
        toRet = new Date();
    }
    return toRet;
}

From source file:com.nubits.nubot.trading.wrappers.ExcoinWrapper.java

public Order parseOrder(JSONObject in, CurrencyPair pair, String type) {
    Order out = new Order();
    //set the id//from   w  ww .  ja  v a 2  s  .  c  o  m
    out.setId(in.get("id").toString());
    //set the inserted date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    Date date = null;
    try {
        date = sdf.parse(in.get("timestamp").toString());
    } catch (java.text.ParseException pe) {
        //sometimes timestamp in this format are returned
        //2014-12-19T16:02:07.961Z
        sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
        try {
            date = sdf.parse(in.get("timestamp").toString());
        } catch (java.text.ParseException pe1) {
            LOG.error(pe1.toString());
        }
    }
    if (date != null) {
        long timeStamp = date.getTime();
        Date insertDate = new Date(timeStamp);
        out.setInsertedDate(insertDate);
    }
    //set the price
    Amount price = new Amount(Double.parseDouble(in.get("price").toString()), pair.getPaymentCurrency());
    out.setPrice(price);
    //set the amount
    Amount amount = new Amount(Double.parseDouble(in.get("commodity_amount").toString()),
            pair.getOrderCurrency());
    out.setAmount(amount);
    //set the type
    if (type == null) {
        type = in.get("type").toString();
    }
    out.setType(type.equals("BID") ? Constant.BUY : Constant.SELL);
    //set the pair
    out.setPair(pair);

    return out;
}

From source file:com.nubits.nubot.trading.wrappers.PeatioWrapper.java

private Date parseDate(String dateStr) {
    Date toRet = null;/*from   www  .ja  v  a 2 s  .c  o  m*/
    //Parse the date
    //Sample 2014-08-19T10:23:49Z

    //Remove the Timezone
    dateStr = dateStr.substring(0, dateStr.length() - 1);
    String datePattern = "yyyy-MM-dd'T'HH:mm:ss";
    DateFormat df = new SimpleDateFormat(datePattern, Locale.ENGLISH);
    try {
        toRet = df.parse(dateStr);
    } catch (java.text.ParseException ex) {
        LOG.error(ex.toString());
        toRet = new Date();
    }
    return toRet;
}

From source file:com.nubits.nubot.trading.wrappers.BitSparkWrapper.java

private Date parseDate(String dateStr) {
    Date toRet = null;//from  w  w w  .j a v  a 2s .co m
    //Parse the date
    //Sample 2014-08-19T10:23:49+01:00

    //Remove the Timezone
    dateStr = dateStr.substring(0, dateStr.length() - 1);
    String datePattern = "yyyy-MM-dd'T'HH:mm:ss";
    DateFormat df = new SimpleDateFormat(datePattern, Locale.ENGLISH);
    try {
        toRet = df.parse(dateStr);
    } catch (java.text.ParseException ex) {
        LOG.error(ex.toString());
        toRet = new Date();
    }
    return toRet;
}

From source file:com.nubits.nubot.trading.wrappers.BtceWrapper.java

private ApiResponse getQuery(String url, String method, HashMap<String, String> query_args, boolean needAuth,
        boolean isGet) {
    ApiResponse apiResponse = new ApiResponse();
    String queryResult = query(API_BASE_URL, method, query_args, needAuth, isGet);
    if (queryResult == null) {
        apiResponse.setError(errors.nullReturnError);
        return apiResponse;
    }// w  w  w.ja  v a 2 s . c  om
    if (queryResult.equals(TOKEN_BAD_RETURN)) {
        apiResponse.setError(errors.noConnectionError);
        return apiResponse;
    }

    JSONParser parser = new JSONParser();
    try {
        JSONObject httpAnswerJson = (JSONObject) (parser.parse(queryResult));
        long success = (long) httpAnswerJson.get("success");
        if (success == 0) {
            //error
            String errorMessage = (String) httpAnswerJson.get("error");
            ApiError apiErr = errors.apiReturnError;
            apiErr.setDescription(errorMessage);
            //LOG.error("Btce returned an error: " + errorMessage);
            apiResponse.setError(apiErr);
        } else {
            apiResponse.setResponseObject(httpAnswerJson);
        }
    } catch (ClassCastException cce) {
        //if casting to a JSON object failed, try a JSON Array
        try {
            JSONArray httpAnswerJson = (JSONArray) (parser.parse(queryResult));
            apiResponse.setResponseObject(httpAnswerJson);
        } catch (ParseException pe) {
            LOG.error("httpResponse: " + queryResult + " \n" + pe.toString());
            apiResponse.setError(errors.parseError);
        }
    } catch (ParseException ex) {
        LOG.error("httpresponse: " + queryResult + " \n" + ex.toString());
        apiResponse.setError(errors.parseError);
        return apiResponse;
    }
    return apiResponse;
}