1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.jackson;
18
19 import java.io.IOException;
20
21 import com.fasterxml.jackson.core.JsonParser;
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.core.JsonToken;
24 import com.fasterxml.jackson.databind.DeserializationContext;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
27
28
29
30
31
32
33
34 public final class Log4jStackTraceElementDeserializer extends StdScalarDeserializer<StackTraceElement> {
35 private static final long serialVersionUID = 1L;
36
37
38
39
40 public Log4jStackTraceElementDeserializer() {
41 super(StackTraceElement.class);
42 }
43
44 @Override
45 public StackTraceElement deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException,
46 JsonProcessingException {
47 JsonToken t = jp.getCurrentToken();
48
49 if (t == JsonToken.START_OBJECT) {
50 String className = null, methodName = null, fileName = null;
51 int lineNumber = -1;
52
53 while ((t = jp.nextValue()) != JsonToken.END_OBJECT) {
54 final String propName = jp.getCurrentName();
55 if ("class".equals(propName)) {
56 className = jp.getText();
57 } else if ("file".equals(propName)) {
58 fileName = jp.getText();
59 } else if ("line".equals(propName)) {
60 if (t.isNumeric()) {
61 lineNumber = jp.getIntValue();
62 } else {
63
64 try {
65 lineNumber = Integer.parseInt(jp.getText().trim());
66 } catch (final NumberFormatException e) {
67 throw JsonMappingException.from(jp, "Non-numeric token (" + t + ") for property 'line'", e);
68 }
69 }
70 } else if ("method".equals(propName)) {
71 methodName = jp.getText();
72 } else if ("nativeMethod".equals(propName)) {
73
74 } else {
75 this.handleUnknownProperty(jp, ctxt, this._valueClass, propName);
76 }
77 }
78 return new StackTraceElement(className, methodName, fileName, lineNumber);
79 }
80 throw ctxt.mappingException(this._valueClass, t);
81 }
82 }