Example usage for com.fasterxml.jackson.core JsonParser getValueAsDouble

List of usage examples for com.fasterxml.jackson.core JsonParser getValueAsDouble

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser getValueAsDouble.

Prototype

public double getValueAsDouble(double defaultValue) throws IOException, JsonParseException 

Source Link

Document

Method that will try to convert value of current token to a Java double.

Usage

From source file:org.zapto.samhippiemiddlepoolchecker.Values.java

PoolError update(String address, URL... urls) {
    //error is returned at the end
    PoolError error = PoolError.NONE;//from   w  ww.  ja v  a 2 s  .co m

    //Aborting the http to save data throws an error. This says if we should cover it up.
    boolean actualNetworkError = true;

    //reset the values. If they are never changed, then 0 is the most accurate number
    accepted = 0;
    rejected = 0;
    immature = 0;
    unexchanged = 0;
    balance = 0;
    paid = 0;

    try {
        //streaming the json
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(urls[0].toString());
        HttpResponse response = client.execute(request);
        InputStream in = response.getEntity().getContent();

        JsonFactory factory = new JsonFactory();
        JsonParser parser = factory.createParser(in);
        boolean addressFound = false;//see if we need to return and address not found error
        mainParse: //label for breaking when address is found
        while (parser.nextToken() != JsonToken.END_OBJECT)//finding "report"
        {
            if ("report".equals(parser.getCurrentName()))//beginning of report
            {
                boolean firstRun = true;
                while (parser.nextToken() == JsonToken.START_ARRAY)//each address has its own array
                {
                    if (firstRun)//this jumps over some junk at the begining
                    {
                        parser.nextToken();
                        firstRun = false;
                    }
                    parser.nextToken();//have to skip some junk each time
                    if (address.equals(parser.getText()))//we have found our address
                    {
                        addressFound = true;//this prevents an address not found error from being returned
                        while (parser.nextToken() != JsonToken.END_ARRAY) {
                            //getting each of our values from the array.
                            //having -420 as a default lets us see if the value is there while not using up 0

                            if ("megahashesPerSecond".equals(parser.getCurrentName())) {
                                float value = (float) parser.getValueAsDouble(-420);
                                if (value > 0)//negative means wrong value
                                {
                                    accepted = value;
                                }
                            }

                            if ("rejectedMegahashesPerSecond".equals(parser.getCurrentName())) {
                                float value = (float) parser.getValueAsDouble(-420);
                                if (value > 0)//negative means wrong value
                                {
                                    rejected = value;
                                }
                            }

                            if ("immatureBalance".equals(parser.getCurrentName())) {
                                float value = (float) parser.getValueAsDouble(-420);
                                if (value > 0)//negative means wrong value
                                {
                                    immature = value;
                                }
                            }

                            if ("unexchangedBalance".equals(parser.getCurrentName())) {
                                float value = (float) parser.getValueAsDouble(-420);
                                if (value > 0)//negative means wrong value
                                {
                                    unexchanged = value;
                                }
                            }

                            if ("bitcoinBalance".equals(parser.getCurrentName())) {
                                float value = (float) parser.getValueAsDouble(-420);
                                if (value > 0)//negative means wrong value
                                {
                                    balance = value;
                                }
                            }

                            if ("paidOut".equals(parser.getCurrentName())) {
                                float value = (float) parser.getValueAsDouble(-420);
                                if (value > 0)//negative means wrong value
                                {
                                    paid = value;
                                }
                            }
                        }
                        break mainParse;//no need to download any more addresses
                    } else {
                        while (parser.nextToken() != JsonToken.END_ARRAY) {
                        } //skipping over an unwanted address
                    }
                }
            }
        }
        if (!addressFound)//we never found an address
        {
            error = PoolError.ADDRESS;
        }
        actualNetworkError = false;
        request.abort();//should stop any extra data usage, also forces ioexception (which is ignored)
        parser.close();
        in.close();
    } catch (MalformedURLException e) {
        if (actualNetworkError) {
            error = PoolError.NETWORK;
        }
    } catch (IOException e) {
        if (actualNetworkError) {
            error = PoolError.NETWORK;
        }
    }
    return error;
}