1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.internal.tool;
  27 
  28 import java.io.PrintWriter;
  29 import java.util.List;
  30 
  31 import jdk.jfr.EventType;
  32 import jdk.jfr.ValueDescriptor;
  33 import jdk.jfr.consumer.RecordedEvent;
  34 import jdk.jfr.consumer.RecordedFrame;
  35 import jdk.jfr.consumer.RecordedObject;
  36 
  37 final class JSONWriter extends EventPrintWriter {
  38 
  39     private boolean first = true;
  40 
  41     public JSONWriter(PrintWriter writer) {
  42         super(writer);
  43     }
  44 
  45     @Override
  46     protected void printBegin() {
  47         printObjectBegin();
  48         printDataStructureName("recording");
  49         printObjectBegin();
  50         printDataStructureName("events");
  51         printArrayBegin();
  52     }
  53 
  54     @Override
  55     protected void print(List<RecordedEvent> events) {
  56         for (RecordedEvent event : events) {
  57             printNewDataStructure(first, true, null);
  58             printEvent(event);
  59             flush(false);
  60             first = false;
  61         }
  62     }
  63 
  64     @Override
  65     protected void printEnd() {
  66         printArrayEnd();;
  67         printObjectEnd();
  68         printObjectEnd();
  69     }
  70 
  71     private void printEvent(RecordedEvent event) {
  72         printObjectBegin();
  73         EventType type = event.getEventType();
  74         printValue(true, false, "type", type.getName());
  75         printNewDataStructure(false, false, "values");
  76         printObjectBegin();
  77         boolean first = true;
  78         for (ValueDescriptor v : event.getFields()) {
  79             printValueDescriptor(first, false, v, getValue(event, v));
  80             first = false;
  81         }
  82         printObjectEnd();
  83         printObjectEnd();
  84     }
  85 
  86     void printValue(boolean first, boolean arrayElement, String name, Object value) {
  87         printNewDataStructure(first, arrayElement, name);
  88         if (!printIfNull(value)) {
  89             if (value instanceof Boolean) {
  90                 printAsString(value);
  91                 return;
  92             }
  93             if (value instanceof Double) {
  94                 Double dValue = (Double) value;
  95                 if (Double.isNaN(dValue) || Double.isInfinite(dValue)) {
  96                     printNull();
  97                     return;
  98                 }
  99                 printAsString(value);
 100                 return;
 101             }
 102             if (value instanceof Float) {
 103                 Float fValue = (Float) value;
 104                 if (Float.isNaN(fValue) || Float.isInfinite(fValue)) {
 105                     printNull();
 106                     return;
 107                 }
 108                 printAsString(value);
 109                 return;
 110             }
 111             if (value instanceof Number) {
 112                 printAsString(value);
 113                 return;
 114             }
 115             print("\"");
 116             printEscaped(String.valueOf(value));
 117             print("\"");
 118         }
 119     }
 120 
 121     public void printObject(RecordedObject object) {
 122         printObjectBegin();
 123         boolean first = true;
 124         for (ValueDescriptor v : object.getFields()) {
 125             printValueDescriptor(first, false, v, getValue(object, v));
 126             first = false;
 127         }
 128         printObjectEnd();
 129     }
 130 
 131     private void printArray(ValueDescriptor v, Object[] array) {
 132         printArrayBegin();
 133         boolean first = true;
 134         int depth = 0;
 135         for (Object arrayElement : array) {
 136             if (!(arrayElement instanceof RecordedFrame) || depth < getStackDepth()) {
 137                 printValueDescriptor(first, true, v, arrayElement);
 138             }
 139             depth++;
 140             first = false;
 141         }
 142         printArrayEnd();
 143     }
 144 
 145     private void printValueDescriptor(boolean first, boolean arrayElement, ValueDescriptor vd, Object value) {
 146         if (vd.isArray() && !arrayElement) {
 147             printNewDataStructure(first, arrayElement, vd.getName());
 148             if (!printIfNull(value)) {
 149                 printArray(vd, (Object[]) value);
 150             }
 151             return;
 152         }
 153         if (!vd.getFields().isEmpty()) {
 154             printNewDataStructure(first, arrayElement, vd.getName());
 155             if (!printIfNull(value)) {
 156                 printObject((RecordedObject) value);
 157             }
 158             return;
 159         }
 160         printValue(first, arrayElement, vd.getName(), value);
 161     }
 162 
 163     private void printNewDataStructure(boolean first, boolean arrayElement, String name) {
 164         if (!first) {
 165             print(", ");
 166             if (!arrayElement) {
 167                 println();
 168             }
 169         }
 170         if (!arrayElement) {
 171             printDataStructureName(name);
 172         }
 173     }
 174 
 175     private boolean printIfNull(Object value) {
 176         if (value == null) {
 177             printNull();
 178             return true;
 179         }
 180         return false;
 181     }
 182 
 183     private void printNull() {
 184         print("null");
 185     }
 186 
 187     private void printDataStructureName(String text) {
 188         printIndent();
 189         print("\"");
 190         print(text);
 191         print("\": ");
 192     }
 193 
 194     private void printObjectEnd() {
 195         retract();
 196         println();
 197         printIndent();
 198         print("}");
 199     }
 200 
 201     private void printObjectBegin() {
 202         println("{");
 203         indent();
 204     }
 205 
 206     private void printArrayEnd() {
 207         print("]");
 208     }
 209 
 210     private void printArrayBegin() {
 211         print("[");
 212     }
 213 
 214     private void printEscaped(String text) {
 215         for (int i = 0; i < text.length(); i++) {
 216             printEscaped(text.charAt(i));
 217         }
 218     }
 219 
 220     private void printEscaped(char c) {
 221         if (c == '\b') {
 222             print("\\b");
 223             return;
 224         }
 225         if (c == '\n') {
 226             print("\\n");
 227             return;
 228         }
 229         if (c == '\t') {
 230             print("\\t");
 231             return;
 232         }
 233         if (c == '\f') {
 234             print("\\f");
 235             return;
 236         }
 237         if (c == '\r') {
 238             print("\\r");
 239             return;
 240         }
 241         if (c == '\"') {
 242             print("\\\"");
 243             return;
 244         }
 245         if (c == '\\') {
 246             print("\\\\");
 247             return;
 248         }
 249         if (c == '/') {
 250             print("\\/");
 251             return;
 252         }
 253         if (c > 0x7F || c < 32) {
 254             print("\\u");
 255             // 0x10000 will pad with zeros.
 256             print(Integer.toHexString(0x10000 + (int) c).substring(1));
 257             return;
 258         }
 259         print(c);
 260     }
 261 }