Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1188:9f5edbef3f55 | 1189:73d754b1889f |
---|---|
1 package luan.modules.parsers; | |
2 | |
3 import java.util.List; | |
4 import java.util.Map; | |
5 import luan.Luan; | |
6 import luan.LuanTable; | |
7 import luan.LuanException; | |
8 | |
9 | |
10 public final class LuanToString { | |
11 | |
12 public static String toString(Object obj,boolean strict) throws LuanException { | |
13 StringBuilder sb = new StringBuilder(); | |
14 new LuanToString(strict).toString(obj,sb,0); | |
15 return sb.toString(); | |
16 } | |
17 | |
18 private final boolean strict; | |
19 | |
20 private LuanToString(boolean strict) { | |
21 this.strict = strict; | |
22 } | |
23 | |
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) ); | |
35 return; | |
36 } | |
37 if( obj instanceof String ) { | |
38 toString((String)obj,sb); | |
39 return; | |
40 } | |
41 if( obj instanceof LuanTable ) { | |
42 toString((LuanTable)obj,sb,indented); | |
43 return; | |
44 } | |
45 if( strict ) | |
46 throw new LuanException("can't handle type "+obj.getClass().getName()); | |
47 sb.append( '<' ); | |
48 sb.append( obj ); | |
49 sb.append( '>' ); | |
50 } | |
51 | |
52 private void toString(final String s,StringBuilder sb) { | |
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( ", " ); | |
67 } | |
68 } | |
69 for( Object obj : map.entrySet() ) { | |
70 Map.Entry entry = (Map.Entry)obj; | |
71 indent(sb,indented+1); | |
72 toString(entry,sb,indented+1); | |
73 } | |
74 if( !list.isEmpty() || !map.isEmpty() ) | |
75 indent(sb,indented); | |
76 sb.append( '}' ); | |
77 return; | |
78 } | |
79 | |
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( ']' ); | |
88 } | |
89 sb.append( " = " ); | |
90 toString( entry.getValue(), sb, indented ); | |
91 } | |
92 | |
93 private void indent(StringBuilder sb,int indented) { | |
94 sb.append( '\n' ); | |
95 for( int i=0; i<indented; i++ ) { | |
96 sb.append( '\t' ); | |
97 } | |
98 } | |
99 | |
100 } |