consensusj
consensusj copied to clipboard
Add string representation for all POJOs
Let me quickly describe what issue I faced yesterday:
I wanted to dump some intermediate results, in particular of listUnspent, which returns a List of UnspentOutputs.
The change was basically:
@@ -166,2 +166,3 @@ class SendToOwnersReorgSpec extends BaseReorgSpec {
when: "creating a third STO transaction"
+ println listUnspent(0, 999999)
def thirdTxid = omniSendSTO(actorAddress, tokenID, 50.divisible)
However, the result:
[com.msgilligan.bitcoinj.json.pojo.UnspentOutput@3a13055a, ...]
It would be nice, if the UnspentOutput (and others) would be converted to something readable, such as:
[{"txid":"hex","vout":n,"address":"1base58",...}, ...]
The Groovy @ToString() class annotation may be useful?
If the POJOs were POGOs we could use the Groovy @toString annotation, but I think we want to keep the core functionality in straight Java.
I've heard good things about Project Lombok that has similar features (including a @toString annotation) for straight Java, but at the expense of additional compile-time and IDE tooling. I'm leaning against using it for this project, but would like to try it out on another project sometime. (There's also a prototype implementation of using Groovy annotations to make POJOs, see GROOVY-7492.)
I think there are two approaches worth considering:
-
Write
toString()methods by hand. -
Use the Jackson
ObjectMapperto convert to JSON String:println mapper.writeValueAsString(listUnspent(0, 999999))
We may be able to add some Groovy code to tests to make (2) more easily accessible or perhaps even automatic.