Java Json jsonValueToJsonString(final JsonValue jsonValue, final String key)

Here you can find the source of jsonValueToJsonString(final JsonValue jsonValue, final String key)

Description

Parse a JsonValue into String

License

Apache License

Parameter

Parameter Description
jsonValue input object
key key for the input object

Return

String representation of the jsonValue object

Declaration

public static String jsonValueToJsonString(final JsonValue jsonValue, final String key) 

Method Source Code

//package com.java2s;
/**// www . j a v a2s. c  o  m
* Copyright 2013 CryptorChat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.Iterator;

import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;

public class Main {
    /**
     * Parse a JsonValue into String
     * @param jsonValue input object
     * @param key key for the input object
     * @return String representation of the jsonValue object
     */
    public static String jsonValueToJsonString(final JsonValue jsonValue, final String key) {
        final StringBuilder builder = new StringBuilder();
        if (key != null) {
            builder.append('\"').append(key).append("\":");
        }
        if (jsonValue instanceof JsonString) {
            builder.append('\"').append(jsonValue.toString()).append('\"');
        } else if (jsonValue instanceof JsonNumber || jsonValue instanceof JsonObject) {
            builder.append(jsonValue.toString());
        } else if (jsonValue instanceof JsonArray) {
            builder.append(jsonArrayToString((JsonArray) jsonValue));
        } else if (jsonValue == JsonValue.TRUE) {
            builder.append("true");
        } else if (jsonValue == JsonValue.FALSE) {
            builder.append("false");
        } else if (jsonValue == JsonValue.NULL) {
            builder.append("null");
        }
        return builder.toString();
    }

    /**
     * Parse a JsonArray into String
     * @param array input array
     * @return String representation of the array object
     */
    public static String jsonArrayToString(final JsonArray array) {
        final StringBuilder builder = new StringBuilder("[");
        final Iterator<JsonValue> iterator = array.iterator();
        while (iterator.hasNext()) {
            final JsonValue current = iterator.next();
            if (!builder.toString().equalsIgnoreCase("[")) {
                builder.append(',');
            }
            builder.append(jsonValueToJsonString(current, null));
        }
        builder.append(']');
        return builder.toString();
    }
}

Related

  1. jsonEquals(JsonArray expected, JsonArray actual, boolean strict)
  2. jsonObject(String key, JsonValue val)
  3. jsonObjectToBuilder(JsonObject jo)
  4. jsonPrettyPrinter(Object object)
  5. jsonString(JsonValue value)
  6. listToJsonArray(List list)
  7. mapToJsonObjectBuilder(Map map)
  8. newObject()
  9. nullable(String value)