forked from boskworks/bosk
-
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.
Add Jackson linked-map representation
- Loading branch information
Showing
5 changed files
with
201 additions
and
41 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
57 changes: 55 additions & 2 deletions
57
bosk-jackson/src/main/java/works/bosk/jackson/JacksonPluginConfiguration.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,9 +1,62 @@ | ||
package works.bosk.jackson; | ||
|
||
public record JacksonPluginConfiguration( | ||
boolean useLinkedEntries | ||
MapShape mapShape | ||
) { | ||
public static JacksonPluginConfiguration defaultConfiguration() { | ||
return new JacksonPluginConfiguration(false); | ||
return new JacksonPluginConfiguration(MapShape.ARRAY); | ||
} | ||
|
||
/** | ||
* How bosk's ordered maps should translate to JSON, where the order of object | ||
* fields is generally not preserved. | ||
*/ | ||
public enum MapShape { | ||
/** | ||
* A shape intended for brevity, human readability, and efficient serialization/deserialization. | ||
* <p> | ||
* An array of single-field objects, where the field name is the map entry's key | ||
* and the field value is the map entry's value. | ||
*/ | ||
ARRAY, | ||
|
||
/** | ||
* A shape intended for efficient incremental modification, | ||
* especially for values stored in a database that supports JSON | ||
* but does not preserve object field order (like Postgresql). | ||
* Inspired by {@link java.util.LinkedHashMap LinkedHashMap}. | ||
* <p> | ||
* An object containing the natural keys and values of the map being stored, | ||
* with a few changes: | ||
* | ||
* <ul> | ||
* <li> | ||
* The object also has fields "{@code -first}" and "{@code -last}" pointing | ||
* at the first and last map entries, so they can be found efficiently. | ||
* (These names have leading dashes to distinguish them from valid | ||
* {@link works.bosk.Identifier Identifier} values.) | ||
* </li> | ||
* <li> | ||
* The object's field values are themselves objects defined by | ||
* {@link works.bosk.jackson.JacksonPlugin.LinkedMapEntry LinkedMapEntry}. | ||
* </li> | ||
* </ul> | ||
* | ||
* The resulting structure supports the following operations in O(1) time: | ||
* <ul> | ||
* <li> | ||
* Lookup an entry given its ID | ||
* </li> | ||
* <li> | ||
* Add a new entry at the end. | ||
* </li> | ||
* <li> | ||
* Delete an entry. | ||
* </li> | ||
* </ul> | ||
* | ||
* It also supports linear time walking of the entries in both forward and reverse order. | ||
*/ | ||
LINKED_MAP, | ||
} | ||
} |
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
15 changes: 11 additions & 4 deletions
15
bosk-jackson/src/test/java/works/bosk/jackson/JacksonRoundTripConformanceTest.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,13 +1,20 @@ | ||
package works.bosk.jackson; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import java.util.stream.Stream; | ||
import works.bosk.drivers.DriverConformanceTest; | ||
import works.bosk.junit.ParametersByName; | ||
|
||
import static works.bosk.AbstractRoundTripTest.jacksonRoundTripFactory; | ||
import static works.bosk.jackson.JacksonPluginConfiguration.MapShape.LINKED_MAP; | ||
|
||
public class JacksonRoundTripConformanceTest extends DriverConformanceTest { | ||
@BeforeEach | ||
void setupDriverFactory() { | ||
driverFactory = jacksonRoundTripFactory(); | ||
@ParametersByName | ||
JacksonRoundTripConformanceTest(JacksonPluginConfiguration config) { | ||
driverFactory = jacksonRoundTripFactory(config); | ||
} | ||
|
||
static Stream<JacksonPluginConfiguration> config() { | ||
return Stream.of(JacksonPluginConfiguration.MapShape.values()) | ||
.map(shape -> new JacksonPluginConfiguration(shape)); | ||
} | ||
} |
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