Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b865a39
introduced a bug, reverting
JohannesJander Jan 30, 2020
f3b445f
Fix in BeanIterator
JohannesJander Jan 31, 2020
2b7f87d
TimeField was still using the JodaTime classes, switched to java.time…
JohannesJander Feb 4, 2020
8af3f31
Merge branch 'master' of https://github.com/frictionlessdata/tablesch…
JohannesJander Feb 12, 2020
6f3d996
BOM's weren't realiably stripped
JohannesJander Feb 12, 2020
5af03e7
Removed one source of NPE's
JohannesJander Feb 28, 2020
37a701f
Some fuckery with the OSGeo repos going on
JohannesJander Apr 16, 2020
6ff659c
NPE with getter-methods on bean class for BeanSchema which aren't dec…
JohannesJander Apr 20, 2020
10f30e6
Mapping of UUIDs to String fields in BeanSchema
JohannesJander Apr 20, 2020
4afdb47
replaces org.json with jackson
jdbranham Apr 23, 2020
23daba2
fix indentation
jdbranham Apr 23, 2020
e219fa9
organize imports
jdbranham Apr 23, 2020
dd6d949
remove commented code
jdbranham Apr 23, 2020
eae4e1e
fix indentation
jdbranham Apr 23, 2020
9202286
remove duplicative code
jdbranham Apr 23, 2020
abf2411
organize imports
jdbranham Apr 23, 2020
2368e3c
ignore temp file
jdbranham Apr 23, 2020
99e0d1e
don't initialize constraints during inferring
jdbranham Apr 24, 2020
6c8e653
replace `Field.forType` implementation with Jackson type inferrence
jdbranham Apr 24, 2020
05bf45a
decouple json library exceptions
jdbranham Apr 25, 2020
4e2c681
add option for already parsed json #performance
jdbranham Apr 25, 2020
3e8e79d
remove unecessary code
jdbranham Apr 25, 2020
6ea5749
use accurate exceptions
jdbranham Apr 26, 2020
2264057
log validation errors
jdbranham Apr 26, 2020
28f6d9a
unwrap text nodes
jdbranham Apr 27, 2020
7f11741
allows case insensitive enums
jdbranham Apr 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ hs_err_pid*



/bin/
/schema.json
37 changes: 25 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<hamcrest.version>1.3</hamcrest.version>
<apache-commons-lang3.version>3.6</apache-commons-lang3.version>
<apache-commons-csv.version>1.5</apache-commons-csv.version>
<jackson.version>2.10.3</jackson.version>
<everit-json-schema.version>1.5.1</everit-json-schema.version>
<joda-time.version>2.9.9</joda-time.version>
<junit.version>5.4.2</junit.version>
Expand All @@ -44,11 +45,7 @@
</repository>
<repository>
<id>osgeo.org</id>
<url>http://download.osgeo.org/webdav/geotools</url>
</repository>
<repository>
<id>boundlessgeo.com</id>
<url>https://repo.boundlessgeo.com/main/</url>
<url>https://repo.osgeo.org/repository/release/</url>
</repository>
</repositories>
<build>
Expand Down Expand Up @@ -204,20 +201,36 @@
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.8.8</version>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-opengis -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.geotools/gt-opengis
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>22.2</version>
</dependency> -->

<!-- https://mvnrepository.com/artifact/org.geotools/gt-opengis -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>23.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.geotools/gt-geometry -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geometry</artifactId>
<version>22.2</version>
<version>23.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.locationtech.jts/jts-core -->
<dependency>
<groupId>org.locationtech.jts</groupId>
Expand Down Expand Up @@ -258,12 +271,12 @@
</dependency>
<!-- JSON Schema Validator -->
<dependency>
<groupId>org.everit.json</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>${everit-json-schema.version}</version>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/io/frictionlessdata/tableschema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import io.frictionlessdata.tableschema.iterator.SimpleTableIterator;
import io.frictionlessdata.tableschema.iterator.TableIterator;
import io.frictionlessdata.tableschema.schema.Schema;
import io.frictionlessdata.tableschema.util.JsonUtil;
import io.frictionlessdata.tableschema.util.TableSchemaUtil;
import org.apache.commons.csv.CSVFormat;
import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URL;
import java.util.*;
Expand Down Expand Up @@ -278,20 +277,20 @@ public List<Object[]> read() throws Exception{
}
public String asJson() {
try {
JSONArray arr = new JSONArray();
List<Map<String, Object>> arr = new ArrayList<>();
List<Object[]> records = read();
Schema schema = (null != this.schema) ? this.schema : this.inferSchema();
for (Object[] rec : records) {
JSONObject obj = new JSONObject();
Map<String, Object> obj = new HashMap<>();
int i = 0;
for (Field field : schema.getFields()) {
Object s = rec[i];
obj.put(field.getName(), field.formatValueForJson(s));
i++;
}
arr.put(obj);
arr.add(obj);
}
return arr.toString();
return JsonUtil.getInstance().serialize(arr);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this asJson method be simplified, by annotating the class appropriately for Jackson to serialize it directly?

} catch (Exception ex) {
throw new RuntimeException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.frictionlessdata.tableschema.datasourceformat;

import io.frictionlessdata.tableschema.exception.TableSchemaException;
import io.frictionlessdata.tableschema.util.JsonUtil;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.net.URL;
Expand All @@ -31,16 +30,12 @@ public class CsvDataSourceFormat extends AbstractDataSourceFormat {
String content = br.lines().collect(Collectors.joining("\n"));
this.dataSource = DataSourceFormat.trimBOM(content);

// ensure that both parsing as JSON array and JSON object data fails. If one succeeds,
// ensure that both parsing as json fails. If it succeeds,
// then the data is not CSV, but JSON -> throw exception
try {
new JSONArray((String)this.dataSource);
} catch (JSONException ex) {
try {
new JSONObject((String)this.dataSource);
} catch (JSONException ex2) {
return;
}
JsonUtil.getInstance().readValue((String)this.dataSource);
} catch (Exception ex) {
return;
}
throw new IllegalArgumentException("Input seems to be in JSON format");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package io.frictionlessdata.tableschema.datasourceformat;

import io.frictionlessdata.tableschema.exception.TableSchemaException;
import io.frictionlessdata.tableschema.util.JsonUtil;

import org.apache.commons.csv.CSVFormat;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -84,13 +84,14 @@ public interface DataSourceFormat {
* @return DataSource created from input String
*/
static DataSourceFormat createDataSourceFormat(String input) {
String content = trimBOM(input);
try {
// JSON array generation only to see if an exception is thrown -> probably CSV data
new JSONArray(input);
return new JsonArrayDataSourceFormat(input);
} catch (JSONException ex) {
JsonUtil.getInstance().createArrayNode(content);
return new JsonArrayDataSourceFormat(content);
} catch (Exception ex) {
// JSON parsing failed, treat it as a CSV
return new CsvDataSourceFormat(input);
return new CsvDataSourceFormat(content);
}
}

Expand All @@ -104,68 +105,6 @@ static DataSourceFormat createDataSourceFormat(File input, File workDir) throws
return createDataSourceFormat(content);
}


/*static DataSourceFormat createDataSourceFormat(File input, File workDir) throws IOException {
InputStream is;
String content;
if (workDir.getAbsolutePath().toLowerCase().endsWith(".zip")) {
is = getZipFileInputStream(workDir.toPath(), input.getName());
} else {
Path resolvedPath = DataSourceFormat.toSecure(input.toPath(), workDir.toPath());
is = new FileInputStream(resolvedPath.toFile());
}
try (BufferedReader rdr = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8))) {
List<String> lines = rdr
.lines()
.collect(Collectors.toList());
content = String.join("\n", lines);
} catch (Exception ex) {
throw new TableSchemaException(ex);
}
return createDataSourceFormat(content);
}*/
/*
static InputStream getZipFileInputStream(Path inFilePath, String fileName) throws IOException {
// Read in memory the file inside the zip.
ZipFile zipFile = new ZipFile(inFilePath.toFile());
ZipEntry entry = findZipEntry(zipFile, fileName);

// Throw exception if expected datapackage.json file not found.
if(entry == null){
throw new TableSchemaException("The zip file does not contain the expected file: " + fileName);
}

return zipFile.getInputStream(entry);
}
*/
/**
* Take a ZipFile and look for the `filename` entry. If it is not on the top-level,
* look for directories and go into them (but only one level deep) and look again
* for the `filename` entry
* @param zipFile the ZipFile to use for looking for the `filename` entry
* @param fileName name of the entry we are looking for
* @return ZipEntry if found, null otherwise
*/
/*static ZipEntry findZipEntry(ZipFile zipFile, String fileName) {
ZipEntry entry = zipFile.getEntry(fileName);
if (null != entry)
return entry;
else {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (zipEntry.isDirectory()) {
entry = zipFile.getEntry(zipEntry.getName()+fileName);
if (null != entry)
return entry;
}
}
}
return null;
}
*/

static String getFileContents(String path, File workDir) throws IOException {
String lines;
if (workDir.getName().endsWith(".zip")) {
Expand Down Expand Up @@ -222,7 +161,6 @@ static DataSourceFormat createDataSourceFormat(InputStream input) throws IOExcep
try (BufferedReader rdr = new BufferedReader(fr)) {
content = rdr.lines().collect(Collectors.joining("\n"));
}
content = trimBOM(content);
} catch (IOException ex) {
throw ex;
}
Expand Down
Loading