Mercurial Hosting > luan
annotate src/luan/modules/parsers/LuanToString.java @ 1189:73d754b1889f
add stringify
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 23 Feb 2018 01:40:04 -0700 |
parents | src/luan/lib/json/JsonToString.java@b70102bab110 |
children | db7d31f4089a |
rev | line source |
---|---|
1189 | 1 package luan.modules.parsers; |
720 | 2 |
3 import java.util.List; | |
4 import java.util.Map; | |
1189 | 5 import luan.Luan; |
6 import luan.LuanTable; | |
7 import luan.LuanException; | |
720 | 8 |
9 | |
1189 | 10 public final class LuanToString { |
720 | 11 |
1189 | 12 public static String toString(Object obj,boolean strict) throws LuanException { |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
13 StringBuilder sb = new StringBuilder(); |
1189 | 14 new LuanToString(strict).toString(obj,sb,0); |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
15 return sb.toString(); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
16 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
17 |
1189 | 18 private final boolean strict; |
19 | |
20 private LuanToString(boolean strict) { | |
21 this.strict = strict; | |
1132
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
22 } |
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
23 |
1189 | 24 private void toString(Object obj,StringBuilder sb,int indented) throws LuanException { |
25 if( obj == null ) { | |
26 sb.append( "nil" ); | |
27 return; | |
28 } | |
29 if( obj instanceof Boolean ) { | |
30 sb.append( obj ); | |
31 return; | |
32 } | |
33 if( obj instanceof Number ) { | |
34 sb.append( Luan.toString((Number)obj) ); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
35 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
36 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
37 if( obj instanceof String ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
38 toString((String)obj,sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
39 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
40 } |
1189 | 41 if( obj instanceof LuanTable ) { |
42 toString((LuanTable)obj,sb,indented); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
43 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
44 } |
1189 | 45 if( strict ) |
46 throw new LuanException("can't handle type "+obj.getClass().getName()); | |
47 sb.append( '<' ); | |
48 sb.append( obj ); | |
49 sb.append( '>' ); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
50 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
51 |
1132
b70102bab110
add json_compressed_string()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1117
diff
changeset
|
52 private void toString(final String s,StringBuilder sb) { |
1189 | 53 sb.append( '"' ); |
54 sb.append( Luan.stringEncode(s) ); | |
55 sb.append( '"' ); | |
56 } | |
57 | |
58 private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException { | |
59 List list = tbl.asList(); | |
60 Map map = tbl.rawMap(); | |
61 sb.append( '{' ); | |
62 if( !list.isEmpty() ) { | |
63 indent(sb,indented+1); | |
64 for( Object obj : list ) { | |
65 toString(obj,sb,indented+1); | |
66 sb.append( ", " ); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
67 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
68 } |
1189 | 69 for( Object obj : map.entrySet() ) { |
70 Map.Entry entry = (Map.Entry)obj; | |
1116 | 71 indent(sb,indented+1); |
1189 | 72 toString(entry,sb,indented+1); |
73 } | |
74 if( !list.isEmpty() || !map.isEmpty() ) | |
1116 | 75 indent(sb,indented); |
1189 | 76 sb.append( '}' ); |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
77 return; |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
78 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
79 |
1189 | 80 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws LuanException { |
81 Object key = entry.getKey(); | |
82 if( key instanceof String && ((String)key).matches("[a-zA-Z_][a-zA-Z_0-9]*") ) { | |
83 sb.append( (String)key ); | |
84 } else { | |
85 sb.append( '[' ); | |
86 toString( key, sb, indented ); | |
87 sb.append( ']' ); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
88 } |
1189 | 89 sb.append( " = " ); |
90 toString( entry.getValue(), sb, indented ); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
91 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
92 |
1189 | 93 private void indent(StringBuilder sb,int indented) { |
94 sb.append( '\n' ); | |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
95 for( int i=0; i<indented; i++ ) { |
1189 | 96 sb.append( '\t' ); |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
97 } |
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
98 } |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
99 |
720 | 100 } |