Skip to content

Commit

Permalink
Upgraded Jackson library
Browse files Browse the repository at this point in the history
  • Loading branch information
subwiz committed Dec 6, 2018
1 parent 03c074a commit eae49cd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 47 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ repositories {
}

dependencies {
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'com.google.code.gson:gson:2.8.5'
compile 'net.sf.jopt-simple:jopt-simple:5.0.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.7',
'com.google.code.gson:gson:2.8.5',
'net.sf.jopt-simple:jopt-simple:5.0.4'
}

group = 'org.wiztools'
Expand Down
50 changes: 7 additions & 43 deletions src/main/java/org/wiztools/jsonvalidator/JacksonValidator.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
package org.wiztools.jsonvalidator;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.util.DefaultPrettyPrinter;

/**
*
* @author subwiz
*/
public class JacksonValidator implements JsonValidator {

private static final ObjectMapper jsonObjMapper = new ObjectMapper();
static {
jsonObjMapper.enable(DeserializationConfig.Feature.USE_BIG_DECIMAL_FOR_FLOATS);
jsonObjMapper.enable(DeserializationConfig.Feature.USE_BIG_INTEGER_FOR_INTS);
}

@Override
public String validate(Reader input, Config config) throws IOException, JsonParseException {
JsonFactory fac = new JsonFactory();
JsonParser parser = fac.createJsonParser(input);
JsonNode node = null;
try{
node = jsonObjMapper.readTree(parser);
}
catch(org.codehaus.jackson.JsonParseException ex){
ObjectMapper mapper = new ObjectMapper();
try {
Object jsonObject = mapper.readValue(input, Object.class);
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
} catch (com.fasterxml.jackson.core.JsonParseException ex) {
throw new JsonParseException(ex);
}
StringWriter out = new StringWriter();
// Create pretty printer:
JsonGenerator gen = fac.createJsonGenerator(out);

if(config.isPrettyPrint()) {
DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(new DefaultPrettyPrinter.Lf2SpacesIndenter());
gen.setPrettyPrinter(pp);
}

// Now write:
jsonObjMapper.writeTree(gen, node);

gen.flush();
gen.close();
return out.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static void printHelp(PrintStream out) {
out.println("Supported options are:");
out.println(" --help Display this help.");
out.println(" --noout Do not print formatted JSON to STDOUT.");
out.println(" --noformat Do not format JSON--print as single line.");
out.println(" --noformat Do not format JSON.");
out.println(" --gson Use Gson instead of default Jackson parser.");
}

Expand Down

0 comments on commit eae49cd

Please sign in to comment.