Skip to content

Commit

Permalink
added --noformat option
Browse files Browse the repository at this point in the history
  • Loading branch information
subwiz committed Jul 30, 2014
1 parent 6a37804 commit e3f4650
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/wiztools/jsonvalidator/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.wiztools.jsonvalidator;

/**
*
* @author subwiz
*/
public class Config {
private boolean prettyPrint = true;

public boolean isPrettyPrint() {
return prettyPrint;
}

public void setPrettyPrint(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
}

}
9 changes: 7 additions & 2 deletions src/main/java/org/wiztools/jsonvalidator/JsonValidate.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ public final class JsonValidate {

private JsonValidate() {}

public static String validate(Reader input) throws IOException, JsonSyntaxException {
public static String validate(Reader input,
Config config) throws IOException, JsonSyntaxException {
JsonParser parser = new JsonParser();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
GsonBuilder builder = new GsonBuilder();
if(config.isPrettyPrint()) {
builder.setPrettyPrinting();
}
Gson gson = builder.create();
JsonElement el = parser.parse(input);
String str = gson.toJson(el);
return str;
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/wiztools/jsonvalidator/JsonValidatorMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,31 @@ public static void main(String[] arg) throws IOException {
OptionParser cli = new OptionParser("h");
cli.accepts("help");
cli.accepts("noout");
cli.accepts("noformat");
OptionSet options = cli.parse(arg);

if(options.has("h") || options.has("help")) {
printHelp(System.out);
System.exit(0);
}

boolean printFormattedOut = true;
boolean printOut = true;
if(options.has("noout")) {
printFormattedOut = false;
printOut = false;
}

final Config config = new Config();
if(options.has("noformat")) {
config.setPrettyPrint(false);
}

List<String> files = (List<String>) options.nonOptionArguments();

if(files.isEmpty()) { // read from STDIN:
try(BufferedReader br = new BufferedReader(
new InputStreamReader(System.in))) {
final String out = JsonValidate.validate(br);
if(printFormattedOut) {
final String out = JsonValidate.validate(br, config);
if(printOut) {
if(System.console() != null) {
System.out.println();
}
Expand All @@ -60,8 +66,8 @@ public static void main(String[] arg) throws IOException {
for(String file: files) {
File f = new File(file);
try(FileReader fr = new FileReader(f)) {
final String out = JsonValidate.validate(fr);
if(printFormattedOut) {
final String out = JsonValidate.validate(fr, config);
if(printOut) {
System.out.println(out);
}
}
Expand Down

0 comments on commit e3f4650

Please sign in to comment.