Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 744bd69

Browse files
committed
JSON-P 1.1
Change-Id: Ifa3f1f84c0b2097dc0d1daa7e14f7fd17d83ad21
1 parent 8fa18c6 commit 744bd69

File tree

3 files changed

+110
-27
lines changed

3 files changed

+110
-27
lines changed

media/json-processing/src/main/java/org/glassfish/jersey/jsonp/JsonProcessingFeature.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -46,8 +46,8 @@
4646

4747
import org.glassfish.jersey.CommonProperties;
4848

49-
import org.glassfish.json.jaxrs.JsonStructureBodyReader;
50-
import org.glassfish.json.jaxrs.JsonStructureBodyWriter;
49+
import org.glassfish.json.jaxrs.JsonValueBodyReader;
50+
import org.glassfish.json.jaxrs.JsonValueBodyWriter;
5151

5252
/**
5353
* {@link Feature} used to register JSON-P providers.
@@ -65,8 +65,8 @@ public boolean configure(final FeatureContext context) {
6565

6666
// Make sure JSON-P workers have higher priority than other Json providers (in case there is a need to use JSON-P and some
6767
// other provider in an application).
68-
context.register(JsonStructureBodyReader.class, Priorities.USER + 1000);
69-
context.register(JsonStructureBodyWriter.class, Priorities.USER + 1000);
68+
context.register(JsonValueBodyReader.class, Priorities.USER + 1000);
69+
context.register(JsonValueBodyWriter.class, Priorities.USER + 1000);
7070

7171
return true;
7272
}

pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,6 @@
17961796
<id>java.net-releases</id>
17971797
<url>https://maven.java.net/content/repositories/releases</url>
17981798
</repository>
1799-
18001799
<repository>
18011800
<id>yasson-releases</id>
18021801
<name>Yasson Releases repository</name>
@@ -1883,8 +1882,8 @@
18831882
<jetty.servlet.api.25.version>6.1.14</jetty.servlet.api.25.version>
18841883
<jmh.version>1.10.2</jmh.version>
18851884
<jmockit.version>1.18</jmockit.version>
1886-
<jsonp.ri.version>1.0.4</jsonp.ri.version>
1887-
<jsonp.jaxrs.version>1.0</jsonp.jaxrs.version>
1885+
<jsonp.ri.version>1.1</jsonp.ri.version>
1886+
<jsonp.jaxrs.version>1.1.1</jsonp.jaxrs.version>
18881887
<jsp.version>2.0</jsp.version>
18891888
<jstl.version>1.2</jstl.version>
18901889
<jta.api.version>1.2</jta.api.version>

tests/e2e-entity/src/test/java/org/glassfish/jersey/tests/e2e/json/JsonProcessingTest.java

Lines changed: 103 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,46 @@
5353

5454
import javax.json.Json;
5555
import javax.json.JsonArray;
56+
import javax.json.JsonNumber;
5657
import javax.json.JsonObject;
58+
import javax.json.JsonString;
5759
import javax.json.JsonStructure;
60+
import javax.json.JsonValue;
5861

5962
import org.glassfish.jersey.jsonp.JsonProcessingFeature;
6063
import org.glassfish.jersey.server.JSONP;
6164
import org.glassfish.jersey.server.ResourceConfig;
6265
import org.glassfish.jersey.server.ServerProperties;
6366
import org.glassfish.jersey.test.JerseyTest;
6467
import org.glassfish.jersey.test.TestProperties;
65-
import org.glassfish.jersey.test.util.runner.ConcurrentRunner;
6668

6769
import org.junit.Test;
68-
import org.junit.runner.RunWith;
6970
import static org.hamcrest.CoreMatchers.is;
7071
import static org.junit.Assert.assertEquals;
7172
import static org.junit.Assert.assertThat;
7273

7374
/**
7475
* @author Michal Gajdos
7576
*/
76-
@RunWith(ConcurrentRunner.class)
77+
// @RunWith(ConcurrentRunner.class)
7778
public class JsonProcessingTest extends JerseyTest {
7879

79-
private static final String JSON_OBJECT_STR = "{\"foo\":\"bar\"}";
80-
private static final String JSON_ARRAY_STR = "[" + JSON_OBJECT_STR + "," + JSON_OBJECT_STR + "]";
80+
private static final String JSON_OBJECT_STR1 = "{\"foo\":\"bar\"}";
81+
private static final String JSON_OBJECT_STR2 = "{\"foo\": 12345}";
82+
private static final String JSON_ARRAY_STR1 = "[" + JSON_OBJECT_STR1 + "," + JSON_OBJECT_STR1 + "]";
83+
private static final String JSON_ARRAY_STR2 = "[" + JSON_OBJECT_STR2 + "," + JSON_OBJECT_STR2 + "]";
8184
private static final String JSON_ARRAY_VALUE_STR = "[null]";
8285

83-
private static final JsonObject JSON_OBJECT = Json.createReader(new StringReader(JSON_OBJECT_STR)).readObject();
84-
private static final JsonArray JSON_ARRAY = Json.createReader(new StringReader(JSON_ARRAY_STR)).readArray();
85-
private static final JsonArray JSON_ARRAY_VALUE = Json.createReader(new StringReader(JSON_ARRAY_VALUE_STR)).readArray();
86+
private static final JsonObject JSON_OBJECT = Json.createReader(new StringReader(JSON_OBJECT_STR1)).readObject();
87+
private static final JsonArray JSON_ARRAY = Json.createReader(new StringReader(JSON_ARRAY_STR1)).readArray();
88+
private static final JsonArray JSON_ARRAY_VALUE = Json.createReader(new StringReader(JSON_ARRAY_VALUE_STR))
89+
.readArray();
90+
91+
private static final JsonValue JSON_VALUE_BOOL = JsonValue.TRUE;
92+
private static final JsonString JSON_VALUE_STRING = Json.createReader(
93+
new StringReader(JSON_ARRAY_STR1)).readArray().getJsonObject(0).getJsonString("foo");
94+
private static final JsonNumber JSON_VALUE_NUMBER = Json.createReader(
95+
new StringReader(JSON_ARRAY_STR2)).readArray().getJsonObject(0).getJsonNumber("foo");
8696

8797
@Path("/")
8898
public static class Resource {
@@ -105,6 +115,24 @@ public JsonArray postJsonArray(final JsonArray jsonArray) {
105115
return jsonArray;
106116
}
107117

118+
@POST
119+
@Path("jsonValue")
120+
public JsonValue postJsonValue(final JsonValue jsonValue) {
121+
return jsonValue;
122+
}
123+
124+
@POST
125+
@Path("jsonString")
126+
public JsonString postJsonString(final JsonString jsonString) {
127+
return jsonString;
128+
}
129+
130+
@POST
131+
@Path("jsonNumber")
132+
public JsonValue postJsonNumber(final JsonNumber jsonNumber) {
133+
return jsonNumber;
134+
}
135+
108136
@GET
109137
@JSONP
110138
@Path("jsonObjectWithPadding")
@@ -134,7 +162,8 @@ public void testJsonObject() throws Exception {
134162

135163
@Test
136164
public void testJsonObjectAsString() throws Exception {
137-
final Response response = target("jsonObject").request(MediaType.APPLICATION_JSON).post(Entity.json(JSON_OBJECT_STR));
165+
final Response response = target("jsonObject").request(MediaType.APPLICATION_JSON)
166+
.post(Entity.json(JSON_OBJECT_STR1));
138167

139168
assertEquals(JSON_OBJECT, response.readEntity(JsonObject.class));
140169
}
@@ -148,7 +177,8 @@ public void testJsonObjectPlus() throws Exception {
148177

149178
@Test
150179
public void testJsonObjectAsStringPlus() throws Exception {
151-
final Response response = target("jsonObject").request("application/foo+json").post(Entity.json(JSON_OBJECT_STR));
180+
final Response response = target("jsonObject").request("application/foo+json")
181+
.post(Entity.json(JSON_OBJECT_STR1));
152182

153183
assertEquals(JSON_OBJECT, response.readEntity(JsonObject.class));
154184
}
@@ -162,7 +192,8 @@ public void testJsonObjectWrongTarget() throws Exception {
162192

163193
@Test
164194
public void testJsonObjectAsStringWrongTarget() throws Exception {
165-
final Response response = target("jsonArray").request(MediaType.APPLICATION_JSON).post(Entity.json(JSON_OBJECT_STR));
195+
final Response response = target("jsonArray").request(MediaType.APPLICATION_JSON)
196+
.post(Entity.json(JSON_OBJECT_STR1));
166197

167198
assertEquals(500, response.getStatus());
168199
}
@@ -176,7 +207,8 @@ public void testJsonObjectWrongEntity() throws Exception {
176207

177208
@Test
178209
public void testJsonObjectAsStringWrongEntity() throws Exception {
179-
final Response response = target("jsonObject").request(MediaType.APPLICATION_JSON).post(Entity.json(JSON_ARRAY_STR));
210+
final Response response = target("jsonObject").request(MediaType.APPLICATION_JSON)
211+
.post(Entity.json(JSON_ARRAY_STR1));
180212

181213
assertEquals(500, response.getStatus());
182214
}
@@ -191,7 +223,7 @@ public void testJsonObjectWrongMediaType() throws Exception {
191223
@Test
192224
public void testJsonObjectAsStringWrongMediaType() throws Exception {
193225
final Response response = target("jsonObject").request(MediaType.APPLICATION_OCTET_STREAM)
194-
.post(Entity.json(JSON_OBJECT_STR));
226+
.post(Entity.json(JSON_OBJECT_STR1));
195227

196228
assertEquals(500, response.getStatus());
197229
}
@@ -205,7 +237,8 @@ public void testJsonArray() throws Exception {
205237

206238
@Test
207239
public void testJsonArrayAsString() throws Exception {
208-
final Response response = target("jsonArray").request(MediaType.APPLICATION_JSON).post(Entity.json(JSON_ARRAY_STR));
240+
final Response response = target("jsonArray").request(MediaType.APPLICATION_JSON)
241+
.post(Entity.json(JSON_ARRAY_STR1));
209242

210243
assertEquals(JSON_ARRAY, response.readEntity(JsonArray.class));
211244
}
@@ -219,7 +252,8 @@ public void testJsonArrayPlus() throws Exception {
219252

220253
@Test
221254
public void testJsonArrayAsStringPlus() throws Exception {
222-
final Response response = target("jsonArray").request("application/foo+json").post(Entity.json(JSON_ARRAY_STR));
255+
final Response response = target("jsonArray").request("application/foo+json")
256+
.post(Entity.json(JSON_ARRAY_STR1));
223257

224258
assertEquals(JSON_ARRAY, response.readEntity(JsonArray.class));
225259
}
@@ -233,7 +267,8 @@ public void testJsonArrayWrongTarget() throws Exception {
233267

234268
@Test
235269
public void testJsonArrayAsStringWrongTarget() throws Exception {
236-
final Response response = target("jsonObject").request(MediaType.APPLICATION_JSON).post(Entity.json(JSON_ARRAY_STR));
270+
final Response response = target("jsonObject").request(MediaType.APPLICATION_JSON)
271+
.post(Entity.json(JSON_ARRAY_STR1));
237272

238273
assertEquals(500, response.getStatus());
239274
}
@@ -247,7 +282,8 @@ public void testJsonArrayWrongEntity() throws Exception {
247282

248283
@Test
249284
public void testJsonArrayAsStringWrongEntity() throws Exception {
250-
final Response response = target("jsonArray").request(MediaType.APPLICATION_JSON).post(Entity.json(JSON_OBJECT_STR));
285+
final Response response = target("jsonArray").request(MediaType.APPLICATION_JSON)
286+
.post(Entity.json(JSON_OBJECT_STR1));
251287

252288
assertEquals(500, response.getStatus());
253289
}
@@ -262,7 +298,7 @@ public void testJsonArrayWrongMediaType() throws Exception {
262298
@Test
263299
public void testJsonArraytAsStringWrongMediaType() throws Exception {
264300
final Response response = target("jsonArray").request(MediaType.APPLICATION_OCTET_STREAM)
265-
.post(Entity.json(JSON_ARRAY_STR));
301+
.post(Entity.json(JSON_ARRAY_STR1));
266302

267303
assertEquals(500, response.getStatus());
268304
}
@@ -288,11 +324,59 @@ public void testJsonStructureObject() throws Exception {
288324
assertEquals(JSON_OBJECT, response.readEntity(JsonStructure.class));
289325
}
290326

327+
@Test
328+
public void testJsonValueBool() throws Exception {
329+
final Response response = target("jsonValue").request(MediaType.APPLICATION_JSON)
330+
.post(Entity.json(JSON_VALUE_BOOL));
331+
332+
assertEquals(JSON_VALUE_BOOL, response.readEntity(JsonValue.class));
333+
}
334+
335+
@Test
336+
public void testJsonValueString() throws Exception {
337+
final Response response = target("jsonString").request(MediaType.APPLICATION_JSON)
338+
.post(Entity.json(JSON_VALUE_STRING));
339+
340+
assertEquals(JSON_VALUE_STRING, response.readEntity(JsonString.class));
341+
}
342+
343+
@Test
344+
public void testJsonValueStringAsValue() throws Exception {
345+
final Response response = target("jsonValue").request(MediaType.APPLICATION_JSON)
346+
.post(Entity.json(JSON_VALUE_STRING));
347+
348+
assertEquals(JSON_VALUE_STRING, response.readEntity(JsonString.class));
349+
}
350+
351+
@Test
352+
public void testJsonValueStringAsString() throws Exception {
353+
final Response response = target("jsonValue").request(MediaType.APPLICATION_JSON)
354+
.post(Entity.json("\"Red 5\""));
355+
356+
assertEquals("Red 5", response.readEntity(JsonString.class).getString());
357+
}
358+
359+
@Test
360+
public void testJsonValueNumber() throws Exception {
361+
final Response response = target("jsonNumber").request(MediaType.APPLICATION_JSON)
362+
.post(Entity.json(JSON_VALUE_NUMBER));
363+
364+
assertEquals(JSON_VALUE_NUMBER, response.readEntity(JsonNumber.class));
365+
}
366+
367+
@Test
368+
public void testJsonValueNumberAsValue() throws Exception {
369+
final Response response = target("jsonValue").request(MediaType.APPLICATION_JSON)
370+
.post(Entity.json(JSON_VALUE_NUMBER));
371+
372+
assertEquals(JSON_VALUE_NUMBER, response.readEntity(JsonNumber.class));
373+
}
374+
291375
@Test
292376
public void testJsonObjectWithPadding() throws Exception {
293377
final Response response = target("jsonObjectWithPadding").request("application/javascript").get();
294378

295379
assertThat(response.getStatus(), is(200));
296-
assertThat(response.readEntity(String.class), is(JSONP.DEFAULT_CALLBACK + "(" + JSON_OBJECT_STR + ")"));
380+
assertThat(response.readEntity(String.class), is(JSONP.DEFAULT_CALLBACK + "(" + JSON_OBJECT_STR1 + ")"));
297381
}
298382
}

0 commit comments

Comments
 (0)