diff --git a/util/pom.xml b/util/pom.xml index 837eaf324b..ede3235570 100644 --- a/util/pom.xml +++ b/util/pom.xml @@ -59,6 +59,11 @@ system-rules 1.16.1 + + org.json + json + 20170516 + diff --git a/util/src/main/java/io/kubernetes/client/util/ClientUtils b/util/src/main/java/io/kubernetes/client/util/ClientUtils new file mode 100644 index 0000000000..b551cb7f56 --- /dev/null +++ b/util/src/main/java/io/kubernetes/client/util/ClientUtils @@ -0,0 +1,62 @@ +/* + * Copyright 2017 Cisco, Inc. + * + * 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.kubernetes.client.util; + +import java.util.ArrayList; +import java.util.Map; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +/** + * This class is used to convert any YAML Object to JSON + * @author prassin3 + * + */ +public class ClientUtils { + + public static Object _convertToJson(Object o) throws JSONException { + if (o instanceof Map) { + Map map = (Map) o; + + JSONObject result = new JSONObject(); + + for (Map.Entry stringObjectEntry : map.entrySet()) { + String key = stringObjectEntry.getKey().toString(); + + result.put(key, _convertToJson(stringObjectEntry.getValue())); + } + + return result; + } else if (o instanceof ArrayList) { + ArrayList arrayList = (ArrayList) o; + JSONArray result = new JSONArray(); + + for (Object arrayObject : arrayList) { + result.put(_convertToJson(arrayObject)); + } + + return result; + } else if (o instanceof String) { + return o; + } else if (o instanceof Boolean) { + return o; + } else { + + return o; + } + } +}