package io.vertx.config.spi.utils;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.util.Objects;
import java.util.Properties;
public class JsonObjectHelper {
@Deprecated
public static Buffer toBuffer(JsonObject json) {
return json.toBuffer();
}
public static void put(JsonObject json, String name, String value, boolean rawData) {
json.put(name, rawData ? value : convert(value));
}
public static Object convert(String value) {
Objects.requireNonNull(value);
Boolean bool = asBoolean(value);
if (bool != null) {
return bool;
}
Double integer = asNumber(value);
if (integer != null) {
return integer;
}
JsonObject obj = asJsonObject(value);
if (obj != null) {
return obj;
}
JsonArray arr = asJsonArray(value);
if (arr != null) {
return arr;
}
return value;
}
private static Double asNumber(String s) {
try {
return Double.parseDouble(s);
} catch (NumberFormatException nfe) {
return null;
}
}
private static Boolean asBoolean(String s) {
if (s.equalsIgnoreCase("true")) {
return Boolean.TRUE;
} else if (s.equalsIgnoreCase("false")) {
return Boolean.FALSE;
} else {
return null;
}
}
private static JsonObject asJsonObject(String s) {
if (s.startsWith("{") && s.endsWith("}")) {
try {
return new JsonObject(s);
} catch (Exception e) {
return null;
}
}
return null;
}
private static JsonArray asJsonArray(String s) {
if (s.startsWith("[") && s.endsWith("]")) {
try {
return new JsonArray(s);
} catch (Exception e) {
return null;
}
}
return null;
}
public static JsonObject from(Properties props) {
return from(props, false);
}
public static JsonObject from(Properties props, boolean rawData) {
JsonObject json = new JsonObject();
props.stringPropertyNames()
.forEach(name -> put(json, name, props.getProperty(name), rawData));
return json;
}
}