Show time string instead of ms in JSON structures

This commit is contained in:
Bluefox 2020-08-22 17:17:43 +02:00 committed by GitHub
parent e7627f4f6c
commit f224cb1af1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,9 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.util.Iterator; import java.util.Iterator;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
@ -84,6 +87,7 @@ public class JSONFormatter {
private String visit(final Object object, final int indent) throws JSONException { private String visit(final Object object, final int indent) throws JSONException {
String ret = ""; String ret = "";
Long n;
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
ret += visit((JSONArray) object, indent); ret += visit((JSONArray) object, indent);
} else if (object instanceof JSONObject) { } else if (object instanceof JSONObject) {
@ -92,7 +96,19 @@ public class JSONFormatter {
if (object instanceof String) { if (object instanceof String) {
ret += write("\"" + ((String) object).replace("<", "&lt;").replace(">", "&gt;") + "\"", indent); ret += write("\"" + ((String) object).replace("<", "&lt;").replace(">", "&gt;") + "\"", indent);
} else { } else {
ret += write(String.valueOf(object), indent); // try to detect Date as milliseconds
if (object instanceof Long) {
n = (Long) object;
if (n > 1580000000000L && n < 2000000000000L) { // from 2020.01.26 to 2033.05.18 it is with high probability a date object
Date date = new Date(n);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ret += write(formatter.format(date), indent);
} else {
ret += write(String.valueOf(object), indent);
}
} else {
ret += write(String.valueOf(object), indent);
}
} }
} }
return ret; return ret;