|
1 |
| -/* |
2 |
| - * Copyright (C) 2016 Nameless Production Committee |
3 |
| - * |
4 |
| - * Licensed under the MIT License (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://opensource.org/licenses/mit-license.php |
9 |
| - */ |
10 |
| -package kiss; |
11 |
| - |
12 |
| -import java.io.IOException; |
13 |
| -import java.util.List; |
14 |
| -import java.util.concurrent.ConcurrentHashMap; |
15 |
| - |
16 |
| -import kiss.model.Model; |
17 |
| -import kiss.model.Property; |
18 |
| -import kiss.model.PropertyWalker; |
19 |
| - |
20 |
| -/** |
21 |
| - * <p> |
22 |
| - * JSON serializer for Java object graph. This serializer rejects cyclic node within ancestor nodes, |
23 |
| - * but same object in sibling nodes will be acceptable. |
24 |
| - * </p> |
25 |
| - * |
26 |
| - * @version 2010/01/12 20:32:11 |
27 |
| - */ |
28 |
| -class JSON implements PropertyWalker { |
29 |
| - |
30 |
| - /** The record for traversed objects. */ |
31 |
| - private final ConcurrentHashMap reference = new ConcurrentHashMap(); |
32 |
| - |
33 |
| - /** The charcter sequence for output as JSON. */ |
34 |
| - private final Appendable out; |
35 |
| - |
36 |
| - /** The flag whether the current property is the first item in context or not. */ |
37 |
| - private boolean first = true; |
38 |
| - |
39 |
| - /** |
40 |
| - * JSON serializer. |
41 |
| - * |
42 |
| - * @param out An output target. |
43 |
| - */ |
44 |
| - JSON(Appendable out) { |
45 |
| - this.out = out; |
46 |
| - } |
47 |
| - |
48 |
| - /** |
49 |
| - * {@inheritDoc} |
50 |
| - */ |
51 |
| - @Override |
52 |
| - public void walk(Model model, Property property, Object node) { |
53 |
| - if (!property.isTransient) { |
54 |
| - try { |
55 |
| - // ======================================== |
56 |
| - // Enter Node |
57 |
| - // ======================================== |
58 |
| - // check whether this is first property in current context or not. |
59 |
| - if (first) { |
60 |
| - // mark as not first |
61 |
| - first = false; |
62 |
| - } else { |
63 |
| - // write property seperator |
64 |
| - out.append(','); |
65 |
| - } |
66 |
| - |
67 |
| - // write property key (root node and List node doesn't need key) |
68 |
| - if (reference.size() != 0 && model.type != List.class) { |
69 |
| - write(property.name); |
70 |
| - out.append(':'); |
71 |
| - } |
72 |
| - |
73 |
| - // write property value |
74 |
| - if (property.isAttribute()) { |
75 |
| - write(I.transform(node, String.class)); |
76 |
| - } else { |
77 |
| - // check cyclic node (non-attribute node only apply this check) |
78 |
| - if (reference.putIfAbsent(node, 0) != null) { |
79 |
| - throw new ClassCircularityError(reference.toString()); |
80 |
| - } else { |
81 |
| - // write suitable brace |
82 |
| - out.append(property.model.type == List.class ? '[' : '{'); |
83 |
| - |
84 |
| - // reset next context |
85 |
| - first = true; |
86 |
| - } |
87 |
| - |
88 |
| - // ======================================== |
89 |
| - // Traverse Child Node |
90 |
| - // ======================================== |
91 |
| - property.model.walk(node, this); |
92 |
| - |
93 |
| - // ======================================== |
94 |
| - // Leave Node |
95 |
| - // ======================================== |
96 |
| - // unregister non-attribute node |
97 |
| - reference.remove(node); |
98 |
| - |
99 |
| - // write suitable brace |
100 |
| - out.append(property.model.type == List.class ? ']' : '}'); |
101 |
| - } |
102 |
| - } catch (IOException e) { |
103 |
| - throw I.quiet(e); |
104 |
| - } |
105 |
| - } |
106 |
| - } |
107 |
| - |
108 |
| - /** |
109 |
| - * <p> |
110 |
| - * Write JSON literal with quote. |
111 |
| - * </p> |
112 |
| - * |
113 |
| - * @param value A character sequence. |
114 |
| - * @throws IOException |
115 |
| - */ |
116 |
| - private void write(String value) throws IOException { |
117 |
| - out.append('"'); |
118 |
| - |
119 |
| - for (int i = 0; i < value.length(); i++) { |
120 |
| - char c = value.charAt(i); |
121 |
| - |
122 |
| - switch (c) { |
123 |
| - case '"': |
124 |
| - out.append("\\\""); |
125 |
| - break; |
126 |
| - |
127 |
| - case '\\': |
128 |
| - out.append("\\\\"); |
129 |
| - break; |
130 |
| - |
131 |
| - case '\b': |
132 |
| - out.append("\\b"); |
133 |
| - break; |
134 |
| - |
135 |
| - case '\f': |
136 |
| - out.append("\\f"); |
137 |
| - break; |
138 |
| - |
139 |
| - case '\n': |
140 |
| - out.append("\\n"); |
141 |
| - break; |
142 |
| - |
143 |
| - case '\r': |
144 |
| - out.append("\\r"); |
145 |
| - break; |
146 |
| - |
147 |
| - case '\t': |
148 |
| - out.append("\\t"); |
149 |
| - break; |
150 |
| - |
151 |
| - default: |
152 |
| - out.append(c); |
153 |
| - } |
154 |
| - } |
155 |
| - |
156 |
| - out.append('"'); |
157 |
| - } |
158 |
| -} |
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Nameless Production Committee |
| 3 | + * |
| 4 | + * Licensed under the MIT License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://opensource.org/licenses/mit-license.php |
| 9 | + */ |
| 10 | +package kiss; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.ConcurrentHashMap; |
| 15 | + |
| 16 | +import kiss.model.Model; |
| 17 | +import kiss.model.Property; |
| 18 | +import kiss.model.PropertyWalker; |
| 19 | + |
| 20 | +/** |
| 21 | + * <p> |
| 22 | + * JSON serializer for Java object graph. This serializer rejects cyclic node within ancestor nodes, |
| 23 | + * but same object in sibling nodes will be acceptable. |
| 24 | + * </p> |
| 25 | + * |
| 26 | + * @version 2016/03/15 18:10:13 |
| 27 | + */ |
| 28 | +class JSON implements PropertyWalker { |
| 29 | + |
| 30 | + /** The record for traversed objects. */ |
| 31 | + private final ConcurrentHashMap reference = new ConcurrentHashMap(); |
| 32 | + |
| 33 | + /** The charcter sequence for output as JSON. */ |
| 34 | + private final Appendable out; |
| 35 | + |
| 36 | + /** The flag whether the current property is the first item in context or not. */ |
| 37 | + private boolean first = true; |
| 38 | + |
| 39 | + /** |
| 40 | + * JSON serializer. |
| 41 | + * |
| 42 | + * @param out An output target. |
| 43 | + */ |
| 44 | + JSON(Appendable out) { |
| 45 | + this.out = out; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritDoc} |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public void walk(Model model, Property property, Object node) { |
| 53 | + if (!property.isTransient) { |
| 54 | + try { |
| 55 | + // ======================================== |
| 56 | + // Enter Node |
| 57 | + // ======================================== |
| 58 | + // check whether this is first property in current context or not. |
| 59 | + if (first) { |
| 60 | + // mark as not first |
| 61 | + first = false; |
| 62 | + } else { |
| 63 | + // write property seperator |
| 64 | + out.append(','); |
| 65 | + } |
| 66 | + |
| 67 | + // write property key (root node and List node doesn't need key) |
| 68 | + if (reference.size() != 0 && model.type != List.class) { |
| 69 | + write(property.name); |
| 70 | + out.append(':'); |
| 71 | + } |
| 72 | + |
| 73 | + // write property value |
| 74 | + if (property.isAttribute()) { |
| 75 | + write(I.transform(node, String.class)); |
| 76 | + } else { |
| 77 | + // check cyclic node (non-attribute node only apply this check) |
| 78 | + if (reference.putIfAbsent(node, 0) != null) { |
| 79 | + throw new ClassCircularityError(reference.toString()); |
| 80 | + } else { |
| 81 | + // write suitable brace |
| 82 | + out.append(property.model.type == List.class ? '[' : '{'); |
| 83 | + } |
| 84 | + |
| 85 | + // ======================================== |
| 86 | + // Traverse Child Node |
| 87 | + // ======================================== |
| 88 | + boolean store = first; // store the first property state |
| 89 | + first = true; |
| 90 | + |
| 91 | + property.model.walk(node, this); |
| 92 | + |
| 93 | + first = store; // restore the first property state |
| 94 | + |
| 95 | + // ======================================== |
| 96 | + // Leave Node |
| 97 | + // ======================================== |
| 98 | + // unregister non-attribute node |
| 99 | + reference.remove(node); |
| 100 | + |
| 101 | + // write suitable brace |
| 102 | + out.append(property.model.type == List.class ? ']' : '}'); |
| 103 | + } |
| 104 | + } catch (IOException e) { |
| 105 | + throw I.quiet(e); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * <p> |
| 112 | + * Write JSON literal with quote. |
| 113 | + * </p> |
| 114 | + * |
| 115 | + * @param value A character sequence. |
| 116 | + * @throws IOException |
| 117 | + */ |
| 118 | + private void write(String value) throws IOException { |
| 119 | + out.append('"'); |
| 120 | + |
| 121 | + for (int i = 0; i < value.length(); i++) { |
| 122 | + char c = value.charAt(i); |
| 123 | + |
| 124 | + switch (c) { |
| 125 | + case '"': |
| 126 | + out.append("\\\""); |
| 127 | + break; |
| 128 | + |
| 129 | + case '\\': |
| 130 | + out.append("\\\\"); |
| 131 | + break; |
| 132 | + |
| 133 | + case '\b': |
| 134 | + out.append("\\b"); |
| 135 | + break; |
| 136 | + |
| 137 | + case '\f': |
| 138 | + out.append("\\f"); |
| 139 | + break; |
| 140 | + |
| 141 | + case '\n': |
| 142 | + out.append("\\n"); |
| 143 | + break; |
| 144 | + |
| 145 | + case '\r': |
| 146 | + out.append("\\r"); |
| 147 | + break; |
| 148 | + |
| 149 | + case '\t': |
| 150 | + out.append("\\t"); |
| 151 | + break; |
| 152 | + |
| 153 | + default: |
| 154 | + out.append(c); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + out.append('"'); |
| 159 | + } |
| 160 | +} |
0 commit comments