當前位置: 首頁>>代碼示例>>Java>>正文


Java Json類代碼示例

本文整理匯總了Java中javax.json.Json的典型用法代碼示例。如果您正苦於以下問題:Java Json類的具體用法?Java Json怎麽用?Java Json使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Json類屬於javax.json包,在下文中一共展示了Json類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: parseJsonString

import javax.json.Json; //導入依賴的package包/類
private static void parseJsonString() {

        JsonParser parser = Json.createParser(new StringReader(JSON));

        while (parser.hasNext()) {
            JsonParser.Event event = parser.next();
            switch (event) {
                case START_ARRAY:
                case END_ARRAY:
                case START_OBJECT:
                case END_OBJECT:
                case VALUE_FALSE:
                case VALUE_NULL:
                case VALUE_TRUE:
                    System.out.println(event.toString());
                    break;
                case KEY_NAME:
                    System.out.print(event.toString() + " " + parser.getString() + " - ");
                    break;
                case VALUE_STRING:
                case VALUE_NUMBER:
                    System.out.println(event.toString() + " " + parser.getString());
                    break;
            }
        }
    }
 
開發者ID:readlearncode,項目名稱:JSON-Processing-with-Java-EE,代碼行數:27,代碼來源:StreamingExample1.java

示例2: onOpen

import javax.json.Json; //導入依賴的package包/類
@OnOpen
public void onOpen(Session session, @PathParam("uuid") String uuid) {
    UUID key = UUID.fromString(uuid);
    peers.put(key, session);
    JsonArrayBuilder builder = Json.createArrayBuilder();
    for (StatusEventType statusEventType : StatusEventType.values()) {
        JsonObjectBuilder object = Json.createObjectBuilder();
        builder.add(object.add(statusEventType.name(), statusEventType.getMessage()).build());
    }

    RemoteEndpoint.Async asyncRemote = session.getAsyncRemote();
    asyncRemote.sendText(builder.build().toString());
    // Send pending messages
    List<String> messages = messageBuffer.remove(key);
    if (messages != null) {
        messages.forEach(asyncRemote::sendText);
    }
}
 
開發者ID:fabric8-launcher,項目名稱:launcher-backend,代碼行數:19,代碼來源:MissionControlStatusEndpoint.java

示例3: verifyInjectedIssuer

import javax.json.Json; //導入依賴的package包/類
@GET
@Path("/verifyInjectedIssuer")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed("Tester")
public JsonObject verifyInjectedIssuer(@QueryParam("iss") String iss) {
    boolean pass = false;
    String msg;
    String issValue = issuer.getString();
    if(issValue == null || issValue.length() == 0) {
        msg = Claims.iss.name()+"value is null or empty, FAIL";
    }
    else if(issValue.equals(iss)) {
        msg = Claims.iss.name()+" PASS";
        pass = true;
    }
    else {
        msg = String.format("%s: %s != %s", Claims.iss.name(), issValue, iss);
    }
    JsonObject result = Json.createObjectBuilder()
        .add("pass", pass)
        .add("msg", msg)
        .build();
    return result;
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:25,代碼來源:JsonValuejectionEndpoint.java

示例4: verifyInjectedIssuer

import javax.json.Json; //導入依賴的package包/類
@GET
@Path("/verifyInjectedIssuer")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedIssuer(@QueryParam("iss") String iss) {
    boolean pass = false;
    String msg;
    String issValue = issuer.get();
    if(issValue == null || issValue.length() == 0) {
        msg = Claims.iss.name()+"value is null or empty, FAIL";
    }
    else if(issValue.equals(iss)) {
        msg = Claims.iss.name()+" PASS";
        pass = true;
    }
    else {
        msg = String.format("%s: %s != %s", Claims.iss.name(), issValue, iss);
    }
    JsonObject result = Json.createObjectBuilder()
        .add("pass", pass)
        .add("msg", msg)
        .build();
    return result;
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:24,代碼來源:ProviderInjectionEndpoint.java

示例5: additionWithIncompleteInput

import javax.json.Json; //導入依賴的package包/類
@Test
public void additionWithIncompleteInput() {
    JsonObject input = Json.createObjectBuilder().
            add("b", 2).
            build();
    Response response = this.tut.
            request(MediaType.APPLICATION_JSON).
            post(json(input));
    assertThat(response.getStatus(), is(400));
    String cause = response.getHeaderString("cause");
    assertThat(cause, containsString(" a "));

    input = Json.createObjectBuilder().
            add("a", 2).
            build();
    response = this.tut.
            request(MediaType.APPLICATION_JSON).
            post(json(input));
    assertThat(response.getStatus(), is(400));
    cause = response.getHeaderString("cause");
    assertThat(cause, containsString(" b "));
}
 
開發者ID:AdamBien,項目名稱:javaee-calculator,代碼行數:23,代碼來源:AdditionResourceIT.java

示例6: find

import javax.json.Json; //導入依賴的package包/類
@GET
@Path("find")
@Produces(MediaType.APPLICATION_JSON)
public Response find() {
    JsonArray build = null;
    try {
        build = religionService.get().stream().map(h -> Json.createObjectBuilder()
                .add("id", h.getReligionId())
                .add("name", h.getName())
                .build())
                .collect(Json::createArrayBuilder, JsonArrayBuilder::add, JsonArrayBuilder::add)
                .build();
    } catch (Exception ex) {
        return Response.ok().header("Exception", ex.getMessage()).build();
    }
    return Response.ok().entity(build == null ? "No data found" : build).build();
}
 
開發者ID:Fatlonder,項目名稱:E-Clinic,代碼行數:18,代碼來源:ReligionRestEndPoint.java

示例7: verifyInjectedAudience

import javax.json.Json; //導入依賴的package包/類
@GET
@Path("/verifyInjectedAudience")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedAudience(@QueryParam("aud") String audience) {
    boolean pass = false;
    String msg;
    // aud
    Set<String> audValue = aud.get();
    if(audValue == null || audValue.size() == 0) {
        msg = Claims.aud.name()+"value is null or empty, FAIL";
    }
    else if(audValue.contains(audience)) {
        msg = Claims.aud.name()+" PASS";
        pass = true;
    }
    else {
        msg = String.format("%s: %s != %s", Claims.aud.name(), audValue, audience);
    }
    JsonObject result = Json.createObjectBuilder()
        .add("pass", pass)
        .add("msg", msg)
        .build();
    return result;
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:25,代碼來源:ProviderInjectionEndpoint.java

示例8: verifyInjectedCustomDouble

import javax.json.Json; //導入依賴的package包/類
@GET
@Path("/verifyInjectedCustomDouble")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomDouble(@QueryParam("value") Double value) {
    boolean pass = false;
    String msg;
    // iat
    JsonNumber customValue = customDouble.getValue();
    if(customValue == null) {
        msg = "customDouble value is null, FAIL";
    }
    else if(Math.abs(customValue.doubleValue() - value.doubleValue()) < 0.00001) {
        msg = "customDouble PASS";
        pass = true;
    }
    else {
        msg = String.format("customDouble: %s != %.8f", customValue, value);
    }
    JsonObject result = Json.createObjectBuilder()
        .add("pass", pass)
        .add("msg", msg)
        .build();
    return result;
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:25,代碼來源:ClaimValueInjectionEndpoint.java

示例9: verifyIssuedAt

import javax.json.Json; //導入依賴的package包/類
@GET
@Path("/verifyIssuedAt")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyIssuedAt(@QueryParam("iat") Long iat) {
    boolean pass = false;
    String msg;
    // iat
    Long iatValue = rawTokenJson.getIssuedAtTime();
    if (iatValue == null || iatValue.intValue() == 0) {
        msg = Claims.iat.name() + "value is null or empty, FAIL";
    }
    else if (iatValue.equals(iat)) {
        msg = Claims.iat.name() + " PASS";
        pass = true;
    }
    else {
        msg = String.format("%s: %s != %s", Claims.iat.name(), iatValue, iat);
    }
    JsonObject result = Json.createObjectBuilder()
            .add("pass", pass)
            .add("msg", msg)
            .build();
    return result;
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:25,代碼來源:RequiredClaimsEndpoint.java

示例10: verifyInjectedCustomDouble2

import javax.json.Json; //導入依賴的package包/類
@RunAsClient
@Test(groups = TEST_GROUP_CDI_JSON,
    description = "Verify that the injected customDouble claim is as expected")
public void verifyInjectedCustomDouble2() throws Exception {
    Reporter.log("Begin verifyInjectedCustomDouble2\n");
    String token2 = TokenUtils.generateTokenString("/Token2.json");
    String uri = baseURL.toExternalForm() + "/endp/verifyInjectedCustomDouble";
    WebTarget echoEndpointTarget = ClientBuilder.newClient()
        .target(uri)
        .queryParam("value", 3.241592653589793)
        .queryParam(Claims.auth_time.name(), authTimeClaim);
    Response response = echoEndpointTarget.request(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, "Bearer " + token2).get();
    Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
    String replyString = response.readEntity(String.class);
    JsonReader jsonReader = Json.createReader(new StringReader(replyString));
    JsonObject reply = jsonReader.readObject();
    Reporter.log(reply.toString());
    Assert.assertTrue(reply.getBoolean("pass"), reply.getString("msg"));
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:20,代碼來源:JsonValueInjectionTest.java

示例11: verifyInjectedJTI

import javax.json.Json; //導入依賴的package包/類
@RunAsClient
@Test(groups = TEST_GROUP_CDI_PROVIDER,
    description = "Verify that the injected jti claim is as expected")
public void verifyInjectedJTI() throws Exception {
    Reporter.log("Begin verifyInjectedJTI\n");
    String uri = baseURL.toExternalForm() + "/endp/verifyInjectedJTI";
    WebTarget echoEndpointTarget = ClientBuilder.newClient()
        .target(uri)
        .queryParam(Claims.jti.name(), "a-123")
        .queryParam(Claims.auth_time.name(), authTimeClaim);
    Response response = echoEndpointTarget.request(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get();
    Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
    String replyString = response.readEntity(String.class);
    JsonReader jsonReader = Json.createReader(new StringReader(replyString));
    JsonObject reply = jsonReader.readObject();
    Reporter.log(reply.toString());
    Assert.assertTrue(reply.getBoolean("pass"), reply.getString("msg"));
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:19,代碼來源:PrimitiveInjectionTest.java

示例12: verifyIssuerClaim

import javax.json.Json; //導入依賴的package包/類
@RunAsClient
@Test(groups = TEST_GROUP_JWT,
        description = "Verify that the token issuer claim is as expected")
public void verifyIssuerClaim() throws Exception {
    Reporter.log("Begin verifyIssuerClaim");
    String uri = baseURL.toExternalForm() + "/endp/verifyIssuer";
    WebTarget echoEndpointTarget = ClientBuilder.newClient()
            .target(uri)
            .queryParam(Claims.iss.name(), TEST_ISSUER)
            .queryParam(Claims.auth_time.name(), authTimeClaim);
    Response response = echoEndpointTarget.request(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get();
    Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
    String replyString = response.readEntity(String.class);
    JsonReader jsonReader = Json.createReader(new StringReader(replyString));
    JsonObject reply = jsonReader.readObject();
    Reporter.log(reply.toString());
    Assert.assertTrue(reply.getBoolean("pass"), reply.getString("msg"));
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:19,代碼來源:RequiredClaimsTest.java

示例13: verifyInjectedUPN

import javax.json.Json; //導入依賴的package包/類
@RunAsClient
@Test(groups = TEST_GROUP_CDI_PROVIDER,
    description = "Verify that the injected upn claim is as expected")
public void verifyInjectedUPN() throws Exception {
    Reporter.log("Begin verifyInjectedUPN\n");
    String uri = baseURL.toExternalForm() + "/endp/verifyInjectedUPN";
    WebTarget echoEndpointTarget = ClientBuilder.newClient()
        .target(uri)
        .queryParam(Claims.upn.name(), "[email protected]")
        .queryParam(Claims.auth_time.name(), authTimeClaim);
    Response response = echoEndpointTarget.request(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get();
    Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
    String replyString = response.readEntity(String.class);
    JsonReader jsonReader = Json.createReader(new StringReader(replyString));
    JsonObject reply = jsonReader.readObject();
    Reporter.log(reply.toString());
    Assert.assertTrue(reply.getBoolean("pass"), reply.getString("msg"));
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:19,代碼來源:PrimitiveInjectionTest.java

示例14: verifyInjectedIssuedAtStandard

import javax.json.Json; //導入依賴的package包/類
@RunAsClient
@Test(groups = TEST_GROUP_CDI,
    description = "Verify that the injected iat claim using @Claim(standard) is as expected")
public void verifyInjectedIssuedAtStandard() throws Exception {
    Reporter.log("Begin verifyInjectedIssuedAtStandard\n");
    String uri = baseURL.toExternalForm() + "/endp/verifyInjectedIssuedAtStandard";
    WebTarget echoEndpointTarget = ClientBuilder.newClient()
        .target(uri)
        .queryParam(Claims.iat.name(), iatClaim)
        .queryParam(Claims.auth_time.name(), authTimeClaim);
    Response response = echoEndpointTarget.request(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get();
    Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
    String replyString = response.readEntity(String.class);
    JsonReader jsonReader = Json.createReader(new StringReader(replyString));
    JsonObject reply = jsonReader.readObject();
    Reporter.log(reply.toString());
    Assert.assertTrue(reply.getBoolean("pass"), reply.getString("msg"));
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:19,代碼來源:ClaimValueInjectionTest.java

示例15: getJsonObjectBuilder

import javax.json.Json; //導入依賴的package包/類
@Override
public JsonObjectBuilder getJsonObjectBuilder() {
    JsonObjectBuilder jsonObjectBuilder =
            Json.createObjectBuilder()
                    .add("transportId", this.transportId)
                    .add("localCandidateId", this.localCandidateId)
                    .add("remoteCandidateId", this.remoteCandidateId)
                    .add("state", this.state)
                    .add("priority", this.priority)
                    .add("nominated", this.nominated)
                    .add("bytesSent", this.bytesSent)
                    .add("currentRoundTripTime", this.currentRoundTripTime)
                    .add("totalRoundTripTime", this.totalRoundTripTime)
                    .add("bytesReceived", this.bytesReceived);
    return jsonObjectBuilder;
}
 
開發者ID:webrtc,項目名稱:KITE,代碼行數:17,代碼來源:RTCIceCandidatePairStats.java


注:本文中的javax.json.Json類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。