1 package org
.lttng
.scope
.tmf2
.views
.core
.timegraph
.view
.json
;
3 import java
.io
.IOException
;
5 import java
.nio
.file
.Files
;
6 import java
.nio
.file
.Path
;
7 import java
.nio
.file
.Paths
;
10 import org
.json
.JSONArray
;
11 import org
.json
.JSONException
;
12 import org
.json
.JSONObject
;
13 import org
.lttng
.scope
.tmf2
.views
.core
.timegraph
.model
.render
.states
.TimeGraphStateRender
;
15 import com
.google
.common
.base
.Charsets
;
16 import com
.google
.gson
.Gson
;
17 import com
.google
.gson
.GsonBuilder
;
19 final class RenderToJson
{
21 private RenderToJson() {}
23 private static final boolean PRETTY_PRINT
= true;
24 private static final int VERSION
= 1;
26 private static final String VERSION_KEY
= "version"; //$NON-NLS-1$
27 private static final String INTERVALS_KEY
= "intervals"; //$NON-NLS-1$
29 private static final String TREE_ELEMENT_KEY
= "elem"; //$NON-NLS-1$
30 private static final String START_TIME_KEY
= "start"; //$NON-NLS-1$
31 private static final String END_TIME_KEY
= "end"; //$NON-NLS-1$
32 private static final String STATE_NAME_KEY
= "state"; //$NON-NLS-1$
33 private static final String COLOR_KEY
= "color"; //$NON-NLS-1$
35 private static final Path OUTPUT_FILE
= Paths
.get("/home/alexandre/json-output"); //$NON-NLS-1$
38 if (Files
.exists(OUTPUT_FILE
)) {
39 Files
.delete(OUTPUT_FILE
);
41 Files
.createFile(OUTPUT_FILE
);
42 } catch (IOException e
) {
46 private static final Gson GSON
;
49 GSON
= new GsonBuilder().setPrettyPrinting().create();
55 public static void printRenderTo1(List
<TimeGraphStateRender
> renders
) {
56 String json
= GSON
.toJson(renders
);
57 try (Writer bw
= Files
.newBufferedWriter(OUTPUT_FILE
, Charsets
.UTF_8
)) {
60 } catch (IOException e1
) {
64 public static void printRenderTo(List
<TimeGraphStateRender
> renders
) {
65 try (Writer bw
= Files
.newBufferedWriter(OUTPUT_FILE
, Charsets
.UTF_8
)) {
66 JSONObject root
= new JSONObject();
67 root
.put(VERSION_KEY
, VERSION
);
69 JSONArray intervalsRoot
= new JSONArray();
70 root
.put(INTERVALS_KEY
, intervalsRoot
);
73 .flatMap(render
-> render
.getStateIntervals().stream())
74 .forEach(interval
-> {
76 JSONObject intervalObject
= new JSONObject();
77 intervalObject
.put(TREE_ELEMENT_KEY
, interval
.getStartEvent().getTreeElement().getName());
78 intervalObject
.put(START_TIME_KEY
, interval
.getStartEvent().getTimestamp());
79 intervalObject
.put(END_TIME_KEY
, interval
.getEndEvent().getTimestamp());
80 intervalObject
.put(STATE_NAME_KEY
, interval
.getStateName());
81 intervalObject
.put(COLOR_KEY
, interval
.getColorDefinition().toString());
83 intervalsRoot
.put(intervalObject
);
84 } catch (JSONException e
) {
85 /* Skip this interval */
89 String json
= (PRETTY_PRINT ? root
.toString(1) : root
.toString());
93 } catch (JSONException
| IOException e
) {