ParseTest.java
/***************************************************************************
Copyright 2014 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.json;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ParseTest {
private static final Logger logger = LoggerFactory.getLogger(ParseTest.class);
private static final class TH implements Handler<Object> {
@Override public Handler start() {
logger.debug("Start");
return this;
}
@Override public Object finish() {
logger.debug("Close");
return null;
}
@Override public Handler startObject() {
logger.debug("{ - Begin Object");
return this;
}
@Override public Handler key(final String key) {
logger.debug("'" + key + "' - key.");
return this;
}
@Override public Handler endObject() {
logger.debug("} - End Object");
return this;
}
@Override public Handler startList() {
logger.debug("[ - Begin List");
return this;
}
@Override public Handler endList() {
logger.debug("] - End List");
return this;
}
@Override public Handler integer(final Long val) {
logger.debug("Integer: " + val.toString());
return this;
}
@Override public Handler decimal(final Double val) {
logger.debug("Decimal: " + val.toString());
return this;
}
@Override public Handler string(final String val) {
logger.debug("String: '" + val + "'");
return this;
}
@Override public Handler bool(final boolean val) {
logger.debug("Boolean: " + Boolean.toString(val));
return this;
}
@Override public Handler jsonNull() {
logger.debug("Null value.");
return this;
}
};
public static void main(final String[] args) throws Exception {
final StreamingParser parser = new StreamingParser();
try (final Reader in = new InputStreamReader(new FileInputStream(args[0]), "UTF-8")) {
parser.parse(in, new TH());
}
JsonObject j;
try (final Reader in = new InputStreamReader(new FileInputStream(args[0]), "UTF-8")) {
j = parser.parse(in, new JsonReader<JsonObject>());
logger.debug(j.toString());
}
final StringWriter out = new StringWriter();
JsonWriter w = new JsonWriter(out);
w.write(j);
logger.debug("Output: {}", out.toString());
}
}