Mercurial Hosting > luan
changeset 1132:b70102bab110
add json_compressed_string()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 02 Jan 2018 21:30:41 -0700 |
parents | 46732cc0ab87 |
children | ba4daf107e07 |
files | src/luan/lib/json/JsonToString.java src/luan/modules/Parsers.luan |
diffstat | 2 files changed, 22 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
diff -r 46732cc0ab87 -r b70102bab110 src/luan/lib/json/JsonToString.java --- a/src/luan/lib/json/JsonToString.java Thu Dec 14 01:38:47 2017 -0700 +++ b/src/luan/lib/json/JsonToString.java Tue Jan 02 21:30:41 2018 -0700 @@ -5,7 +5,7 @@ import java.util.Iterator; -public final class JsonToString { +public class JsonToString { public static final class JsonException extends RuntimeException { private JsonException(String msg) { @@ -15,12 +15,21 @@ public static String toString(Object obj) throws JsonException { StringBuilder sb = new StringBuilder(); - toString(obj,sb,0); + new JsonToString().toString(obj,sb,0); sb.append('\n'); return sb.toString(); } - private static void toString(Object obj,StringBuilder sb,int indented) throws JsonException { + public static String toCompressedString(Object obj) throws JsonException { + StringBuilder sb = new StringBuilder(); + JsonToString jts = new JsonToString() { + void indent(StringBuilder sb,int indented) {} + }; + jts.toString(obj,sb,0); + return sb.toString(); + } + + private void toString(Object obj,StringBuilder sb,int indented) throws JsonException { if( obj == null || obj instanceof Boolean || obj instanceof Number ) { sb.append(obj); return; @@ -40,7 +49,7 @@ throw new JsonException("can't handle type "+obj.getClass().getName()); } - private static void toString(final String s,StringBuilder sb) { + private void toString(final String s,StringBuilder sb) { sb.append('"'); for( int i=0; i<s.length(); i++ ) { char c = s.charAt(i); @@ -73,7 +82,7 @@ sb.append('"'); } - private static void toString(List list,StringBuilder sb,int indented) { + private void toString(List list,StringBuilder sb,int indented) { sb.append('['); if( !list.isEmpty() ) { indent(sb,indented+1); @@ -88,7 +97,7 @@ return; } - private static void toString(Map map,StringBuilder sb,int indented) throws JsonException { + private void toString(Map map,StringBuilder sb,int indented) throws JsonException { sb.append('{'); if( !map.isEmpty() ) { Iterator<Map.Entry> i = map.entrySet().iterator(); @@ -104,7 +113,7 @@ sb.append('}'); } - private static void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { + private void toString(Map.Entry entry,StringBuilder sb,int indented) throws JsonException { Object key = entry.getKey(); if( !(key instanceof String) ) throw new JsonException("table keys must be strings"); @@ -113,12 +122,12 @@ toString(entry.getValue(),sb,indented); } - private static void indent(StringBuilder sb,int indented) { + void indent(StringBuilder sb,int indented) { sb.append('\n'); for( int i=0; i<indented; i++ ) { sb.append('\t'); } } - private JsonToString() {} // never + private JsonToString() {} }
diff -r 46732cc0ab87 -r b70102bab110 src/luan/modules/Parsers.luan --- a/src/luan/modules/Parsers.luan Thu Dec 14 01:38:47 2017 -0700 +++ b/src/luan/modules/Parsers.luan Tue Jan 02 21:30:41 2018 -0700 @@ -25,4 +25,8 @@ return JsonToString.toString(Luan.toJava(obj)) end +function Parsers.json_compressed_string(obj) + return JsonToString.toCompressedString(Luan.toJava(obj)) +end + return Parsers