-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
11 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 7 additions & 43 deletions
50
src/main/java/org/wiztools/jsonvalidator/JacksonValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters