Commit aac4cc99 authored by Georg Fette's avatar Georg Fette
Browse files

added jsonutil

parent 0a23e6d1
......@@ -225,6 +225,11 @@
<artifactId>commons-dbcp2</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
</dependencies>
<build>
<plugins>
......@@ -244,5 +249,5 @@
</plugins>
</build>
<groupId>UniWueUtils</groupId>
<version>0.0.2</version>
<version>0.0.4</version>
</project>
\ No newline at end of file
package de.uniwue.misc.util;
import org.json.JSONArray;
import org.json.JSONObject;
import de.uniwue.misc.util.ResourceElem.FieldType;
public class JSONUtil {
public static void parseJsonObject(JSONObject anObj, ResourceElem container) {
for (String aKey : anObj.keySet()) {
Object subObject = anObj.get(aKey);
ResourceElem newField = new ResourceElem();
newField.name = aKey;
container.fields.put(aKey, newField);
parseJsonField(subObject, newField);
}
}
public static void parseJsonField(Object subObject, ResourceElem newField) {
if (subObject instanceof JSONObject) {
newField.type = FieldType.Object;
JSONObject jsonObj = (JSONObject) subObject;
parseJsonObject(jsonObj, newField);
} else if (subObject instanceof JSONArray) {
newField.type = FieldType.Array;
JSONArray jsonArray = (JSONArray) subObject;
for (int j = 0; j < jsonArray.length(); j++) {
Object anArrayElem = jsonArray.get(j);
ResourceElem newArrayElem = new ResourceElem();
newArrayElem.name = newField.name;
newField.arrayElems.add(newArrayElem);
parseJsonField(anArrayElem, newArrayElem);
}
} else if (subObject instanceof String) {
newField.type = FieldType.Primitive;
newField.value = subObject.toString();
} else if (subObject instanceof Number) {
newField.type = FieldType.Primitive;
newField.value = subObject.toString();
}
}
}
package de.uniwue.misc.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ResourceElem {
public String name;
public String value;
public HashMap<String, ResourceElem> fields = new HashMap<String, ResourceElem>();
public List<ResourceElem> arrayElems = new ArrayList<ResourceElem>();
public static enum FieldType {
Primitive,
Array,
Object
}
public FieldType type;
public boolean isArray() {
return type == FieldType.Array;
}
public String toString() {
if (type == FieldType.Primitive) {
return name + " : " + value;
} else {
return name;
}
}
}
......@@ -12,13 +12,13 @@ public class StringUtilsUniWue {
aCol.add(aColumn.toLowerCase());
}
}
public static String cleanStringFromSpecialCharacters(String extID) {
String result = extID;
result = result.replaceAll("[^a-zA-Z0-9_]", "_");
return result;
}
public static String concat(String[] someStrings, String separator) {
if (someStrings == null)
return null;
......@@ -76,4 +76,8 @@ public class StringUtilsUniWue {
return result;
}
public static String firstUpperCase(String aString) {
return aString.substring(0, 1).toUpperCase() + aString.substring(1);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment