Here you can find the source of jsonValueToJsonString(final JsonValue jsonValue, final String key)
Parameter | Description |
---|---|
jsonValue | input object |
key | key for the input object |
public static String jsonValueToJsonString(final JsonValue jsonValue, final String key)
//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(); } }