I'm using the PubNub API with Java for pulling data from MtGox.
When retrieving data, the API delivers it in form of a JSONObject
, which represents a tree structure of JSON data. Trying to extract bits of information directly from a JSONObject
produces ugly code with lots of calls to getJSONObject(String)
, for which again exceptions might need to be handled.
Therefor, I'm looking for a convenient way to extract information from the JSONObject
responses. So far, I've come across the possibility to convert the JSONObject
into a POJO and then access the POJO. For conversion, I found the ObjectMapper
from the Jackson library, which does a nice job here:
public void successCallback(String channel, Object message) {
JSONObject messageJson = (JSONObject) message;
ObjectMapper mapper = new ObjectMapper();
Message myMessage = mapper.readValue(messageJson.toString(), Message.class);
// do stuff with myMessage here
}
This approach has the disadvantage that I have to write my own POJO classes, e.g. the Message class in the above example, because I could not find these classes ready to use anywhere.
How to conveniently access the information stored in the JSONObject
?
PubNub Java Class for MtGox JSON API
It's easy to create a ready made Java Class for ingesting the live feed provided by Mt.Gox This is a work-in-progress post to show you how to access the PubNub Data Feed from Mt.Gox as shown in the Dev Console live feed!
Official Bitcoin Wiki JSON Streaming API
We will be working from the Bitcoin wiki feed instructions provided by Bitcoin official Wiki: https://en.bitcoin.it/wiki/MtGox/API/Pubnub - continue reading below the screenshot to continue.
To see the live real-time data feed we will be using, please checkout the following two links:
PubNub Java SDK Docs
We will be using the PubNub Java SDK Docs http://www.pubnub.com/docs/java/javase/overview/data-push.html
Specifically we'll be using the
mtgox.subcribe(...)
instance method to focus our efforts which looks like the following:To compile the example got to https://github.com/pubnub/mtgox/tree/master/java and run
And then to RUN:
The Concept
For me, the best solution was to convert the
JSONObject
s from the PubNub API to bean classes which I found in the MtGox module of the XChange library.Admitted, this approach adds quite a bit of glue code as you can see at the end of this answer, but I think it's worth the trouble, because after the conversion, the code gets much simpler. E.g. for getting the rate and currency from the ticker at which BTC was last traded, you can simply write
and
How To Do It
The mtgox module of the XChange library is available as a maven artifact, which is very convenient. You only need to add this module as a dependency to your project and the project setup is done.
In the xchange-mtgox module, you will find the package com.xeiam.xchange.mtgox.v2.dto.marketdata with the two calsses
MtGoxTrade
andMtGoxTicker
.Converting from
JSONObject
to one of these classes is easy with the JacksonObjectMapper
. As an advantage, the Jackson library is automatically imported as a transitive dependency of the xchange-mtgox maven artifact. That means that if you're using maven, you don't even have to write a single line of code to add it to your project.Below is a complete runnable Example. Most is standard code for using PubNub. The important bits are between the marks
// BEGIN OF IMPORTANT PART
and// END OF IMPORTANT PART
.For clarity, I've removed the imports, which you can add back in with the appropriate hotkeys in most IDEs (it's Ctrl+Shift+O in Eclipse).
Morevoer, note that there is a performance penalty in this code, which can be avoided by following the answer to the question How to efficiently map a org.json.JSONObject to a POJO?