本文整理匯總了Java中javax.json.JsonValue.ValueType方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonValue.ValueType方法的具體用法?Java JsonValue.ValueType怎麽用?Java JsonValue.ValueType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.json.JsonValue
的用法示例。
在下文中一共展示了JsonValue.ValueType方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getValueByPointer
import javax.json.JsonValue; //導入方法依賴的package包/類
/**
* Looks up a {@link JsonValue} which is referenced by a given JSON pointer.
* @param pointer the JSON pointer which refers to a {@link JsonValue}.
* @return the {@link JsonValue} if found, {@code null} otherwise.
* @exception IllegalArgumentException if specified pointer is {@code null}.
*/
public JsonValue getValueByPointer(JsonPointer pointer) {
if (pointer == null) {
throw new IllegalArgumentException();
}
JsonValue current = getRootValue();
for (Object token: pointer) {
if (current == null) {
break;
}
JsonValue.ValueType type = current.getValueType();
if (type == JsonValue.ValueType.ARRAY) {
if ((token instanceof Integer)) {
int index = ((Integer)token).intValue();
current = ((JsonArray)current).get(index);
} else {
current = null;
}
} else if (type == JsonValue.ValueType.OBJECT) {
current = ((JsonObject)current).get(token);
} else {
break;
}
}
return current;
}
示例2: compareJsonValue
import javax.json.JsonValue; //導入方法依賴的package包/類
private static void compareJsonValue(JsonValue actual, JsonValue expected) {
assertNotNull(actual);
assertNotNull(expected);
assertThat(actual.getValueType(), is(expected.getValueType()));
JsonValue.ValueType type = actual.getValueType();
if (type == JsonValue.ValueType.OBJECT) {
JsonObject actualObject = (JsonObject)actual;
JsonObject expectedObject = (JsonObject)expected;
assertThat(actualObject.size(), equalTo(expectedObject.size()));
for (String key: actualObject.keySet()) {
compareJsonValue(actualObject.get(key), expectedObject.get(key));
}
} else if (type == JsonValue.ValueType.ARRAY) {
JsonArray actualArray = (JsonArray)actual;
JsonArray expectedArray = (JsonArray)expected;
assertThat(actualArray.size(), equalTo(expectedArray.size()));
for (int i = 0; i < actualArray.size(); i++) {
compareJsonValue(actualArray.get(i), expectedArray.get(i));
}
} else {
assertTrue(actual.equals(expected));
}
}
示例3: createJsonString
import javax.json.JsonValue; //導入方法依賴的package包/類
/**
* Create a {@link JsonString}.
* <p>
* This is here mainly to support {@link #createJsonValue(java.lang.String)} as the {@link Json} class does not provide a way of generating this.
* <p>
* @param s String
* <p>
* @return JsonString
* <p>
* @throws NullPointerException if s is null
*/
public static JsonString createJsonString( final String s )
{
Objects.requireNonNull( s );
return new JsonString()
{
@Override
public String getString()
{
return s;
}
@Override
public CharSequence getChars()
{
return s;
}
@Override
public JsonValue.ValueType getValueType()
{
return JsonValue.ValueType.STRING;
}
};
}
示例4: printValue
import javax.json.JsonValue; //導入方法依賴的package包/類
/**
* This method prints the value of the JSON data. If the value is a JSON object and key1 is provided, it will
* only print the values of key1 and optionally key2. If key1 is null, it will recurse into the JSON object
* and print all the nested structures.
*
* @param key specifies the key string of the JSON object, can be null if no associated key.
* @param value specifies the JSON value.
* @param level specifies the indentation level.
* @param key1 specifies the first key.
* @param key2 specifies the second key.
* @param dataOut specifies the output print stream.
*/
private void printValue(String key, JsonValue value, int level, String key1, String key2, PrintStream dataOut)
{
JsonValue.ValueType valueType = value.getValueType();
switch (valueType)
{
case OBJECT:
JsonObject obj = (JsonObject)value;
printIndentation(level, dataOut);
if (key1 != null)
{
dataOut.print(obj.get(key1));
if (key2 != null)
{
dataOut.println(": " + obj.get(key2));
}
else
{
dataOut.println();
}
}
else
{
if (key != null)
{
dataOut.print(key + ": ");
}
dataOut.println("{");
Iterator<String> iterator = obj.keySet().iterator();
while (iterator.hasNext())
{
String childKey = iterator.next();
printValue(childKey, obj.get(childKey), level + 1, key1, key2, dataOut);
}
printIndentation(level, dataOut);
dataOut.println("}");
}
break;
case ARRAY:
printIndentation(level, dataOut);
if (key != null)
{
dataOut.print(key + ": ");
}
dataOut.println("[");
JsonArray array = (JsonArray)value;
for (int i = 0; i < array.size(); i++)
{
printValue(null, array.get(i), level + 1, key1, key2, dataOut);
}
printIndentation(level, dataOut);
dataOut.println("]");
break;
default:
printIndentation(level, dataOut);
if (key != null)
{
dataOut.print(key + ": ");
}
dataOut.println(value);
break;
}
}
示例5: createJsonNumber
import javax.json.JsonValue; //導入方法依賴的package包/類
public static JsonNumber createJsonNumber( BigDecimal d )
{
return new JsonNumber()
{
@Override
public boolean isIntegral()
{
return d.scale() == 0;
}
@Override
public int intValue()
{
return d.intValue();
}
@Override
public int intValueExact()
{
return d.intValueExact();
}
@Override
public long longValue()
{
return d.longValue();
}
@Override
public long longValueExact()
{
return d.longValueExact();
}
@Override
public BigInteger bigIntegerValue()
{
return d.toBigInteger();
}
@Override
public BigInteger bigIntegerValueExact()
{
return d.toBigIntegerExact();
}
@Override
public double doubleValue()
{
return d.doubleValue();
}
@Override
public BigDecimal bigDecimalValue()
{
return d;
}
@Override
public JsonValue.ValueType getValueType()
{
return ValueType.NUMBER;
}
};
}