Skip to content

Commit fc7546e

Browse files
Fixes in FilebasedResource to better support nontabular data.
Fix for a bug in FilebasedResource returning the format.
1 parent 6d8ba3b commit fc7546e

File tree

5 files changed

+78
-19
lines changed

5 files changed

+78
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.frictionlessdata</groupId>
66
<artifactId>datapackage-java</artifactId>
7-
<version>0.9.6-SNAPSHOT</version>
7+
<version>0.9.7-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<issueManagement>
1010
<url>https://github.com/frictionlessdata/datapackage-java/issues</url>

src/main/java/io/frictionlessdata/datapackage/resource/AbstractResource.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -765,13 +765,15 @@ public void writeData(Writer out) throws Exception {
765765
@Override
766766
public void writeData(Path outputDir) throws Exception {
767767
Dialect lDialect = (null != dialect) ? dialect : Dialect.DEFAULT;
768-
List<Table> tables = getTables();
768+
boolean isNonTabular = ((profile != null) && (profile.equals(Profile.PROFILE_DATA_RESOURCE_DEFAULT)));
769+
isNonTabular = (isNonTabular | (null == serializationFormat));
770+
List<Table> tables = isNonTabular ? null : getTables();
771+
769772
Set<String> paths = getDatafileNamesForWriting();
770773

771774
int cnt = 0;
772775
for (String fName : paths) {
773776
String fileName = fName+"."+getSerializationFormat();
774-
Table t = tables.get(cnt++);
775777
Path p;
776778
FileSystem fileSystem = outputDir.getFileSystem();
777779
if (outputDir.toString().isEmpty()) {
@@ -784,27 +786,21 @@ public void writeData(Path outputDir) throws Exception {
784786
}
785787
Files.deleteIfExists(p);
786788

787-
// if the serializationFormat is set, serialize the data to JSON/CSV file
788-
if (null != serializationFormat) {
789+
790+
if (isNonTabular) {
791+
byte [] data = (byte[])this.getRawData();
792+
try (OutputStream out = Files.newOutputStream(p)) {
793+
out.write(data);
794+
}
795+
} else {
796+
Table t = tables.get(cnt++);
789797
try (Writer wr = Files.newBufferedWriter(p, StandardCharsets.UTF_8)) {
790798
if (serializationFormat.equals(TableDataSource.Format.FORMAT_CSV.getLabel())) {
791799
t.writeCsv(wr, lDialect.toCsvFormat());
792800
} else if (serializationFormat.equals(TableDataSource.Format.FORMAT_JSON.getLabel())) {
793801
wr.write(t.asJson());
794802
}
795803
}
796-
} else {
797-
// if serializationFormat is not set (probably non-tabular data), serialize the data to a binary file
798-
byte [] data = (byte[])this.getRawData();
799-
try (OutputStream out = Files.newOutputStream(p)) {
800-
out.write(data);
801-
}
802-
/*} else {
803-
try (FileOutputStream fos = new FileOutputStream(p.toFile())){
804-
fos.write(data);
805-
}
806-
}*/
807-
808804
}
809805
}
810806
}

src/main/java/io/frictionlessdata/datapackage/resource/FilebasedResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public FilebasedResource(String name, Collection<File> paths, File basePath) {
7474
public String getSerializationFormat() {
7575
if (null != serializationFormat)
7676
return serializationFormat;
77-
if (null == format) {
77+
if (null != format) {
7878
return format;
7979
}
8080
return sniffFormat(paths);

src/test/java/io/frictionlessdata/datapackage/resource/NonTabularResourceTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,70 @@ public void testUnCompressedDatapackageWithPdfResource() throws Exception {
351351
Assertions.assertEquals("application/pdf", readResource.getMediaType());
352352
}
353353

354+
@Test
355+
@DisplayName("Test creating ZIP-compressed datapackage with JSON ObjectNode as FilebasedResource")
356+
public void testCreateAndReadZippedPackageWithJsonObject() throws Exception {
357+
// Create temporary directories for source and output
358+
Path tempDirPath = Files.createTempDirectory("datapackage-source-");
359+
Path locPath = tempDirPath.resolve("data");
360+
Files.createDirectories(locPath);
361+
//Path tempOutputPath = Files.createTempDirectory("datapackage-output-");
362+
Path tempOutputPath = tempDirPath;
363+
364+
// Create an ObjectNode and write it to a temporary file
365+
ObjectMapper mapper = new ObjectMapper();
366+
ObjectNode jsonData = mapper.createObjectNode();
367+
jsonData.put("key", "value");
368+
jsonData.put("number", 123);
369+
byte[] jsonBytes = mapper.writeValueAsBytes(jsonData);
370+
371+
File jsonFile = new File(locPath.toFile(), "data.json");
372+
Files.write(jsonFile.toPath(), jsonBytes);
373+
374+
// Create a FilebasedResource for the JSON file
375+
FilebasedResource jsonResource = new FilebasedResource(
376+
"json-data",
377+
List.of(new File("data/data.json")),
378+
tempDirPath.toFile()
379+
);
380+
381+
// Set resource properties
382+
jsonResource.setProfile(Profile.PROFILE_DATA_RESOURCE_DEFAULT);
383+
jsonResource.setFormat("json");
384+
jsonResource.setMediaType("application/json");
385+
jsonResource.setEncoding("UTF-8");
386+
387+
// Create a package with the resource
388+
List<Resource> resources = new ArrayList<>();
389+
resources.add(jsonResource);
390+
Package pkg = new Package(resources);
391+
pkg.setName("json-package");
392+
393+
// Write the package to a compressed ZIP file
394+
File zipFile = new File(tempOutputPath.toFile(), "datapackage.zip");
395+
pkg.write(zipFile, true);
396+
397+
// Verify the ZIP file was created
398+
Assertions.assertTrue(zipFile.exists());
399+
Assertions.assertTrue(zipFile.length() > 0);
400+
401+
// Read the package back from the ZIP file
402+
Package readPackage = new Package(zipFile.toPath(), true);
403+
404+
// Verify package properties
405+
Assertions.assertEquals("json-package", readPackage.getName());
406+
Assertions.assertEquals(1, readPackage.getResources().size());
407+
408+
// Verify the JSON resource
409+
Resource<?> readResource = readPackage.getResource("json-data");
410+
Assertions.assertNotNull(readResource);
411+
Assertions.assertEquals("application/json", readResource.getMediaType());
412+
413+
// Verify the content of the resource
414+
byte[] readData = (byte[]) readResource.getRawData();
415+
Assertions.assertArrayEquals(jsonBytes, readData);
416+
}
417+
354418

355419
/**
356420
* A non-tabular resource

src/test/java/io/frictionlessdata/datapackage/resource/RoundtripTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ void validateCreateResourceDescriptorRoundtrip() throws Exception {
100100
Path tempDirPath = Files.createTempDirectory("datapackage-");
101101
File createdFile = new File(tempDirPath.toFile(), "test_save_datapackage.zip");
102102
pkg.write(createdFile, true);
103-
System.out.println(tempDirPath);
104103

105104
// create new Package from the serialized form and check they are equal
106105
Package testPkg = new Package(createdFile.toPath(), true);

0 commit comments

Comments
 (0)