-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Serializers + Address + Sha256Hash + Transaction * Deserializers + Sha256Hash
- Loading branch information
1 parent
c15b456
commit 79d27ea
Showing
5 changed files
with
124 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...inj-rpcclient/src/main/java/com/msgilligan/bitcoinj/rpc/conversion/AddressSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.msgilligan.bitcoinj.rpc.conversion; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import org.bitcoinj.core.Address; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
*/ | ||
public class AddressSerializer extends JsonSerializer<Address> { | ||
@Override | ||
public void serialize(Address value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { | ||
gen.writeString(value.toString()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...pcclient/src/main/java/com/msgilligan/bitcoinj/rpc/conversion/Sha256HashDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.msgilligan.bitcoinj.rpc.conversion; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import org.bitcoinj.core.Coin; | ||
import org.bitcoinj.core.Sha256Hash; | ||
|
||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
|
||
/** | ||
* | ||
*/ | ||
public class Sha256HashDeserializer extends JsonDeserializer<Sha256Hash> { | ||
@Override | ||
public Sha256Hash deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { | ||
JsonToken token = p.getCurrentToken(); | ||
switch (token) { | ||
case VALUE_STRING: | ||
return Sha256Hash.wrap(p.getValueAsString()); | ||
default: | ||
throw ctxt.mappingException(Object.class, token); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...-rpcclient/src/main/java/com/msgilligan/bitcoinj/rpc/conversion/Sha256HashSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.msgilligan.bitcoinj.rpc.conversion; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import org.bitcoinj.core.Sha256Hash; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
*/ | ||
public class Sha256HashSerializer extends JsonSerializer<Sha256Hash> { | ||
@Override | ||
public void serialize(Sha256Hash value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { | ||
gen.writeString(value.toString()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...rpcclient/src/main/java/com/msgilligan/bitcoinj/rpc/conversion/TransactionSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.msgilligan.bitcoinj.rpc.conversion; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import org.bitcoinj.core.Transaction; | ||
|
||
import java.io.IOException; | ||
import java.util.Formatter; | ||
|
||
/** | ||
* | ||
*/ | ||
public class TransactionSerializer extends JsonSerializer<Transaction> { | ||
@Override | ||
public void serialize(Transaction value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { | ||
// From: http://bitcoin.stackexchange.com/questions/8475/how-to-get-hex-string-from-transaction-in-bitcoinj | ||
final StringBuilder sb = new StringBuilder(); | ||
Formatter formatter = new Formatter(sb); | ||
byte[] bytes = value.bitcoinSerialize(); | ||
for (byte b : bytes) { | ||
formatter.format("%02x", b); | ||
} | ||
formatter.close(); | ||
gen.writeString(sb.toString()); | ||
} | ||
} |