Merge pull request #90 from negedzuregal/parser

Custom json generator
This commit is contained in:
romanman 2014-08-24 17:50:02 +03:00
commit 5b48086ceb
2 changed files with 67 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import org.ethereum.core.Block;
import org.ethereum.core.Blockchain; import org.ethereum.core.Blockchain;
import org.ethereum.core.Genesis; import org.ethereum.core.Genesis;
import org.ethereum.crypto.HashUtil; import org.ethereum.crypto.HashUtil;
import org.ethereum.json.EtherObjectMapper;
import org.ethereum.json.JSONHelper; import org.ethereum.json.JSONHelper;
import org.ethereum.listener.EthereumListener; import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager; import org.ethereum.manager.WorldManager;
@ -17,9 +18,6 @@ import org.iq80.leveldb.DBIterator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex; 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.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
@ -409,8 +407,7 @@ public class Repository {
ObjectNode blockNode = jsonFactory.objectNode(); ObjectNode blockNode = jsonFactory.objectNode();
JSONHelper.dumpBlock(blockNode, block, gasUsed, this.getWorldState().getRootHash(), keys, this); JSONHelper.dumpBlock(blockNode, block, gasUsed, this.getWorldState().getRootHash(), keys, this);
ObjectMapper mapper = new ObjectMapper(); EtherObjectMapper mapper = new EtherObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
bw.write(mapper.writeValueAsString(blockNode)); bw.write(mapper.writeValueAsString(blockNode));
} catch (IOException e) { } catch (IOException e) {

View File

@ -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(": ");
}
}
}