------------------
9.1.0 (2024-02-29)
------------------
+* Implemented pretty print mode for MJsonArray and MJsonObject.
* Implemented automatic stripping of newline characters when loading JSON objects.
* Upgraded copyright notice to year 2024.
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
+ public static final String JSON_PRETTY_PRINT_MODE_EOL = "\r\n";
+ public static final int JSON_PRETTY_PRINT_MODE_INDENTATION = 2; // characters
+
/* Threads. */
public static final String LOG_FILTER_MARK_MESSAGE_APPENDER_THREAD_ID = "logFilterMarkMessageAppender";
package com.marcozanon.macaco.json;
+import com.marcozanon.macaco.MConstants;
import com.marcozanon.macaco.text.MText;
import java.util.LinkedList;
@Override
public String getJsonValue() {
+ return this.getJsonValue(false);
+ }
+
+ @Override
+ public String getJsonValue(boolean prettyPrintMode) {
+ return this.getJsonValue(prettyPrintMode, 0);
+ }
+
+ @Override
+ protected String getJsonValue(boolean prettyPrintMode, int leftMargin) {
StringBuilder s = new StringBuilder("");
//
s.append("[");
+ if (prettyPrintMode) {
+ s.append(MConstants.JSON_PRETTY_PRINT_MODE_EOL);
+ }
for (int i = 0; this.getValueCount() > i; i++) {
if (0 < i) {
- s.append(", ");
+ s.append(",");
+ if (prettyPrintMode) {
+ s.append(MConstants.JSON_PRETTY_PRINT_MODE_EOL);
+ }
+ else {
+ s.append(" ");
+ }
+ }
+ if (prettyPrintMode) {
+ for (int l = 0; (leftMargin + MConstants.JSON_PRETTY_PRINT_MODE_INDENTATION) > l; l++) {
+ s.append(" ");
+ }
+ }
+ s.append(this.getValues().get(i).getJsonValue(prettyPrintMode, leftMargin + MConstants.JSON_PRETTY_PRINT_MODE_INDENTATION));
+ }
+ if (prettyPrintMode) {
+ s.append(MConstants.JSON_PRETTY_PRINT_MODE_EOL);
+ for (int l = 0; leftMargin > l; l++) {
+ s.append(" ");
}
- s.append(this.getValues().get(i).getJsonValue());
}
s.append("]");
//
@Override
public String getJsonValue() {
- return this.getValue().toString();
+ return this.getJsonValue(false);
+ }
+
+ @Override
+ public String getJsonValue(boolean prettyPrintMode) {
+ return this.getJsonValue(prettyPrintMode, 0);
}
+ @Override
+ protected String getJsonValue(boolean prettyPrintMode, int leftMargin) {
+ return this.getValue().toString();
+ }
}
@Override
public String getJsonValue() {
- return "null";
+ return this.getJsonValue(false);
+ }
+
+ @Override
+ public String getJsonValue(boolean prettyPrintMode) {
+ return this.getJsonValue(prettyPrintMode, 0);
}
+ @Override
+ protected String getJsonValue(boolean prettyPrintMode, int leftMargin) {
+ return "null";
+ }
}
@Override
public String getJsonValue() {
- return this.getValue().toString();
+ return this.getJsonValue(false);
+ }
+
+ @Override
+ public String getJsonValue(boolean prettyPrintMode) {
+ return this.getJsonValue(prettyPrintMode, 0);
}
+ @Override
+ protected String getJsonValue(boolean prettyPrintMode, int leftMargin) {
+ return this.getValue().toString();
+ }
}
package com.marcozanon.macaco.json;
+import com.marcozanon.macaco.MConstants;
import com.marcozanon.macaco.text.MText;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@Override
public String getJsonValue() {
+ return this.getJsonValue(false);
+ }
+
+ @Override
+ public String getJsonValue(boolean prettyPrintMode) {
+ return this.getJsonValue(prettyPrintMode, 0);
+ }
+
+ @Override
+ protected String getJsonValue(boolean prettyPrintMode, int leftMargin) {
StringBuilder s = new StringBuilder("");
//
s.append("{");
+ if (prettyPrintMode) {
+ s.append(MConstants.JSON_PRETTY_PRINT_MODE_EOL);
+ }
int k = 0;
for (String key: this.getValues().keySet()) {
if (0 < k) {
- s.append(", ");
+ s.append(",");
+ if (prettyPrintMode) {
+ s.append(MConstants.JSON_PRETTY_PRINT_MODE_EOL);
+ }
+ else {
+ s.append(" ");
+ }
+ }
+ if (prettyPrintMode) {
+ for (int l = 0; (leftMargin + MConstants.JSON_PRETTY_PRINT_MODE_INDENTATION) > l; l++) {
+ s.append(" ");
+ }
}
s.append("\"" + MJsonString.getEscapedString(key, false) + "\"");
s.append(": ");
- s.append(this.getValues().get(key).getJsonValue());
+ s.append(this.getValues().get(key).getJsonValue(prettyPrintMode, leftMargin + MConstants.JSON_PRETTY_PRINT_MODE_INDENTATION));
k++;
}
+ if (prettyPrintMode) {
+ s.append(MConstants.JSON_PRETTY_PRINT_MODE_EOL);
+ for (int l = 0; leftMargin > l; l++) {
+ s.append(" ");
+ }
+ }
s.append("}");
//
return s.toString();
@Override
public String getJsonValue() {
- return "\"" + MJsonString.getEscapedString(this.getValue(), this.getExtendedEscapeMode()) + "\"";
+ return this.getJsonValue(false);
+ }
+
+ @Override
+ public String getJsonValue(boolean prettyPrintMode) {
+ return this.getJsonValue(prettyPrintMode, 0);
}
+ @Override
+ protected String getJsonValue(boolean prettyPrintMode, int leftMargin) {
+ return "\"" + MJsonString.getEscapedString(this.getValue(), this.getExtendedEscapeMode()) + "\"";
+ }
}
public abstract String getJsonValue();
+ public abstract String getJsonValue(boolean prettyPrintMode);
+
+ protected abstract String getJsonValue(boolean prettyPrintMode, int leftMargin);
+
}