Skip to content

Commit 1a9b317

Browse files
committed
Add JsonPointer#toString in api/impl/tests
Signed-off-by: leadpony <[email protected]>
1 parent 817010b commit 1a9b317

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

api/src/main/java/javax/json/JsonPointer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,13 @@ public interface JsonPointer {
113113
* @throws JsonException if the referenced value does not exist
114114
*/
115115
JsonValue getValue(JsonStructure target);
116+
117+
/**
118+
* Returns the string representation of this JSON Pointer.
119+
* The value to be returned is an empty string or a sequence of '{@code /}' prefixed tokens.
120+
*
121+
* @return the valid escaped JSON Pointer string.
122+
*/
123+
@Override
124+
String toString();
116125
}

impl/src/main/java/org/glassfish/json/JsonPointerImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ public JsonStructure remove(JsonStructure target) {
196196
return execute((r,v)->r.remove(), target, null);
197197
}
198198

199+
/**
200+
* {@inheritDoc}
201+
*/
202+
@Override
203+
public String toString() {
204+
return jsonPointer;
205+
}
206+
199207
/**
200208
* Executes the operation
201209
* @param op a {code BiFunction} used to specify the operation to execute on
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2015, 2017 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.json.tests;
18+
19+
import static org.junit.Assert.assertThat;
20+
import static org.hamcrest.CoreMatchers.equalTo;
21+
import static org.hamcrest.CoreMatchers.is;
22+
23+
import java.util.Arrays;
24+
25+
import javax.json.Json;
26+
import javax.json.JsonPointer;
27+
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.Parameterized;
31+
import org.junit.runners.Parameterized.Parameters;
32+
33+
/**
34+
* JSON pointer toString tests.
35+
*
36+
* @author leadpony
37+
*/
38+
@RunWith(Parameterized.class)
39+
public class JsonPointerToStringTest {
40+
41+
@Parameters(name = "{index}: {0}")
42+
public static Iterable<Object> data() {
43+
return Arrays.asList("", "/", "/one/two/3", "/a~1b", "/m~0n");
44+
}
45+
46+
private final String expected;
47+
48+
public JsonPointerToStringTest(String expected) {
49+
this.expected = expected;
50+
}
51+
52+
@Test
53+
public void shouldReturnOriginalEscapedString() {
54+
JsonPointer pointer = Json.createPointer(expected);
55+
assertThat(pointer.toString(), is(equalTo(expected)));
56+
}
57+
}

0 commit comments

Comments
 (0)