Skip to content

Commit 2b1f355

Browse files
committed
Experiments with json <-> Java mappings
1 parent 9c8a595 commit 2b1f355

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

src/test/java/org/oser/tools/jdbc/experiment/SelectAggregateAsJsonTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package org.oser.tools.jdbc.experiment;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
37
import com.github.benmanes.caffeine.cache.Cache;
8+
import org.junit.jupiter.api.Assertions;
49
import org.junit.jupiter.api.Test;
510
import org.oser.tools.jdbc.DbExporter;
611
import org.oser.tools.jdbc.DbRecord;
712
import org.oser.tools.jdbc.Fk;
813
import org.oser.tools.jdbc.TestHelpers;
14+
import org.oser.tools.jdbc.experiment.testbed.Book;
915

1016
import java.io.IOException;
1117
import java.sql.Connection;
@@ -25,8 +31,8 @@ void basicRequest() throws SQLException, IOException, ClassNotFoundException {
2531
DbRecord book = dbExporter.contentAsTree(connection, "book", 3);
2632

2733
Cache<String, List<Fk>> filteredFkCache = dbExporter.getFilteredFkCache();
28-
String sqlStatement = SelectAggregateAsJson.selectStatementForAggregateSelection("book", filteredFkCache, "oracle");
29-
// String sqlStatement = SelectAggregateAsJson.selectStatementForAggregateSelection("book", filteredFkCache, "postgres");
34+
// String sqlStatement = SelectAggregateAsJson.selectStatementForAggregateSelection("book", filteredFkCache, "oracle");
35+
String sqlStatement = SelectAggregateAsJson.selectStatementForAggregateSelection("book", filteredFkCache, "postgres");
3036

3137
System.out.println(sqlStatement);
3238

@@ -71,4 +77,21 @@ void basicRequestOracle() throws SQLException, IOException, ClassNotFoundExcepti
7177
invokeDbAndPrintJson(connection, sqlStatement);
7278
}
7379

80+
// can we map hierarchies easily? Y
81+
// does it work with arrays in json and single values in Java? Y
82+
// can we easily convert underscore_separated to camelCase names? Y
83+
@Test
84+
void deserializeJsonHierarchyToJava() throws JsonProcessingException {
85+
String s = "{\"id\": 3, \"title\": \"Slaughter House Five\", \"dms_id\": 2, \"author_id\": 3, \"number_pages\": 350, \"images\": [{\"id\":33, \"name\": \"authorPhoto\" }], \"author\": [{\"id\": 3, \"last_name\": \"Vonnegut\"}]}";
86+
87+
ObjectMapper mapper = new ObjectMapper();
88+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
89+
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
90+
mapper.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true);
91+
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
92+
Book bookFromJSON = mapper.readValue(s, Book.class);
93+
94+
Assertions.assertNotNull(bookFromJSON);
95+
System.out.println(bookFromJSON);
96+
}
7497
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.oser.tools.jdbc.experiment.testbed;
2+
3+
import lombok.Data;
4+
import lombok.ToString;
5+
6+
@ToString
7+
@Data
8+
public class Author {
9+
int id;
10+
String lastName;
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.oser.tools.jdbc.experiment.testbed;
2+
3+
import lombok.Data;
4+
import lombok.ToString;
5+
6+
import java.util.List;
7+
8+
@ToString
9+
@Data
10+
public class Book {
11+
int id;
12+
String title;
13+
int numberPages;
14+
15+
int dmsId;
16+
17+
Author author;
18+
19+
List<Image> images;
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.oser.tools.jdbc.experiment.testbed;
2+
3+
import lombok.Data;
4+
import lombok.ToString;
5+
6+
import java.util.List;
7+
8+
@ToString
9+
@Data
10+
public class Image {
11+
int id;
12+
String name;
13+
14+
}

0 commit comments

Comments
 (0)