118 lines
3.4 KiB
Java
118 lines
3.4 KiB
Java
/*-
|
|
* ~~~~~~licensing~~~~~~
|
|
* base-support
|
|
* ==========
|
|
* Copyright (C) 2020 - 2021 EmDev LLC
|
|
* ==========
|
|
* 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.
|
|
* ~~~~~~/licensing~~~~~~
|
|
*/
|
|
package ru.entaxy.platform.base.support;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.stream.Collectors;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import com.google.gson.JsonPrimitive;
|
|
|
|
public class JSONUtils {
|
|
|
|
public static final String PROP_VALUE = "_value";
|
|
|
|
public static Map<String, Object> element2map(JsonElement element){
|
|
Map<String, Object> result = new HashMap<>();
|
|
if (element.isJsonObject()) {
|
|
JsonObject jsonObject = element.getAsJsonObject();
|
|
for (Entry<String, JsonElement> entry: jsonObject.entrySet()) {
|
|
result.put(entry.getKey(), element2object(entry.getValue()));
|
|
}
|
|
} else
|
|
if (element.isJsonArray()) {
|
|
JsonArray array = element.getAsJsonArray();
|
|
result.put(PROP_VALUE, element2list(element));
|
|
} else
|
|
if (element.isJsonNull()) {
|
|
result.put(PROP_VALUE, null);
|
|
} else
|
|
if (element.isJsonPrimitive()) {
|
|
result.put(PROP_VALUE, element2value(element));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static Object element2object(JsonElement element) {
|
|
if (element.isJsonNull() || element.isJsonPrimitive())
|
|
return element2value(element);
|
|
if (element.isJsonArray())
|
|
return element2list(element);
|
|
if (element.isJsonObject())
|
|
return element2map(element);
|
|
return null;
|
|
}
|
|
|
|
public static Object element2value(JsonElement element) {
|
|
Object result = null;
|
|
if (element.isJsonNull() || !element.isJsonPrimitive())
|
|
return result;
|
|
try {
|
|
JsonPrimitive primitive = element.getAsJsonPrimitive();
|
|
if (primitive.isNumber())
|
|
result = primitive.getAsNumber();
|
|
else if (primitive.isBoolean())
|
|
result = primitive.getAsBoolean();
|
|
else result = primitive.getAsString();
|
|
} catch (Exception e1) {
|
|
try {
|
|
result = element.getAsBoolean();
|
|
} catch (Exception e2) {
|
|
result = element.getAsString();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static List<Object> element2list(JsonElement element) {
|
|
List<Object> result = new ArrayList<>();
|
|
JsonArray array = element.getAsJsonArray();
|
|
for (int i=0; i<array.size(); i++)
|
|
result.add(element2object(array.get(i)));
|
|
return result;
|
|
}
|
|
|
|
public static JsonObject getJsonRootObject(URL url) throws IOException {
|
|
String metadata = new BufferedReader (
|
|
new InputStreamReader(
|
|
url.openStream(), StandardCharsets.UTF_8))
|
|
.lines()
|
|
.collect(Collectors.joining("\n"));
|
|
|
|
JsonElement je = (new JsonParser()).parse(metadata);
|
|
JsonObject root = je.getAsJsonObject();
|
|
return root;
|
|
}
|
|
|
|
}
|