diff --git a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java index 8babea95..a437fd0c 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java @@ -6,6 +6,7 @@ import org.ethereum.core.Block; import org.ethereum.core.Blockchain; import org.ethereum.core.Genesis; import org.ethereum.crypto.HashUtil; +import org.ethereum.json.EtherObjectMapper; import org.ethereum.json.JSONHelper; import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; @@ -17,9 +18,6 @@ import org.iq80.leveldb.DBIterator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spongycastle.util.encoders.Hex; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -409,8 +407,7 @@ public class Repository { ObjectNode blockNode = jsonFactory.objectNode(); JSONHelper.dumpBlock(blockNode, block, gasUsed, this.getWorldState().getRootHash(), keys, this); - ObjectMapper mapper = new ObjectMapper(); - mapper.enable(SerializationFeature.INDENT_OUTPUT); + EtherObjectMapper mapper = new EtherObjectMapper(); bw.write(mapper.writeValueAsString(blockNode)); } catch (IOException e) { diff --git a/ethereumj-core/src/main/java/org/ethereum/json/EtherObjectMapper.java b/ethereumj-core/src/main/java/org/ethereum/json/EtherObjectMapper.java new file mode 100644 index 00000000..2304e40e --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/json/EtherObjectMapper.java @@ -0,0 +1,65 @@ +package org.ethereum.json; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.io.SegmentedStringWriter; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * An extended {@link com.fasterxml.jackson.databind.ObjectMapper ObjectMapper} class to + * customize ethereum state dumps. + * + * @author Alon Muroch + * + */ +public class EtherObjectMapper extends ObjectMapper { + + @Override + public String writeValueAsString(Object value) + throws JsonProcessingException { + // alas, we have to pull the recycler directly here... + SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler()); + try { + JsonGenerator ge = _jsonFactory.createGenerator(sw); + // set ethereum custom pretty printer + EtherPrettyPrinter pp = new EtherPrettyPrinter(); + ge.setPrettyPrinter(pp); + + _configAndWriteValue(ge, value); + } catch (JsonProcessingException e) { // to support [JACKSON-758] + throw e; + } catch (IOException e) { // shouldn't really happen, but is declared as possibility so: + throw JsonMappingException.fromUnexpectedIOE(e); + } + return sw.getAndClear(); + } + + /** + * An extended {@link com.fasterxml.jackson.core.util.DefaultPrettyPrinter} class to customize + * an ethereum {@link com.fasterxml.jackson.core.PrettyPrinter Pretty Printer} Generator + * + * @author Alon Muroch + * + */ + public class EtherPrettyPrinter extends DefaultPrettyPrinter { + + public EtherPrettyPrinter() { + super(); + } + + @Override + public void writeObjectFieldValueSeparator(JsonGenerator jg) + throws IOException, JsonGenerationException { + /** + * Custom object separator (Default is " : ") to make it easier to compare state dumps with other + * ethereum client implementations + */ + jg.writeRaw(": "); + } + } +}