io.knotx.server.api.context.MultiMapConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of knotx-server-http-api Show documentation
Show all versions of knotx-server-http-api Show documentation
HTTP Server API - Knot.x HTTP Server models
/*
* Copyright (C) 2019 Knot.x Project
*
* 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.
*/
package io.knotx.server.api.context;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.reactivex.core.MultiMap;
final class MultiMapConverter {
private MultiMapConverter() {
//util constructor
}
/**
* Converts MultiMap to JsonObject
It expects the MultiMap key, contains List of String
* objects, so the result of conversion will look like below
*
*
* {
* "mapKey1": ["val1", "val2"],
* "mapKey2": ["val1"]
* }
*
*
* @param multiMap - {@link MultiMap} to convert
* @return - {@link JsonObject} with {@link JsonArray} under each object key
*/
static JsonObject toJsonObject(MultiMap multiMap) {
JsonObject json = new JsonObject();
multiMap.getDelegate().forEach(
entry -> {
JsonArray values;
if (json.containsKey(entry.getKey())) {
values = json.getJsonArray(entry.getKey());
} else {
values = new JsonArray();
json.put(entry.getKey(), values);
}
values.add(entry.getValue());
}
);
return json;
}
/**
* Converts JsonObject to MultiMap. It expects the JsonObject key, contains JsonArray with list of
* String objects.
* Each jsonObject key is converted into MultiMap "key", while JsonArray as List of String objects
* for this velue.
*
* @param json - {@link JsonObject} to convert
* @return - {@link MultiMap} created from {@link JsonObject}
*/
static MultiMap fromJsonObject(JsonObject json) {
MultiMap map = MultiMap.caseInsensitiveMultiMap();
json.stream()
.forEach(
entry ->
((JsonArray) entry.getValue())
.stream()
.forEach(
value ->
map.add(entry.getKey(), (String) value)
)
);
return map;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy