Java源码示例:org.nd4j.shade.jackson.core.JsonProcessingException
示例1
@Override
public void serialize(ConfusionMatrix<Integer> cm, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
List<Integer> classes = cm.getClasses();
Map<Integer, Multiset<Integer>> matrix = cm.getMatrix();
Map<Integer, int[][]> m2 = new LinkedHashMap<>();
for (Integer i : matrix.keySet()) { //i = Actual class
Multiset<Integer> ms = matrix.get(i);
int[][] arr = new int[2][ms.size()];
int used = 0;
for (Integer j : ms.elementSet()) {
int count = ms.count(j);
arr[0][used] = j; //j = Predicted class
arr[1][used] = count; //prediction count
used++;
}
m2.put(i, arr);
}
gen.writeStartObject();
gen.writeObjectField("classes", classes);
gen.writeObjectField("matrix", m2);
gen.writeEndObject();
}
示例2
@Override
public int[] deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
JsonNode n = jp.getCodec().readTree(jp);
if(n.isArray()){
ArrayNode an = (ArrayNode)n;
int size = an.size();
int[] out = new int[size];
for( int i=0; i<size; i++ ){
out[i] = an.get(i).asInt();
}
return out;
} else if(n.isNumber()){
int v = n.asInt();
return new int[]{v,v};
} else {
throw new IllegalStateException("Could not deserialize value: " + n);
}
}
示例3
@Override
public DataFormat deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
String text = node.textValue();
switch (text){
case "NCHW":
return CNN2DFormat.NCHW;
case "NHWC":
return CNN2DFormat.NHWC;
case "NCW":
return RNNFormat.NCW;
case "NWC":
return RNNFormat.NWC;
default:
return null;
}
}
示例4
/**
* Convert the specified object to a YAML String, throwing an unchecked exception (RuntimeException) if conversion fails
*
* @param o Object
* @return Object as YAML
*/
public static String toYaml(@NonNull Object o) {
try {
return yaml().writeValueAsString(o);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting object of class " + o.getClass().getName() + " to YAML", e);
}
}
示例5
/**
* Convert the specified object to a JSON String, throwing an unchecked exception (RuntimeException) if conversion fails
*
* @param o Object
* @return Object as JSON
*/
public static String toJson(@NonNull Object o) {
try {
return json().writeValueAsString(o);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting object of class " + o.getClass().getName() + " to JSON", e);
}
}
示例6
default String toJson(){
try {
return ObjectMappers.json().writeValueAsString(this);
} catch (JsonProcessingException e){
throw new RuntimeException("Error serializing Data instance to JSON", e);
}
}
示例7
static Data fromJson(String json) {
try {
return ObjectMappers.json().readValue(json, Data.class);
} catch (JsonProcessingException e){
throw new RuntimeException("Error deserializing Data from JSON", e);
}
}
示例8
/**
* Convert the specified object to a YAML String, throwing an unchecked exception (RuntimeException) if conversion fails
*
* @param o Object
* @return Object as YAML
*/
public static String toYaml(@NonNull Object o) {
try {
return yaml().writeValueAsString(o);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting object of class " + o.getClass().getName() + " to YAML", e);
}
}
示例9
/**
* Convert the specified object to a JSON String, throwing an unchecked exception (RuntimeException) if conversion fails
*
* @param o Object
* @return Object as JSON
*/
public static String toJson(@NonNull Object o) {
try {
return json().writeValueAsString(o);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting object of class " + o.getClass().getName() + " to JSON", e);
}
}
示例10
@Override
public Point deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
JsonNode n = jp.getCodec().readTree(jp);
String lbl = n.has("label") ? n.get("label").textValue() : null;
Double prob = n.has("probability") ? n.get("probability").doubleValue() : null;
ArrayNode cn = (ArrayNode)n.get("coords");
double[] pts = new double[cn.size()];
for( int i=0; i<pts.length; i++ ){
pts[i] = cn.get(i).doubleValue();
}
return new NDPoint(pts, lbl, prob);
}
示例11
@Test
public void testEventsJson() throws JsonProcessingException {
String content = "[{\"name\":\"Runner\",\"cat\":\"START\",\"ts\":577532080904,\"pid\":17104,\"tid\":1,\"ph\":\"B\"},\n" +
"{\"name\":\"Runner\",\"cat\":\"END\",\"ts\":577532194829,\"pid\":17104,\"tid\":1,\"ph\":\"E\"},\n" +
"{\"name\":\"LoggingPipelineRunner\",\"cat\":\"START\",\"ts\":577532194878,\"pid\":17104,\"tid\":1,\"ph\":\"B\"},\n" +
"{\"name\":\"LoggingPipelineRunner\",\"cat\":\"END\",\"ts\":577532195804,\"pid\":17104,\"tid\":1,\"ph\":\"E\"},\n" +
"{\"name\":\"Runner\",\"cat\":\"START\",\"ts\":577532195829,\"pid\":17104,\"tid\":1,\"ph\":\"B\"}]";
TraceEvent[] events = new ObjectMapper().readValue(content, TraceEvent[].class);
assertEquals("Runner", events[0].getName());
assertEquals("LoggingPipelineRunner", events[2].getName());
}
示例12
/**
* Convert the ImageTransformProcess to a JSON string
*
* @return ImageTransformProcess, as JSON
*/
public String toJson() {
try {
return JsonMappers.getMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
//TODO better exceptions
throw new RuntimeException(e);
}
}
示例13
/**
* Convert the ImageTransformProcess to a YAML string
*
* @return ImageTransformProcess, as YAML
*/
public String toYaml() {
try {
return JsonMappers.getMapperYaml().writeValueAsString(this);
} catch (JsonProcessingException e) {
//TODO better exceptions
throw new RuntimeException(e);
}
}
示例14
public String writeValue(Object value) {
try {
return jacksonObjectMapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
示例15
/**
* Convert the TransformProcess to a JSON string
*
* @return TransformProcess, as JSON
*/
public String toJson() {
try {
return JsonMappers.getMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
//TODO proper exception message
throw new RuntimeException(e);
}
}
示例16
/**
* Convert the TransformProcess to a YAML string
*
* @return TransformProcess, as YAML
*/
public String toYaml() {
try {
return JsonMappers.getMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
//TODO proper exception message
throw new RuntimeException(e);
}
}
示例17
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
oos.writeObject(td);
oos.close();
byte[] bytes = baos.toByteArray();
Base64 b = new Base64();
String str = b.encodeAsString(bytes);
j.writeStartObject();
j.writeStringField("digest", str);
j.writeEndObject();
}
}
示例18
@Override
public TDigest deserialize(JsonParser jp, DeserializationContext d) throws IOException, JsonProcessingException {
JsonNode node = (JsonNode)jp.getCodec().readTree(jp);
String field = node.get("digest").asText();
Base64 b = new Base64();
byte[] bytes = b.decode(field);
try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))){
return (TDigest) ois.readObject();
} catch (Exception e){
throw new RuntimeException("Error deserializing TDigest object from JSON", e);
}
}
示例19
@Override
public DateTimeFieldType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String value = node.get("fieldType").textValue();
return map.get(value);
}
示例20
@Override
public void serialize(DateTimeFieldType dateTimeFieldType, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("fieldType", dateTimeFieldType.getName());
jsonGenerator.writeEndObject();
}
示例21
/**
* Serialize this updater as json
*
* @return
*/
@Override
public String toJson() {
try {
return objectMapper.writeValueAsString(status());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
示例22
/**
* Return a yaml configuration of this optimization configuration
*
* @return
*/
public String toYaml() {
try {
return JsonMapper.getYamlMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
示例23
/**
* Convert the ImageTransformProcess to a YAML string
*
* @return ImageTransformProcess, as YAML
*/
public String toYaml() {
try {
return JsonMappers.getMapperYaml().writeValueAsString(this);
} catch (JsonProcessingException e) {
//TODO better exceptions
throw new RuntimeException(e);
}
}
示例24
public String writeValue(Object value) {
try {
return jacksonObjectMapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
示例25
/**
* Convert the TransformProcess to a JSON string
*
* @return TransformProcess, as JSON
*/
public String toJson() {
try {
return JsonMappers.getMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
//TODO proper exception message
throw new RuntimeException(e);
}
}
示例26
/**
* Convert the TransformProcess to a YAML string
*
* @return TransformProcess, as YAML
*/
public String toYaml() {
try {
return JsonMappers.getMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
//TODO proper exception message
throw new RuntimeException(e);
}
}
示例27
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
oos.writeObject(td);
oos.close();
byte[] bytes = baos.toByteArray();
Base64 b = new Base64();
String str = b.encodeAsString(bytes);
j.writeStartObject();
j.writeStringField("digest", str);
j.writeEndObject();
}
}
示例28
@Override
public TDigest deserialize(JsonParser jp, DeserializationContext d) throws IOException, JsonProcessingException {
JsonNode node = (JsonNode)jp.getCodec().readTree(jp);
String field = node.get("digest").asText();
Base64 b = new Base64();
byte[] bytes = b.decode(field);
try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))){
return (TDigest) ois.readObject();
} catch (Exception e){
throw new RuntimeException("Error deserializing TDigest object from JSON", e);
}
}
示例29
@Override
public DateTimeFieldType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String value = node.get("fieldType").textValue();
return map.get(value);
}
示例30
@Override
public void serialize(DateTimeFieldType dateTimeFieldType, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("fieldType", dateTimeFieldType.getName());
jsonGenerator.writeEndObject();
}