View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.jsonTest;
16  
17  import ch.qos.logback.classic.encoder.JsonEncoder;
18  import ch.qos.logback.classic.spi.PubThrowableProxy;
19  import ch.qos.logback.classic.spi.StackTraceElementProxy;
20  import com.fasterxml.jackson.core.JacksonException;
21  import com.fasterxml.jackson.core.JsonParser;
22  import com.fasterxml.jackson.databind.DeserializationContext;
23  import com.fasterxml.jackson.databind.JsonNode;
24  import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
25  
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  public class PubThrowableProxyDeserializer  extends StdDeserializer<PubThrowableProxy>  {
31  
32      protected PubThrowableProxyDeserializer() {
33          this(null);
34      }
35      protected PubThrowableProxyDeserializer(Class<?> vc) {
36          super(vc);
37      }
38  
39      @Override
40      public PubThrowableProxy deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
41              throws IOException, JacksonException {
42          JsonNode node = jsonParser.getCodec().readTree(jsonParser);
43  
44          return jsonNodeThrowableProxy(node);
45      }
46  
47      static StackTraceElementProxy[] EMPTY_STEP_ARRAY = new StackTraceElementProxy[0];
48      static PubThrowableProxy[] EMPTY_PTP_ARRAY = new PubThrowableProxy[0];
49  
50      private static PubThrowableProxy jsonNodeThrowableProxy(JsonNode node) {
51          JsonNode classNameJN = node.get(JsonEncoder.CLASS_NAME_ATTR_NAME);
52          JsonNode messageJN = node.get(JsonEncoder.MESSAGE_ATTR_NAME);
53          JsonNode stepArrayJN = node.get(JsonEncoder.STEP_ARRAY_NAME_ATTRIBUTE);
54          JsonNode causeJN = node.get(JsonEncoder.CAUSE_ATTR_NAME);
55          JsonNode commonFramesCountJN = node.get(JsonEncoder.COMMON_FRAMES_COUNT_ATTR_NAME);
56  
57          JsonNode suppressedJN = node.get(JsonEncoder.SUPPRESSED_ATTR_NAME);
58  
59          PubThrowableProxy ptp = new PubThrowableProxy();
60          ptp.setClassName(classNameJN.textValue());
61          ptp.setMessage(messageJN.textValue());
62  
63          List<StackTraceElementProxy> stepList = stepNodeToList(stepArrayJN);
64          ptp.setStackTraceElementProxyArray(stepList.toArray(EMPTY_STEP_ARRAY));
65  
66          if(commonFramesCountJN != null) {
67              int commonFramesCount = commonFramesCountJN.asInt();
68              ptp.setCommonFramesCount(commonFramesCount);
69          }
70  
71          if(causeJN != null) {
72              PubThrowableProxy cause = jsonNodeThrowableProxy(causeJN);
73              ptp.setCause(cause);
74          }
75  
76          if(suppressedJN != null) {
77              //System.out.println("suppressedJN "+suppressedJN);
78              List<PubThrowableProxy>  ptpList = suppressedNodeToList(suppressedJN);
79              System.out.println("iiiiiiiiiiii");
80              System.out.println("ptpList="+ptpList);
81  
82              ptp.setSuppressed(ptpList.toArray(EMPTY_PTP_ARRAY));
83          }
84  
85          System.out.println("xxxxxxxxxxxxx");
86          System.out.println(ptp.getSuppressed());
87  
88          return ptp;
89      }
90  
91      private static List<StackTraceElementProxy> stepNodeToList(JsonNode stepArrayJN) {
92          List<StackTraceElementProxy> stepList = new ArrayList<>();
93          for(JsonNode jsonNode: stepArrayJN) {
94              StackTraceElementProxy step = STEPDeserializer.jsonNodeToSTEP(jsonNode);
95              stepList.add(step);
96          }
97          return stepList;
98      }
99  
100     private static List<PubThrowableProxy> suppressedNodeToList(JsonNode ptpArrayJN) {
101         List<PubThrowableProxy> ptpList = new ArrayList<>();
102         for(JsonNode jsonNode: ptpArrayJN) {
103             //System.out.println("---in  suppressedNodeToList seeing "+jsonNode);
104             PubThrowableProxy ptp = jsonNodeThrowableProxy(jsonNode);
105             //System.out.println("--in  suppressedNodeToList ptp="+ptp);
106             ptpList.add(ptp);
107         }
108         return ptpList;
109     }
110 }