Mercurial Hosting > luan
annotate src/luan/modules/parsers/LuanToString.java @ 1190:db7d31f4089a
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 23 Feb 2018 15:48:15 -0700 |
parents | 73d754b1889f |
children | 2024d23ddd64 |
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 ) { |
1190 | 38 sb.append( '"' ); |
39 sb.append( Luan.stringEncode((String)obj) ); | |
40 sb.append( '"' ); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
41 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
42 } |
1189 | 43 if( obj instanceof LuanTable ) { |
44 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
|
45 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
46 } |
1189 | 47 if( strict ) |
48 throw new LuanException("can't handle type "+obj.getClass().getName()); | |
49 sb.append( '<' ); | |
50 sb.append( obj ); | |
51 sb.append( '>' ); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
52 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
53 |
1189 | 54 private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException { |
55 List list = tbl.asList(); | |
56 Map map = tbl.rawMap(); | |
57 sb.append( '{' ); | |
58 if( !list.isEmpty() ) { | |
59 indent(sb,indented+1); | |
60 for( Object obj : list ) { | |
61 toString(obj,sb,indented+1); | |
62 sb.append( ", " ); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
63 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
64 } |
1189 | 65 for( Object obj : map.entrySet() ) { |
66 Map.Entry entry = (Map.Entry)obj; | |
1116 | 67 indent(sb,indented+1); |
1189 | 68 toString(entry,sb,indented+1); |
69 } | |
70 if( !list.isEmpty() || !map.isEmpty() ) | |
1116 | 71 indent(sb,indented); |
1189 | 72 sb.append( '}' ); |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
73 return; |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
74 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
75 |
1189 | 76 private void toString(Map.Entry entry,StringBuilder sb,int indented) throws LuanException { |
77 Object key = entry.getKey(); | |
78 if( key instanceof String && ((String)key).matches("[a-zA-Z_][a-zA-Z_0-9]*") ) { | |
79 sb.append( (String)key ); | |
80 } else { | |
81 sb.append( '[' ); | |
82 toString( key, sb, indented ); | |
83 sb.append( ']' ); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
84 } |
1189 | 85 sb.append( " = " ); |
86 toString( entry.getValue(), sb, indented ); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
87 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
88 |
1189 | 89 private void indent(StringBuilder sb,int indented) { |
90 sb.append( '\n' ); | |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
91 for( int i=0; i<indented; i++ ) { |
1189 | 92 sb.append( '\t' ); |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
93 } |
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
94 } |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
95 |
720 | 96 } |