Skip to content

Commit ece2dd0

Browse files
committed
Implement convenient string conversion
1 parent b1442bc commit ece2dd0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/main/java/com/tananaev/jsonpatch/JsonPatch.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package com.tananaev.jsonpatch;
1818

19+
import com.google.gson.GsonBuilder;
1920
import com.google.gson.JsonElement;
21+
import com.tananaev.jsonpatch.gson.JsonPathSerializer;
2022
import com.tananaev.jsonpatch.operation.AbsOperation;
2123

2224
import java.util.LinkedList;
@@ -31,4 +33,11 @@ public JsonElement apply(JsonElement original) {
3133
return result;
3234
}
3335

36+
@Override
37+
public String toString() {
38+
GsonBuilder gsonBuilder = new GsonBuilder();
39+
gsonBuilder.registerTypeAdapter(JsonPath.class, new JsonPathSerializer());
40+
return gsonBuilder.create().toJson(this);
41+
}
42+
3443
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.tananaev.jsonpatch;
2+
3+
import com.google.gson.JsonPrimitive;
4+
import com.tananaev.jsonpatch.operation.ReplaceOperation;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class JsonPatchTest {
10+
11+
@Test
12+
public void toStringTest() throws Exception {
13+
JsonPatch patch = new JsonPatch();
14+
patch.add(new ReplaceOperation(new JsonPath("/object/key"), new JsonPrimitive("test")));
15+
assertEquals("[{\"value\":\"test\",\"op\":\"replace\",\"path\":\"/object/key\"}]", patch.toString());
16+
}
17+
18+
}

0 commit comments

Comments
 (0)