Example usage for javax.json JsonObject toString

List of usage examples for javax.json JsonObject toString

Introduction

In this page you can find the example usage for javax.json JsonObject toString.

Prototype

@Override
String toString();

Source Link

Document

Returns JSON text for this JSON value.

Usage

From source file:io.hakbot.util.JsonUtil.java

/**
 * Returns a string representation of a json object, or null if json object is null.
 *///w w w. j  a  v a 2  s.c  om
public static String toJsonString(JsonObject json) {
    return json != null ? json.toString() : null;
}

From source file:org.dcm4che3.tool.qc.QC.java

public static QCResult performOperation(String opDescription, QC qc) throws IllegalArgumentException {

    switch (qc.getOperation()) {

    case SPLIT:// ww w . j av  a  2  s . co  m
    case MERGE:
    case SEGMENT:
    case UPDATE:
    case REJECT:
    case RESTORE:
        JsonObject qcMessage = initQCObject(qc);
        System.out.println(qcMessage.toString());
        return sendRequest(opDescription, qc, qcMessage);
    case DELETE:
        if (checkDelete(qc)) {
            return sendDeleteRequest(opDescription, qc);
        }
    case PATIENT_LINK:
    case PATIENT_MERGE:
    case PATIENT_UNLINK:
    case PATIENT_UPDATE_ID:
        JsonArray qcPatientMessage = initPatientObject(qc);
        qc.setUrl(adjustPatientURL(qc).replace(" ", "%20"));
        return sendRequest(opDescription, qc, qcPatientMessage);
    default:
        throw new IllegalArgumentException("Incorrect Operation specified");
    }
}

From source file:org.kitodo.production.forms.IndexingForm.java

private static String readMapping() {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    try (InputStream inputStream = classloader.getResourceAsStream("mapping.json")) {
        String mapping = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        try (JsonReader jsonReader = Json.createReader(new StringReader(mapping))) {
            JsonObject jsonObject = jsonReader.readObject();
            return jsonObject.toString();
        }/*from  ww  w.  j  a  v a2 s . c om*/
    } catch (IOException e) {
        Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
        return "";
    }
}

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Becareful result is not unmarshalled//  ww w. jav  a2s.  co  m
 *
 * @param json
 * @return
 */
protected static MessageToClient mtcFromJson(String json) {
    try (JsonReader reader = Json.createReader(new StringReader(json))) {
        JsonObject root = reader.readObject();
        MessageToClient message = new MessageToClient();
        message.setId(root.getString(Constants.Message.ID));
        message.setTime(root.getInt(Constants.Message.TIME));
        message.setType(MessageType.valueOf(root.getString(Constants.Message.TYPE)));
        message.setDeadline(root.getInt(Constants.Message.DEADLINE));
        if (null != message.getType()) {
            switch (message.getType()) {
            case FAULT:
                JsonObject faultJs = root.getJsonObject(Constants.Message.RESPONSE);
                Fault f = Fault.createFromJson(faultJs.toString());
                message.setFault(f);
                break;
            case MESSAGE:
                message.setResult("" + root.get(Constants.Message.RESPONSE));
                message.setType(MessageType.MESSAGE);
                break;
            case CONSTRAINT:
                JsonArray result = root.getJsonArray(Constants.Message.RESPONSE);
                List<ConstraintViolation> list = new ArrayList<>();
                for (JsonValue jsonValue : result) {
                    list.add(getJava(ConstraintViolation.class, ((JsonObject) jsonValue).toString()));
                }
                message.setConstraints(list.toArray(new ConstraintViolation[] {}));
                break;
            default:
                message.setResult("" + root.get(Constants.Message.RESPONSE));
                break;
            }
        }
        return message;
    }
}

From source file:co.runrightfast.vertx.core.hazelcast.serializers.CompressedJsonObjectSerializer.java

@Override
public byte[] write(final JsonObject json) throws IOException {
    final byte[] bytes = json.toString().getBytes(UTF_8);
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
    try (final GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
        gzip.write(bytes);/* w  ww  .ja va 2s  .c  om*/
    }
    return bos.toByteArray();
}

From source file:org.raml.v2.json_schema.TypeToJsonSchemaTest.java

@Test
public void test() throws IOException, SAXException {
    final RamlBuilder builder = new RamlBuilder();
    final Node build = builder.build(input);
    final List<TypeDeclarationField> fields = build.findDescendantsWith(TypeDeclarationField.class);
    for (TypeDeclarationField field : fields) {
        if (field.getName().equals("root")) {
            final ResolvedType resolvedType = ((TypeDeclarationNode) field.getValue()).getResolvedType();
            JsonObject actual = new TypeToJsonSchemaVisitor().transform(resolvedType);
            dump = actual.toString();
            expected = IOUtils.toString(new FileInputStream(expectedOutput));
            assertTrue(jsonEquals(dump, expected));
            return;
        }// w w  w  .j a va  2  s .c  o  m
    }
    fail("No type called root was found");
}

From source file:co.runrightfast.vertx.orientdb.ODatabaseDocumentTxHealthCheck.java

@Override
protected Result check() throws Exception {
    try (final ODatabaseDocumentTx db = oDatabaseDocumentTxSupplier.get()) {
        final JsonObjectBuilder msgBuilder = Json.createObjectBuilder().add("db", getDatabaseInfo(db));
        if (CollectionUtils.isNotEmpty(documentObjects)) {
            final JsonObjectBuilder counts = Json.createObjectBuilder();
            documentObjects.stream().forEach(documentObject -> browseClass(db, documentObject, counts));
            msgBuilder.add("counts", counts);
        }/* w ww  .  j  av a2  s. c  o m*/

        final JsonObject msg = msgBuilder.build();
        if (isHealthy(msg)) {
            return HealthCheck.Result.healthy(msg.toString());
        }
        return HealthCheck.Result.unhealthy(msg.toString());
    } catch (final Exception e) {
        final JsonObjectBuilder msg = Json.createObjectBuilder().add("db",
                Json.createObjectBuilder().add("name", databaseName).build());
        return HealthCheck.Result
                .unhealthy(new ODatabaseDocumentTxHealthCheckException(msg.build().toString(), e));
    }
}

From source file:org.grogshop.services.endpoints.impl.ShopUserProfileServiceImpl.java

@Override
public Response get(@PathParam("id") Long user_id) throws ServiceException {
    Profile p = profileService.getById(user_id);
    if (p == null) {
        throw new ServiceException("Profile for " + user_id + " doesn't exists");
    }//www .  j av  a2  s.c  o m
    JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
    jsonObjBuilder.add("bio", (p.getIntroduction() == null) ? "" : p.getIntroduction());
    jsonObjBuilder.add("location", (p.getPostcode() == null) ? "" : p.getPostcode());
    jsonObjBuilder.add("username", (p.getRealname() == null) ? "" : p.getRealname());

    JsonObject jsonObj = jsonObjBuilder.build();
    return Response.ok(jsonObj.toString()).build();
}

From source file:com.amazon.alexa.avs.auth.companionapp.OAuth2ClientForPkce.java

/**
 * Uses the LWA service to fetch an access token and refresh token in exchange for a refresh token and clientId.
 * Expected use case of this method: refreshing tokens once the initial provisioning is complete and normal
 * usage of the device is ready to commence.
 *
 * @param refreshToken received from the initial provisioning request
 * @param clientId of the security profile associated with the companion app
 * @return {@link OAuth2AccessToken} object containing the access token and refresh token
 * @throws IOException/*from   w ww  . j  av  a  2 s.c  o  m*/
 */
public OAuth2TokensForPkce exchangeRefreshTokenForTokens(String refreshToken, String clientId)
        throws IOException {
    HttpURLConnection connection = (HttpURLConnection) tokenEndpoint.openConnection();

    JsonObject data = prepareExchangeRefreshTokenForTokensData(refreshToken, clientId);

    JsonObject jsonObject = postRequest(connection, data.toString());

    String newAccessToken = jsonObject.getString(AuthConstants.OAuth2.ACCESS_TOKEN);
    String newRefreshToken = jsonObject.getString(AuthConstants.OAuth2.REFRESH_TOKEN);
    int expiresIn = jsonObject.getInt(AuthConstants.OAuth2.EXPIRES_IN);

    return new OAuth2TokensForPkce(clientId, newAccessToken, newRefreshToken, expiresIn);
}

From source file:com.amazon.alexa.avs.auth.companionapp.OAuth2ClientForPkce.java

/**
 * Uses the LWA service to fetch an access token and refresh token in exchange for an auth code
 * (and a few other relevant parameter). Expected use case of this method: once we receive a
 * message/notification from the companion app with the authCode, this method will be used to
 * hit LWA and return tokens. These tokens can then be used to access AVS.
 *
 * @param authCode provided by the companion application
 * @param redirectUri corresponding to the companion application
 * @param clientId of the security profile associated with the companion app
 * @param codeVerifier unique value known to the device
 * @return {@link OAuth2AccessToken} object containing the access token and refresh token
 * @throws IOException//from w  w  w  . jav  a 2  s . c  om
 */
public OAuth2TokensForPkce exchangeAuthCodeForTokens(String authCode, String redirectUri, String clientId,
        String codeVerifier) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) tokenEndpoint.openConnection();

    JsonObject data = prepareExchangeAuthCodeForTokensData(authCode, redirectUri, clientId, codeVerifier);

    JsonObject jsonObject = postRequest(connection, data.toString());

    String newAccessToken = jsonObject.getString(AuthConstants.OAuth2.ACCESS_TOKEN);
    String newRefreshToken = jsonObject.getString(AuthConstants.OAuth2.REFRESH_TOKEN);
    int expiresIn = jsonObject.getInt(AuthConstants.OAuth2.EXPIRES_IN);

    return new OAuth2TokensForPkce(clientId, newAccessToken, newRefreshToken, expiresIn);
}