Commit 6ae541dc authored by Matija Obreza's avatar Matija Obreza
Browse files

Switched to JacksonJsonProvider using jackson-databind

parent 16002434
......@@ -85,6 +85,12 @@
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
<exclusions>
<exclusion>
<artifactId>json-smart</artifactId>
<groupId>net.minidev</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
......@@ -96,6 +102,11 @@
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.5</version>
</dependency>
</dependencies>
<profiles>
......
......@@ -22,9 +22,6 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
......@@ -34,11 +31,16 @@ import org.croptrust.excel.templates.ExcelToJSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Configuration.ConfigurationBuilder;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
/**
* The Class ExcelToJSONImpl.
......@@ -48,12 +50,19 @@ public class ExcelToJSONImpl implements ExcelToJSON {
/** The Constant LOG. */
private final static Logger LOG = LoggerFactory.getLogger(ExcelToJSONImpl.class);
/** The json path config. */
private final Configuration jsonPathConfig = Configuration.defaultConfiguration().addOptions(
Option.DEFAULT_PATH_LEAF_TO_NULL);
/** JsonPath configuration */
private static Configuration jsonPathConfig;
static {
jsonPathConfig = new ConfigurationBuilder().jsonProvider(new JacksonJsonProvider())
.mappingProvider(new JacksonMappingProvider()).options(Option.DEFAULT_PATH_LEAF_TO_NULL).build();
}
/* (non-Javadoc)
* @see org.croptrust.excel.templates.ExcelToJSON#readAsJson(org.apache.poi.ss.usermodel.Sheet, java.lang.String[], java.lang.String, boolean, boolean)
/*
* (non-Javadoc)
*
* @see org.croptrust.excel.templates.ExcelToJSON#readAsJson(org.apache.poi.ss.usermodel.Sheet, java.lang.String[],
* java.lang.String, boolean, boolean)
*/
@Override
public List<String> readAsJson(Sheet sheet, String[] columnMapping, String objectDefaults, boolean ignoreHeaderRow,
......@@ -68,14 +77,14 @@ public class ExcelToJSONImpl implements ExcelToJSON {
{
// Log defaults
DocumentContext defaults = JsonPath.parse(objectDefaults);
DocumentContext defaults = JsonPath.using(jsonPathConfig).parse(objectDefaults);
if (LOG.isTraceEnabled())
LOG.trace("Defaults: {}", defaults.jsonString());
// TODO Expand defaults object with all mappings defined
// Update objectDefaults
objectDefaults=defaults.jsonString();
objectDefaults = defaults.jsonString();
}
// Read rows
......@@ -190,7 +199,7 @@ public class ExcelToJSONImpl implements ExcelToJSON {
Object parent = obj.read(parentPath);
// System.err.println(parent);
if (parent == null || parent instanceof JSONArray) {
if (parent == null || parent instanceof ArrayNode) {
// convert to array
obj.set(parentPath, new HashMap<>());
// System.err.println("Converted "+ obj.jsonString());
......@@ -200,11 +209,11 @@ public class ExcelToJSONImpl implements ExcelToJSON {
// System.err.println(">+ " + obj.jsonString());
} else {
// Is an array index: [digit]
// make parent into array
Object parent = obj.read(parentPath);
// System.err.println(parent);
if (parent == null || parent instanceof JSONObject) {
if (parent == null || parent instanceof ObjectNode) {
// convert to array
obj.set(parentPath, new ArrayList<>());
// System.err.println("Converted "+ obj.jsonString());
......
......@@ -33,7 +33,7 @@ import org.junit.Test;
import com.jayway.jsonpath.JsonPath;
public class ExcelToJSONTest {
public class ExcelToJSONTest extends JsonPathTest {
private ExcelToJSON excelToJson = new ExcelToJSONImpl();
......
/*
* Copyright 2016 Global Crop Diversity Trust, www.croptrust.org
*
* 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 org.croptrust.excel.templates;
import java.util.EnumSet;
import java.util.Set;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
/**
* The Class JsonPathTest.
*/
public class JsonPathTest {
/**
* Instantiates a new json path test.
*/
public JsonPathTest() {
Configuration.setDefaults(new Configuration.Defaults() {
private final JsonProvider jsonProvider = new JacksonJsonProvider();
private final MappingProvider mappingProvider = new JacksonMappingProvider();
@Override
public JsonProvider jsonProvider() {
return jsonProvider;
}
@Override
public MappingProvider mappingProvider() {
return mappingProvider;
}
@Override
public Set<Option> options() {
return EnumSet.noneOf(Option.class);
}
});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment