Skip to content

Commit 5b4e704

Browse files
committed
example of Gson serialize and deserialize
1 parent 3d5343c commit 5b4e704

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

java-diff-utils/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@
2424
<artifactId>assertj-core</artifactId>
2525
<scope>test</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>com.google.code.gson</groupId>
29+
<artifactId>gson</artifactId>
30+
<version>2.8.7</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.core</groupId>
35+
<artifactId>jackson-core</artifactId>
36+
<version>2.12.3</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
40+
<dependency>
41+
<groupId>com.fasterxml.jackson.core</groupId>
42+
<artifactId>jackson-databind</artifactId>
43+
<version>2.12.3</version>
44+
<scope>test</scope>
45+
</dependency>
2746
</dependencies>
2847

2948
<build>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.github.difflib.examples;
2+
3+
import com.github.difflib.DiffUtils;
4+
import com.github.difflib.TestConstants;
5+
import com.github.difflib.patch.*;
6+
import com.google.gson.*;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.IOException;
10+
import java.lang.reflect.Type;
11+
import java.util.List;
12+
13+
import static com.github.difflib.GenerateUnifiedDiffTest.fileToLines;
14+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
15+
16+
public class GsonSerializeTest {
17+
List<String> origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt");
18+
List<String> revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt");
19+
20+
public GsonSerializeTest() throws IOException {
21+
}
22+
23+
@Test
24+
public void serialize() {
25+
Patch<String> diff = DiffUtils.diff(origLines, revLines);
26+
String json = new Gson().toJson(diff);
27+
System.out.println(json);
28+
}
29+
30+
@Test
31+
@SuppressWarnings("unchecked")
32+
public void deserialize() throws PatchFailedException {
33+
Patch<String> diff = DiffUtils.diff(origLines, revLines);
34+
String json = new Gson().toJson(diff);
35+
Patch<String> patch = new GsonBuilder()
36+
.registerTypeAdapter(Chunk.class, new ChunkDeserializer())
37+
.registerTypeAdapter(AbstractDelta.class, new AbstractDeltaDeserializer())
38+
.registerTypeAdapter(ConflictOutput.class, (JsonDeserializer<ConflictOutput<String>>) (_ignored1, _ignored2, _ignored3) -> null)
39+
.create()
40+
.fromJson(json, Patch.class);
41+
assertThat(patch.applyTo(origLines)).isEqualTo(revLines);
42+
}
43+
44+
public static class AbstractDeltaDeserializer implements JsonDeserializer<AbstractDelta<String>> {
45+
46+
@Override
47+
public AbstractDelta<String> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
48+
JsonObject object = json.getAsJsonObject();
49+
DeltaType type = DeltaType.valueOf(object.get("type").getAsString());
50+
Chunk<String> source = context.deserialize(object.get("source"), Chunk.class);
51+
Chunk<String> target = context.deserialize(object.get("target"), Chunk.class);
52+
switch (type) {
53+
case EQUAL:
54+
return new EqualDelta<>(source, target);
55+
case INSERT:
56+
return new InsertDelta<>(source, target);
57+
case DELETE:
58+
return new DeleteDelta<>(source, target);
59+
case CHANGE:
60+
return new ChangeDelta<>(source, target);
61+
}
62+
return null;
63+
}
64+
}
65+
66+
public static class ChunkDeserializer implements JsonDeserializer<Chunk<String>> {
67+
68+
@Override
69+
public Chunk<String> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
70+
JsonObject object = json.getAsJsonObject();
71+
int position = object.get("position").getAsInt();
72+
List<String> lines = context.deserialize(object.get("lines"), List.class);
73+
List<Integer> changePosition = context.deserialize(object.get("changePosition"), List.class);
74+
return new Chunk<>(position, lines, changePosition);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)