Java Code Examples for javax.json.JsonValue#asJsonArray()

The following examples show how to use javax.json.JsonValue#asJsonArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonUtilsTests.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapClaimValueCollection() {
    JsonArray expResult = Json.createArrayBuilder()
            .add("a")
            .add("b")
            .add("c")
            .build();
    JsonValue result = JsonUtils.wrapValue(Arrays.asList("a", "b", "c"));

    assertTrue(result instanceof JsonArray);
    JsonArray resultArray = result.asJsonArray();
    assertEquals(expResult.size(), resultArray.size());
    assertEquals(expResult.getString(0), resultArray.getString(0));
    assertEquals(expResult.getString(1), resultArray.getString(1));
    assertEquals(expResult.getString(2), resultArray.getString(2));
}
 
Example 2
Source File: JsonUtilsTests.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrapClaimValueCollectionWithNull() {
    JsonArray expResult = Json.createArrayBuilder()
            .add(NULL)
            .build();
    JsonValue result = JsonUtils.wrapValue(Arrays.asList((String) null));

    assertTrue(result instanceof JsonArray);
    JsonArray resultArray = result.asJsonArray();
    assertEquals(expResult.size(), resultArray.size());
    assertEquals(expResult.get(0), resultArray.get(0));
}
 
Example 3
Source File: Generator.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private static boolean areEqualsIgnoringOrder(final JsonValue oldValue, final JsonValue newValue) {
    if (!oldValue.getValueType().equals(newValue.getValueType())) {
        return false;
    }
    switch (oldValue.getValueType()) {
    case STRING:
        return JsonString.class.cast(oldValue).getString().equals(JsonString.class.cast(newValue).getString());
    case NUMBER:
        return JsonNumber.class.cast(oldValue).doubleValue() == JsonNumber.class.cast(newValue).doubleValue();
    case OBJECT:
        final JsonObject oldObject = oldValue.asJsonObject();
        final JsonObject newObject = newValue.asJsonObject();
        if (!oldObject.keySet().equals(newObject.keySet())) {
            return false;
        }
        return oldObject
                .keySet()
                .stream()
                .map(key -> areEqualsIgnoringOrder(oldObject.get(key), newObject.get(key)))
                .reduce(true, (a, b) -> a && b);
    case ARRAY:
        final JsonArray oldArray = oldValue.asJsonArray();
        final JsonArray newArray = newValue.asJsonArray();
        if (oldArray.size() != newArray.size()) {
            return false;
        }
        if (oldArray.isEmpty()) {
            return true;
        }
        for (final JsonValue oldItem : oldArray) {
            if (newArray.stream().noneMatch(newitem -> areEqualsIgnoringOrder(oldItem, newitem))) {
                return false;
            }
        }
        return true;
    default:
        // value type check was enough
        return true;
    }
}
 
Example 4
Source File: SchemalessJsonToIndexedRecord.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private Schema guessSchema(final String recordName, final JsonValue element) {
    switch (element.getValueType()) {
    case STRING:
        return STRING;
    case NUMBER:
        final Number number = JsonNumber.class.cast(element).numberValue();
        if (Long.class.isInstance(number)) {
            return LONG;
        }
        if (Integer.class.isInstance(number)) {
            return INT;
        }
        return DOUBLE;
    case FALSE:
    case TRUE:
        return BOOLEAN;
    case NULL:
        return NULL;
    case OBJECT:
        final Schema record = Schema.createRecord(recordName, null, NAMESPACE, false);
        record
                .setFields(element
                        .asJsonObject()
                        .entrySet()
                        .stream()
                        .map(it -> new Schema.Field(it.getKey(),
                                guessSchema(buildNextName(recordName, it.getKey()), it.getValue()), null, null))
                        .collect(toList()));
        return record;
    case ARRAY:
        final JsonArray array = element.asJsonArray();
        if (!array.isEmpty()) {
            return Schema.createArray(guessSchema(buildNextName(recordName, "Array"), array.iterator().next()));
        }
        return Schema.createArray(Schema.create(Schema.Type.NULL));
    default:
        throw new IllegalArgumentException("Unsupported: " + element.toString());
    }
}
 
Example 5
Source File: ChaincodeCollectionConfiguration.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
private static JsonArray getJsonArray(JsonObject obj, String prop) throws ChaincodeCollectionConfigurationException {
    JsonValue ret = obj.get(prop);
    if (ret == null) {
        throw new ChaincodeCollectionConfigurationException(format("property %s missing", prop));
    }
    if (ret.getValueType() != JsonValue.ValueType.ARRAY) {
        throw new ChaincodeCollectionConfigurationException(format("property %s wrong type expected array got %s", prop, ret.getValueType().name()));
    }

    return ret.asJsonArray();

}
 
Example 6
Source File: SystemTestJson.java    From tcases with MIT License 5 votes vote down vote up
/**
 * Returns the FunctionTestDef represented by the given JSON value.
 */
private static FunctionTestDef asFunctionTestDef( String functionName, JsonValue json)
  {
  FunctionTestDef functionTestDef;

  try
    {
    functionTestDef = new FunctionTestDef( validIdentifier( functionName));

    JsonArray testCases;
    if( json.getValueType() == ARRAY)
      {
      // For compatibility, accept documents conforming to schema version <= 3.0.1
      testCases = json.asJsonArray();
      }
    else
      {
      JsonObject functionObject = json.asJsonObject();
      testCases = functionObject.getJsonArray( TEST_CASES_KEY);
      
      // Get annotations for this function.
      Optional.ofNullable( functionObject.getJsonObject( HAS_KEY))
        .ifPresent( has -> has.keySet().stream().forEach( key -> functionTestDef.setAnnotation( key, has.getString( key))));
      }

    // Get function test cases.
    testCases
      .getValuesAs( JsonObject.class)
      .stream()
      .forEach( testCase -> functionTestDef.addTestCase( asTestCase( testCase)));
    }
  catch( SystemTestException e)
    {
    throw new SystemTestException( String.format( "Error defining function=%s", functionName), e);
    }
  
  return functionTestDef;
  }
 
Example 7
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static JsonArray getJsonValueAsArray(JsonValue value) {
    return (value != null && value.getValueType() == ValueType.ARRAY) ? value.asJsonArray() : null;
}