Skip to content

Commit 951abcb

Browse files
Sebastian Hoßsebhoss
authored andcommitted
add support for gson serialization
1 parent b06abac commit 951abcb

File tree

12 files changed

+309
-1
lines changed

12 files changed

+309
-1
lines changed

.idea/inspectionProfiles/storage-units.java.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,20 @@ public class HardDisk implements Serializable {
278278
}
279279
```
280280

281+
#### GSON
282+
283+
Use a [GSON](https://github.com/google/gson) serializer/deserializer like this:
284+
285+
```java
286+
import static wtf.metio.storageunits.gson.*;
287+
288+
new GsonBuilder()
289+
.registerTypeHierarchyAdapter(StorageUnit.class, new StorageUnitSerializer())
290+
.registerTypeHierarchyAdapter(StorageUnit.class, new BinaryStorageUnitDeserializer())
291+
.registerTypeHierarchyAdapter(StorageUnit.class, new DecimalStorageUnitDeserializer())
292+
.create();
293+
```
294+
281295
#### Jackson
282296

283297
Use the provided `StorageUnitModule` like this:
@@ -394,6 +408,14 @@ To use this project just declare the following dependency inside your POM:
394408
</dependency>
395409
<!-- EclipseLink ONLY -->
396410

411+
<!-- GSON ONLY -->
412+
<dependency>
413+
<groupId>wtf.metio.storage-units</groupId>
414+
<artifactId>storage-units-gson</artifactId>
415+
<version>${version.storage-units}</version>
416+
</dependency>
417+
<!-- GSON ONLY -->
418+
397419
<!-- Jackson ONLY -->
398420
<dependency>
399421
<groupId>wtf.metio.storage-units</groupId>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<modules>
4343
<module>storage-units-dozer</module>
4444
<module>storage-units-eclipselink</module>
45+
<module>storage-units-gson</module>
4546
<module>storage-units-jackson</module>
4647
<module>storage-units-jakarta</module>
4748
<module>storage-units-mapstruct</module>

storage-units-gson/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ SPDX-FileCopyrightText: The Storage-Units Authors
4+
~ SPDX-License-Identifier: 0BSD
5+
-->
6+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
11+
<!-- PARENT -->
12+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
13+
<!-- https://maven.apache.org/pom.html#Inheritance -->
14+
<parent>
15+
<groupId>wtf.metio.storage-units</groupId>
16+
<artifactId>storage-units.java</artifactId>
17+
<version>9999.99.99-SNAPSHOT</version>
18+
</parent>
19+
20+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
21+
<!-- COORDINATES -->
22+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
23+
<!-- https://maven.apache.org/pom.html#Maven_Coordinates -->
24+
<artifactId>storage-units-gson</artifactId>
25+
26+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
27+
<!-- INFORMATIONS -->
28+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
29+
<!-- https://maven.apache.org/pom.html#More_Project_Information -->
30+
<name>Storage Units :: GSON</name>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>wtf.metio.storage-units</groupId>
35+
<artifactId>storage-units-model</artifactId>
36+
<version>${project.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.google.code.gson</groupId>
40+
<artifactId>gson</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.jspecify</groupId>
44+
<artifactId>jspecify</artifactId>
45+
</dependency>
46+
</dependencies>
47+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
/**
6+
* Module for GSON support.
7+
*/
8+
@org.jspecify.nullness.NullMarked
9+
module wtf.metio.storageunits.gson {
10+
11+
requires wtf.metio.storageunits.model;
12+
requires com.google.gson;
13+
requires org.jspecify;
14+
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
package wtf.metio.storageunits.gson;
6+
7+
import com.google.gson.JsonDeserializationContext;
8+
import com.google.gson.JsonDeserializer;
9+
import com.google.gson.JsonElement;
10+
import com.google.gson.JsonParseException;
11+
import wtf.metio.storageunits.model.StorageUnit;
12+
import wtf.metio.storageunits.model.StorageUnits;
13+
14+
import java.lang.reflect.Type;
15+
16+
/**
17+
* Deserializes value back into binary storage units.
18+
*/
19+
public final class BinaryStorageUnitDeserializer implements JsonDeserializer<StorageUnit<?>> {
20+
21+
@Override
22+
public StorageUnit<?> deserialize(
23+
final JsonElement json,
24+
final Type typeOfT,
25+
final JsonDeserializationContext context) throws JsonParseException {
26+
return StorageUnits.parse(json.getAsJsonPrimitive().getAsString()).asBestMatchingBinaryUnit();
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
package wtf.metio.storageunits.gson;
6+
7+
import com.google.gson.JsonDeserializationContext;
8+
import com.google.gson.JsonDeserializer;
9+
import com.google.gson.JsonElement;
10+
import com.google.gson.JsonParseException;
11+
import wtf.metio.storageunits.model.StorageUnit;
12+
import wtf.metio.storageunits.model.StorageUnits;
13+
14+
import java.lang.reflect.Type;
15+
16+
/**
17+
* Deserializes value back into decimal storage units.
18+
*/
19+
public final class DecimalStorageUnitDeserializer implements JsonDeserializer<StorageUnit<?>> {
20+
21+
@Override
22+
public StorageUnit<?> deserialize(
23+
final JsonElement json,
24+
final Type typeOfT,
25+
final JsonDeserializationContext context) throws JsonParseException {
26+
return StorageUnits.parse(json.getAsJsonPrimitive().getAsString()).asBestMatchingDecimalUnit();
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
6+
package wtf.metio.storageunits.gson;
7+
8+
import com.google.gson.JsonElement;
9+
import com.google.gson.JsonPrimitive;
10+
import com.google.gson.JsonSerializationContext;
11+
import com.google.gson.JsonSerializer;
12+
import wtf.metio.storageunits.model.StorageUnit;
13+
14+
import java.lang.reflect.Type;
15+
16+
/**
17+
* Serializes storage units into strings.
18+
*/
19+
public final class StorageUnitSerializer implements JsonSerializer<StorageUnit<?>> {
20+
21+
@Override
22+
public JsonElement serialize(
23+
final StorageUnit<?> src,
24+
final Type typeOfSrc,
25+
final JsonSerializationContext context) {
26+
return new JsonPrimitive(src.inByte().toString());
27+
}
28+
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
/**
6+
* Serialization support for GSON.
7+
*
8+
* @see <a href="https://github.com/google/gson">GSON Homepage</a>
9+
*/
10+
package wtf.metio.storageunits.gson;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SPDX-FileCopyrightText: The Storage-Units Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
package wtf.metio.storageunits.gson;
6+
7+
import com.google.gson.Gson;
8+
import com.google.gson.GsonBuilder;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import wtf.metio.storageunits.model.StorageUnit;
13+
14+
class BinaryDeserializationTest {
15+
16+
private Gson gson;
17+
18+
@BeforeEach
19+
void setUp() {
20+
gson = new GsonBuilder()
21+
.registerTypeHierarchyAdapter(StorageUnit.class, new BinaryStorageUnitDeserializer())
22+
.create();
23+
}
24+
25+
@Test
26+
void deserializeStorageUnit() {
27+
// given
28+
final String input = "1024";
29+
30+
// when
31+
final StorageUnit<?> unit = gson.fromJson(input, StorageUnit.class);
32+
33+
// then
34+
Assertions.assertEquals("1.00 KiB", unit.toString());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)