-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
309 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ SPDX-FileCopyrightText: The Storage-Units Authors | ||
~ SPDX-License-Identifier: 0BSD | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> | ||
<!-- PARENT --> | ||
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> | ||
<!-- https://maven.apache.org/pom.html#Inheritance --> | ||
<parent> | ||
<groupId>wtf.metio.storage-units</groupId> | ||
<artifactId>storage-units.java</artifactId> | ||
<version>9999.99.99-SNAPSHOT</version> | ||
</parent> | ||
|
||
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> | ||
<!-- COORDINATES --> | ||
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> | ||
<!-- https://maven.apache.org/pom.html#Maven_Coordinates --> | ||
<artifactId>storage-units-gson</artifactId> | ||
|
||
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> | ||
<!-- INFORMATIONS --> | ||
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> | ||
<!-- https://maven.apache.org/pom.html#More_Project_Information --> | ||
<name>Storage Units :: GSON</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>wtf.metio.storage-units</groupId> | ||
<artifactId>storage-units-model</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jspecify</groupId> | ||
<artifactId>jspecify</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
/** | ||
* Module for GSON support. | ||
*/ | ||
@org.jspecify.nullness.NullMarked | ||
module wtf.metio.storageunits.gson { | ||
|
||
requires wtf.metio.storageunits.model; | ||
requires com.google.gson; | ||
requires org.jspecify; | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...e-units-gson/src/main/java/wtf/metio/storageunits/gson/BinaryStorageUnitDeserializer.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
package wtf.metio.storageunits.gson; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import wtf.metio.storageunits.model.StorageUnit; | ||
import wtf.metio.storageunits.model.StorageUnits; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Deserializes value back into binary storage units. | ||
*/ | ||
public final class BinaryStorageUnitDeserializer implements JsonDeserializer<StorageUnit<?>> { | ||
|
||
@Override | ||
public StorageUnit<?> deserialize( | ||
final JsonElement json, | ||
final Type typeOfT, | ||
final JsonDeserializationContext context) throws JsonParseException { | ||
return StorageUnits.parse(json.getAsJsonPrimitive().getAsString()).asBestMatchingBinaryUnit(); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...-units-gson/src/main/java/wtf/metio/storageunits/gson/DecimalStorageUnitDeserializer.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
package wtf.metio.storageunits.gson; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import wtf.metio.storageunits.model.StorageUnit; | ||
import wtf.metio.storageunits.model.StorageUnits; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Deserializes value back into decimal storage units. | ||
*/ | ||
public final class DecimalStorageUnitDeserializer implements JsonDeserializer<StorageUnit<?>> { | ||
|
||
@Override | ||
public StorageUnit<?> deserialize( | ||
final JsonElement json, | ||
final Type typeOfT, | ||
final JsonDeserializationContext context) throws JsonParseException { | ||
return StorageUnits.parse(json.getAsJsonPrimitive().getAsString()).asBestMatchingDecimalUnit(); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
storage-units-gson/src/main/java/wtf/metio/storageunits/gson/StorageUnitSerializer.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
|
||
package wtf.metio.storageunits.gson; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import wtf.metio.storageunits.model.StorageUnit; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Serializes storage units into strings. | ||
*/ | ||
public final class StorageUnitSerializer implements JsonSerializer<StorageUnit<?>> { | ||
|
||
@Override | ||
public JsonElement serialize( | ||
final StorageUnit<?> src, | ||
final Type typeOfSrc, | ||
final JsonSerializationContext context) { | ||
return new JsonPrimitive(src.inByte().toString()); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
storage-units-gson/src/main/java/wtf/metio/storageunits/gson/package-info.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
/** | ||
* Serialization support for GSON. | ||
* | ||
* @see <a href="https://github.com/google/gson">GSON Homepage</a> | ||
*/ | ||
package wtf.metio.storageunits.gson; |
37 changes: 37 additions & 0 deletions
37
storage-units-gson/src/test/java/wtf/metio/storageunits/gson/BinaryDeserializationTest.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
package wtf.metio.storageunits.gson; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import wtf.metio.storageunits.model.StorageUnit; | ||
|
||
class BinaryDeserializationTest { | ||
|
||
private Gson gson; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
gson = new GsonBuilder() | ||
.registerTypeHierarchyAdapter(StorageUnit.class, new BinaryStorageUnitDeserializer()) | ||
.create(); | ||
} | ||
|
||
@Test | ||
void deserializeStorageUnit() { | ||
// given | ||
final String input = "1024"; | ||
|
||
// when | ||
final StorageUnit<?> unit = gson.fromJson(input, StorageUnit.class); | ||
|
||
// then | ||
Assertions.assertEquals("1.00 KiB", unit.toString()); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
storage-units-gson/src/test/java/wtf/metio/storageunits/gson/DecimalDeserializationTest.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
package wtf.metio.storageunits.gson; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import wtf.metio.storageunits.model.StorageUnit; | ||
|
||
class DecimalDeserializationTest { | ||
|
||
private Gson gson; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
gson = new GsonBuilder() | ||
.registerTypeHierarchyAdapter(StorageUnit.class, new DecimalStorageUnitDeserializer()) | ||
.create(); | ||
} | ||
|
||
@Test | ||
void deserializeStorageUnit() { | ||
// given | ||
final String input = "1000"; | ||
|
||
// when | ||
final StorageUnit<?> unit = gson.fromJson(input, StorageUnit.class); | ||
|
||
// then | ||
Assertions.assertEquals("1.00 kB", unit.toString()); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
storage-units-gson/src/test/java/wtf/metio/storageunits/gson/StorageUnitSerializerTest.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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* SPDX-FileCopyrightText: The Storage-Units Authors | ||
* SPDX-License-Identifier: 0BSD | ||
*/ | ||
|
||
package wtf.metio.storageunits.gson; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import wtf.metio.storageunits.model.Kibibyte; | ||
import wtf.metio.storageunits.model.StorageUnit; | ||
import wtf.metio.storageunits.model.StorageUnits; | ||
|
||
class StorageUnitSerializerTest { | ||
|
||
private Gson gson; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
gson = new GsonBuilder() | ||
.registerTypeHierarchyAdapter(StorageUnit.class, new StorageUnitSerializer()) | ||
.create(); | ||
} | ||
|
||
@Test | ||
void serializeStorageUnit() { | ||
// given | ||
final StorageUnit<?> unit = StorageUnits.kibibyte(1L); | ||
|
||
// when | ||
final String output = gson.toJson(unit); | ||
|
||
// then | ||
Assertions.assertEquals("\"1024\"", output); | ||
} | ||
|
||
@Test | ||
void serializeNonStorageUnit() { | ||
// given | ||
final String input = "abc"; | ||
|
||
// when | ||
final String output = gson.toJson(input); | ||
|
||
// then | ||
Assertions.assertEquals("\"abc\"", output); | ||
} | ||
|
||
} |