Coverage Summary for Class: Serializers (org.ethereum.vm.trace)

Class Method, % Line, %
Serializers 0% (0/6) 0% (0/21)
Serializers$ByteArraySerializer 0% (0/2) 0% (0/2)
Serializers$DataWordSerializer 0% (0/2) 0% (0/2)
Serializers$OpCodeSerializer 0% (0/2) 0% (0/2)
Total 0% (0/12) 0% (0/27)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) 5  * 6  * This program is free software: you can redistribute it and/or modify 7  * it under the terms of the GNU Lesser General Public License as published by 8  * the Free Software Foundation, either version 3 of the License, or 9  * (at your option) any later version. 10  * 11  * This program is distributed in the hope that it will be useful, 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14  * GNU Lesser General Public License for more details. 15  * 16  * You should have received a copy of the GNU Lesser General Public License 17  * along with this program. If not, see <http://www.gnu.org/licenses/>. 18  */ 19  20 package org.ethereum.vm.trace; 21  22 import co.rsk.panic.PanicProcessor; 23 import com.fasterxml.jackson.annotation.JsonAutoDetect; 24 import com.fasterxml.jackson.core.JsonGenerator; 25 import com.fasterxml.jackson.core.JsonProcessingException; 26 import com.fasterxml.jackson.databind.JsonSerializer; 27 import com.fasterxml.jackson.databind.ObjectMapper; 28 import com.fasterxml.jackson.databind.SerializationFeature; 29 import com.fasterxml.jackson.databind.SerializerProvider; 30 import com.fasterxml.jackson.databind.introspect.VisibilityChecker; 31 import org.ethereum.util.ByteUtil; 32 import org.ethereum.vm.DataWord; 33 import org.slf4j.Logger; 34 import org.slf4j.LoggerFactory; 35  36 import java.io.ByteArrayOutputStream; 37 import java.io.IOException; 38 import java.io.OutputStream; 39 import java.nio.charset.StandardCharsets; 40  41 public final class Serializers { 42  43  private static final Logger LOGGER = LoggerFactory.getLogger("vmtrace"); 44  private static final PanicProcessor panicProcessor = new PanicProcessor(); 45  46  public static class DataWordSerializer extends JsonSerializer<DataWord> { 47  48  @Override 49  public void serialize(DataWord gas, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { 50  jgen.writeString(gas.value().toString()); 51  } 52  } 53  54  public static class ByteArraySerializer extends JsonSerializer<byte[]> { 55  56  @Override 57  public void serialize(byte[] memory, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { 58  jgen.writeString(ByteUtil.toHexString(memory)); 59  } 60  } 61  62  public static class OpCodeSerializer extends JsonSerializer<Byte> { 63  64  @Override 65  public void serialize(Byte op, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { 66  jgen.writeString(org.ethereum.vm.OpCode.code(op).name()); 67  } 68  } 69  70  71  public static String serializeFieldsOnly(Object value, boolean pretty) { 72  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 73  try { 74  serializeFieldsOnly(value, pretty, baos); 75  return baos.toString(StandardCharsets.UTF_8.name()); 76  } catch (IOException e) { 77  LOGGER.error("JSON serialization error: ", e); 78  panicProcessor.panic("serialization", "JSON serialization error: " + e.toString()); 79  return "{}"; 80  } 81  } 82  83  public static void serializeFieldsOnly(Object value, boolean pretty, OutputStream out) throws IOException { 84  ObjectMapper mapper = createMapper(pretty); 85  mapper.setVisibility(fieldsOnlyVisibilityChecker(mapper)); 86  mapper.writeValue(out, value); 87  } 88  89  private static VisibilityChecker<?> fieldsOnlyVisibilityChecker(ObjectMapper mapper) { 90  return mapper.getSerializationConfig().getDefaultVisibilityChecker() 91  .withFieldVisibility(JsonAutoDetect.Visibility.ANY) 92  .withGetterVisibility(JsonAutoDetect.Visibility.NONE) 93  .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE); 94  } 95  96  public static ObjectMapper createMapper(boolean pretty) { 97  ObjectMapper mapper = new ObjectMapper(); 98  if (pretty) { 99  mapper.enable(SerializationFeature.INDENT_OUTPUT); 100  } 101  return mapper; 102  } 103 }