-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathExampleGsonSerialization.java
More file actions
133 lines (93 loc) · 3.43 KB
/
ExampleGsonSerialization.java
File metadata and controls
133 lines (93 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package modules.examples;
import common.parallelization.CallbackReceiver;
import modules.CharPipe;
import modules.ModuleImpl;
import modules.InputPort;
import modules.OutputPort;
import modules.Pipe;
import java.util.Iterator;
import java.util.Properties;
//Gson import
import com.google.gson.Gson;
import base.workbench.ModuleRunner;
public class ExampleGsonSerialization extends ModuleImpl {
// Main method for stand-alone execution
public static void main(String[] args) throws Exception {
ModuleRunner.runStandAlone(ExampleGsonSerialization.class, args);
}
//Add property keys:
/* no property keys */
//Add properties variables:
/* no property variables */
//Add variables:
private String inputString;
//Add I/O labels
private final String INPUTID = "string";
private final String OUTPUTID = "JSON";
//Add constructors:
public ExampleGsonSerialization (CallbackReceiver callbackReceiver,
Properties properties) throws Exception {
super(callbackReceiver, properties);
//Add description for properties
/* no property keys available */
//Add default values
this.getPropertyDefaultValues().put(ModuleImpl.PROPERTYKEY_NAME,
"Example Gson Serialization Module");
//Define I/O
InputPort inputPort = new InputPort (INPUTID, "plain text input", this);
inputPort.addSupportedPipe(CharPipe.class);
OutputPort outputPort = new OutputPort(OUTPUTID, "serialized JSON output", this);
outputPort.addSupportedPipe(CharPipe.class);
super.addInputPort(inputPort);
super.addOutputPort(outputPort);
//Add module description
this.setDescription("Example Module: Serializing a plain test string.<br/> Output is JSON format.");
// You can override the automatic category selection (for example if a module is to be shown in "deprecated")
//this.setCategory("Examples");
}
//Add methods:
//Add process() method:
@Override
public boolean process() throws Exception {
//initialize inputString
this.inputString = "";
//Variables used for input data
int bufferSize = 1024;
char [] bufferInput = new char [bufferSize];
// Read first characters
int charCode = this.getInputPorts().get(INPUTID).getInputReader().read(bufferInput, 0, bufferSize);
// Loop until no more data can be read
while (charCode != -1) {
// Check for interrupt signal
if (Thread.interrupted()) {
this.closeAllOutputs();
throw new InterruptedException("Thread has been interrupted.");
}
// Convert char array to string buffer
StringBuffer inputBuffer = new StringBuffer(new String (bufferInput).substring(0, charCode));
this.inputString += inputBuffer.toString();
// Read next charsplitWords
charCode = this.getInputPorts().get(INPUTID).getInputReader().read(bufferInput, 0, bufferSize);
}
//write JSON to output
Gson gson = new Gson();
Iterator<Pipe> charPipes = this.getOutputPorts().get(OUTPUTID).getPipes(CharPipe.class).iterator();
while (charPipes.hasNext()) {
gson.toJson(this.inputString, ((CharPipe)charPipes.next()).getOutput());
}
// close outputs
this.closeAllOutputs();
//success
return true;
}
//Add applyProperties() method:
@Override
public void applyProperties() throws Exception {
// Set defaults for properties not yet set
super.setDefaultsIfMissing();
// Apply own properties
/* no own properties */
// Apply parent object's properties (just the name variable actually)
super.applyProperties();
}
}