Skip to content
This repository was archived by the owner on Dec 5, 2023. It is now read-only.

Commit e04c2fc

Browse files
committed
Documentation.
1 parent 9b1d0f2 commit e04c2fc

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@
2121
<version>1.1</version>
2222
</dependency>
2323
</dependencies>
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-javadoc-plugin</artifactId>
29+
<version>2.8</version>
30+
<configuration>
31+
</configuration>
32+
</plugin>
33+
</plugins>
34+
</build>
2435
</project>

src/main/java/com/rapportive/storm/scheme/SimpleJSONScheme.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,53 @@
1313
import backtype.storm.tuple.Fields;
1414

1515

16+
/**
17+
* Deserialisation scheme for JSON values using the json-simple library.
18+
* Emits one-element tuples with the field name <tt>object</tt>, containing
19+
* the parsed JSON value.
20+
*
21+
* <strong>N.B.</strong> if passed invalid JSON it will throw an
22+
* IllegalArgumentException.
23+
*
24+
* @author Sam Stokes ([email protected])
25+
* @see <a href="http://code.google.com/p/json-simple/">json-simple</a>
26+
*/
1627
public class SimpleJSONScheme implements Scheme {
1728
private static final long serialVersionUID = -7734176307841199017L;
1829

1930
private final String encoding;
2031

2132

33+
/**
34+
* Create a new JSON deserialisation scheme using the given character
35+
* encoding.
36+
*
37+
* @param encoding character encoding used to deserialise JSON from raw
38+
* bytes
39+
*/
2240
public SimpleJSONScheme(String encoding) {
2341
this.encoding = encoding;
2442
}
43+
/**
44+
* Create a new JSON deserialisation scheme using UTF-8 as the character
45+
* encoding.
46+
*/
2547
public SimpleJSONScheme() {
2648
this("UTF-8");
2749
}
2850

2951

52+
/**
53+
* Deserialise a JSON value from <tt>bytes</tt> using the requested
54+
* character encoding.
55+
*
56+
* @return a one-element tuple containing the parsed JSON value.
57+
*
58+
* @throws IllegalArgumentException if <tt>bytes</tt> does not contain
59+
* valid JSON encoded using the requested encoding.
60+
* @throws IllegalStateException if the requested character encoding is
61+
* not supported.
62+
*/
3063
@Override
3164
public List<Object> deserialize(byte[] bytes) {
3265
final String chars;
@@ -45,6 +78,9 @@ public List<Object> deserialize(byte[] bytes) {
4578
}
4679

4780

81+
/**
82+
* Emits tuples containing only one field, named "object".
83+
*/
4884
@Override
4985
public Fields getOutputFields() {
5086
return new Fields("object");

src/main/java/com/rapportive/storm/serializer/SimpleJSONSerializer.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,51 @@
1515

1616
import backtype.storm.serialization.ISerialization;
1717

18+
/**
19+
* Serialisation scheme for JSON values using the json-simple library.
20+
* Assumes the deserialised form will be a UTF-8-encoded string, and serialises
21+
* into that form.
22+
*
23+
* <strong>N.B.</strong> currently this only supports JSON objects and arrays
24+
* at the top level (though nested values are fine).
25+
*
26+
* <strong>N.B.</strong> if passed invalid JSON it will throw an
27+
* IllegalArgumentException.
28+
*
29+
* @author Sam Stokes ([email protected])
30+
* @see <a href="http://code.google.com/p/json-simple/">json-simple</a>
31+
*/
1832
public class SimpleJSONSerializer implements ISerialization<Object> {
33+
/**
34+
* Encoding used to serialise and deserialise between byte streams and
35+
* JSON structures.
36+
*
37+
* This is hard-coded to UTF-8 because the mechanism for registering a
38+
* serialisation does not allow passing arguments to the serialisation
39+
* constructor.
40+
*/
1941
public static final String ENCODING = "UTF-8";
2042

43+
/**
44+
* Returns whether this serialisation can handle the given type.
45+
*
46+
* @return <tt>true</tt> if c is {@link org.json.simple.JSONObject} or
47+
* {@link org.json.simple.JSONArray}.
48+
*/
2149
@SuppressWarnings("rawtypes")
2250
@Override
2351
public boolean accept(Class c) {
2452
return JSONObject.class.equals(c) ||
2553
JSONArray.class.equals(c);
2654
}
2755

56+
/**
57+
* Serialise a JSON object or array to the stream.
58+
*
59+
* @throws IllegalArgumentException if <tt>object</tt> is not a JSON
60+
* object or array
61+
* @throws IOException if there is an error writing to the stream.
62+
*/
2863
@Override
2964
public void serialize(Object object, DataOutputStream stream)
3065
throws IOException {
@@ -39,6 +74,12 @@ public void serialize(Object object, DataOutputStream stream)
3974
writer.flush();
4075
}
4176

77+
/**
78+
* Deserialise a JSON value from the stream.
79+
*
80+
* @throws IllegalArgumentException if unable to parse a JSON value from
81+
* the stream
82+
*/
4283
@Override
4384
public Object deserialize(DataInputStream stream) throws IOException {
4485
final Reader reader = new InputStreamReader(stream, ENCODING);

0 commit comments

Comments
 (0)