From eae49cde06d56d9d522078000d9d53ba655d6806 Mon Sep 17 00:00:00 2001 From: Subhash Chandran Date: Thu, 6 Dec 2018 22:12:33 +0530 Subject: [PATCH] Upgraded Jackson library --- build.gradle | 6 +-- .../jsonvalidator/JacksonValidator.java | 50 +++---------------- .../jsonvalidator/JsonValidatorMain.java | 2 +- 3 files changed, 11 insertions(+), 47 deletions(-) diff --git a/build.gradle b/build.gradle index 7c54dbe..fa01f52 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/org/wiztools/jsonvalidator/JacksonValidator.java b/src/main/java/org/wiztools/jsonvalidator/JacksonValidator.java index e86a384..1c34c52 100644 --- a/src/main/java/org/wiztools/jsonvalidator/JacksonValidator.java +++ b/src/main/java/org/wiztools/jsonvalidator/JacksonValidator.java @@ -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(); } - } diff --git a/src/main/java/org/wiztools/jsonvalidator/JsonValidatorMain.java b/src/main/java/org/wiztools/jsonvalidator/JsonValidatorMain.java index 605e58f..17afaa5 100644 --- a/src/main/java/org/wiztools/jsonvalidator/JsonValidatorMain.java +++ b/src/main/java/org/wiztools/jsonvalidator/JsonValidatorMain.java @@ -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."); }