Java Code Examples for javax.json.JsonObject#forEach()

The following examples show how to use javax.json.JsonObject#forEach() . 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: RtNetworks.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Network create(final String name, final JsonObject filters)
    throws IOException, UnexpectedResponseException {
    final JsonObjectBuilder json = Json.createObjectBuilder();
    json.add("Name", name);
    filters.forEach(json::add);
    return this.createNetwork(json);
}
 
Example 2
Source File: RtVolumes.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Volume create(final String name, final JsonObject parameters)
    throws IOException, UnexpectedResponseException {
    JsonObjectBuilder json = Json.createObjectBuilder();
    json.add("Name", name);
    parameters.forEach(json::add);
    return this.createVolume(json);
}
 
Example 3
Source File: Merged.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Ctor.
 * @param objects JsonObjects to combine.
 */
Merged(final JsonObject... objects) {
    super(() -> {
        final JsonObjectBuilder combined = Json.createObjectBuilder();
        for(final JsonObject json : objects) {
            json.forEach(combined::add);
        }
        return combined.build();
    });
}
 
Example 4
Source File: JsonParser.java    From jeddict with Apache License 2.0 5 votes vote down vote up
private BeanClass generateClass(EntityMappings entityMappings, String className, JsonObject jsonObject) {
    reporter.accept(getMessage(DocWizardDescriptor.class, "MSG_Progress_Class_Parsing", className));

    BeanClass beanClass = createBeanClass(entityMappings, className);
    jsonObject.forEach((key, value) -> {
        Attribute baseAttribute;
        if (value instanceof JsonString) {
            baseAttribute = createBeanAttribute(beanClass, STRING, key);
        } else if (value instanceof JsonNumber) {
            baseAttribute = createBeanAttribute(beanClass, getJsonNumberType((JsonNumber) value), key);
        } else if (value instanceof JsonArray) {
            JsonArray jsonArray = (JsonArray) value;
            if (!jsonArray.isEmpty()) {
                JsonValue arrayElement = jsonArray.get(0);
                if (arrayElement instanceof JsonString) {
                    baseAttribute = createBeanCollection(beanClass, STRING, key);
                } else if (arrayElement instanceof JsonNumber) {
                    baseAttribute = createBeanCollection(beanClass, getJsonNumberType((JsonNumber) arrayElement), key);
                } else if (arrayElement instanceof JsonObject) {
                    baseAttribute = createOneToManyAssociation(beanClass, generateClass(entityMappings, key, (JsonObject) arrayElement), key);
                } else {
                    baseAttribute = createBeanCollection(beanClass, STRING, key);
                }
            } else {
                baseAttribute = createBeanCollection(beanClass, STRING, key);
            }
        } else if (value instanceof JsonObject) {
            baseAttribute = createOneToOneAssociation(beanClass, generateClass(entityMappings, key, (JsonObject) value), key);
        } else if (value instanceof JsonValue) {
            if (((JsonValue) value).getValueType() == TRUE || ((JsonValue) value).getValueType() == FALSE) {
                baseAttribute = createBeanAttribute(beanClass, BOOLEAN, key);
            } else {
                baseAttribute = createBeanAttribute(beanClass, STRING, key);
            }
        } else {
            baseAttribute = createBeanAttribute(beanClass, STRING, key);
        }
    });
    return beanClass;
}
 
Example 5
Source File: JsonParser.java    From jeddict with Apache License 2.0 5 votes vote down vote up
private Entity generateEntity(EntityMappings entityMappings, String className, JsonObject jsonObject) {
    reporter.accept(getMessage(DocWizardDescriptor.class, "MSG_Progress_Class_Parsing", className));

    Entity entity = createEntity(entityMappings, className);
    jsonObject.forEach((key, value) -> {
        Attribute baseAttribute;
        if (value instanceof JsonString) {
            baseAttribute = createBasicAttribute(entity, STRING, key);
        } else if (value instanceof JsonNumber) {
            baseAttribute = createBasicAttribute(entity, getJsonNumberType((JsonNumber) value), key);
        } else if (value instanceof JsonArray) {
            JsonArray jsonArray = (JsonArray) value;
            if (!jsonArray.isEmpty()) {
                JsonValue arrayElement = jsonArray.get(0);
                if (arrayElement instanceof JsonString) {
                    baseAttribute = createElementCollection(entity, STRING, key);
                } else if (arrayElement instanceof JsonNumber) {
                    baseAttribute = createElementCollection(entity, getJsonNumberType((JsonNumber) arrayElement), key);
                } else if (arrayElement instanceof JsonObject) {
                    baseAttribute = createOneToMany(entity, generateEntity(entityMappings, key, (JsonObject) arrayElement), key);
                } else {
                    baseAttribute = createElementCollection(entity, STRING, key);
                }
            } else {
                baseAttribute = createElementCollection(entity, STRING, key);
            }
        } else if (value instanceof JsonObject) {
            baseAttribute = createOneToOne(entity, generateEntity(entityMappings, key, (JsonObject) value), key);
        } else if (value instanceof JsonValue) {
            if (((JsonValue) value).getValueType() == TRUE || ((JsonValue) value).getValueType() == FALSE) {
                baseAttribute = createBasicAttribute(entity, BOOLEAN, key);
            } else {
                baseAttribute = createBasicAttribute(entity, STRING, key);
            }
        } else {
            baseAttribute = createBasicAttribute(entity, STRING, key);
        }
    });
    return entity;
}
 
Example 6
Source File: JSONResponseReader.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public Object readResponse( Response response, Class<?> resultType )
{
    if( response.getEntity().getMediaType().equals( MediaType.APPLICATION_JSON ) )
    {
        if( ValueComposite.class.isAssignableFrom( resultType ) )
        {
            String jsonValue = response.getEntityAsText();
            ValueCompositeType valueType = module.valueDescriptor( resultType.getName() ).valueType();
            return jsonDeserializer.deserialize( module, valueType, jsonValue );
        }
        else if( resultType.equals( Form.class ) )
        {
            try( JsonReader reader = jsonFactories.readerFactory()
                                                  .createReader( response.getEntity().getReader() ) )
            {
                JsonObject jsonObject = reader.readObject();
                Form form = new Form();
                jsonObject.forEach(
                    ( key, value ) ->
                    {
                        String valueString = value.getValueType() == JsonValue.ValueType.STRING
                                             ? ( (JsonString) value ).getString()
                                             : value.toString();
                        form.set( key, valueString );
                    } );
                return form;
            }
            catch( IOException | JsonException e )
            {
                throw new ResourceException( e );
            }
        }
    }
    return null;
}
 
Example 7
Source File: RecordConverters.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
private Record json2Record(final RecordBuilderFactory factory, final JsonObject object) {
    final Record.Builder builder = factory.newRecordBuilder();
    object.forEach((key, value) -> {
        switch (value.getValueType()) {
        case ARRAY: {
            final List<Object> items =
                    value.asJsonArray().stream().map(it -> mapJson(factory, it)).collect(toList());
            builder
                    .withArray(factory
                            .newEntryBuilder()
                            .withName(key)
                            .withType(Schema.Type.ARRAY)
                            .withElementSchema(getArrayElementSchema(factory, items))
                            .build(), items);
            break;
        }
        case OBJECT: {
            final Record record = json2Record(factory, value.asJsonObject());
            builder
                    .withRecord(factory
                            .newEntryBuilder()
                            .withName(key)
                            .withType(Schema.Type.RECORD)
                            .withElementSchema(record.getSchema())
                            .build(), record);
            break;
        }
        case TRUE:
        case FALSE:
            builder.withBoolean(key, JsonValue.TRUE.equals(value));
            break;
        case STRING:
            builder.withString(key, JsonString.class.cast(value).getString());
            break;
        case NUMBER:
            final JsonNumber number = JsonNumber.class.cast(value);
            builder.withDouble(key, number.doubleValue());
            break;
        case NULL:
            break;
        default:
            throw new IllegalArgumentException("Unsupported value type: " + value);
        }
    });
    return builder.build();
}
 
Example 8
Source File: JsonHelper.java    From logbook-kai with MIT License 3 votes vote down vote up
/**
 * JsonObjectをMapに変換します
 *
 * @param <T> JsonObjectの内容の型
 * @param <K> Mapのキーの型
 * @param <R> Mapの内容の型
 * @param obj 変換するJsonObject
 * @param keyMapper JsonObjectのキーからキーを取り出すFunction
 * @param valueMapper array内のJsonValueを変換するFunction
 * @return 変換後のMap
 */
@SuppressWarnings("unchecked")
public static <T extends JsonValue, K, R> Map<K, R> toMap(JsonObject obj, Function<String, K> keyMapper,
        Function<T, R> valueMapper) {
    Map<K, R> map = new LinkedHashMap<>();
    obj.forEach((k, v) -> {
        R r = valueMapper.apply((T) v);
        map.put(keyMapper.apply(k), r);
    });
    return map;
}