|
| 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