-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathCompactMapWriter.java
145 lines (128 loc) · 5.4 KB
/
CompactMapWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.cedarsoftware.io.writers;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.Set;
import com.cedarsoftware.io.JsonWriter;
import com.cedarsoftware.io.WriterContext;
import com.cedarsoftware.util.CompactMap;
import com.cedarsoftware.util.ReflectionUtils;
import static com.cedarsoftware.io.JsonWriter.JsonClassWriter;
/**
* Writer for CompactMap instances that produces a shortened configuration format.
*
* @author John DeRegnaucourt ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class CompactMapWriter implements JsonClassWriter {
@Override
public void write(Object obj, boolean showType, Writer output, WriterContext context) throws IOException {
CompactMap map = (CompactMap) obj;
// Get configuration values
boolean caseSensitive = !((boolean) ReflectionUtils.call(map, "isCaseInsensitive"));
int compactSize = (int) ReflectionUtils.call(map, "compactSize");
String ordering = (String) ReflectionUtils.call(map, "getOrdering");
String singleKey = (String) ReflectionUtils.call(map, "getSingleValueKey");
// Get the map implementation class - simplified approach
String mapImplClass;
try {
// Use getNewMap() to get the actual map type
Map<?, ?> newMap = (Map<?, ?>) ReflectionUtils.call(map, "getNewMap");
mapImplClass = newMap.getClass().getName();
} catch (Exception e) {
// Fallback to HashMap if we can't get the map type
mapImplClass = "java.util.HashMap";
}
// Generate shortened config string: mapClassFullName/CS|CI/S{size}/{singleKey}/{order}
StringBuilder config = new StringBuilder();
config.append(mapImplClass).append('/');
config.append(caseSensitive ? "CS" : "CI").append('/');
config.append('S').append(compactSize).append('/');
config.append(singleKey == null ? "-" : singleKey).append('/');
// Map ordering codes to short form
String orderCode;
switch (ordering) {
case CompactMap.SORTED:
orderCode = "Sort";
break;
case CompactMap.REVERSE:
orderCode = "Rev";
break;
case CompactMap.INSERTION:
orderCode = "Ins";
break;
default:
orderCode = "Unord";
}
config.append(orderCode);
// Write shortened config
output.write("\"config\":\"" + config + "\",");
// Write data section
output.write("\"data\":{");
// Check if all keys are strings and not forcing two arrays
boolean allStringKeys = true;
if (!context.getWriteOptions().isForceMapOutputAsTwoArrays()) {
Set<Map.Entry<Object, Object>> entries = ((Map<Object, Object>) map).entrySet();
for (Map.Entry<Object, Object> entry : entries) {
if (!(entry.getKey() instanceof String)) {
allStringKeys = false;
break;
}
}
} else {
allStringKeys = false; // Force array format if setting is enabled
}
// Write entries in appropriate format
if (allStringKeys) {
// Standard JSON object format for string keys
boolean first = true;
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) map).entrySet()) {
if (first) {
first = false;
} else {
output.write(',');
}
String key = (String) entry.getKey();
JsonWriter.writeJsonUtf8String(output, key);
output.write(':');
context.writeImpl(entry.getValue(), showType);
}
} else {
// Use json-io's standard @keys/@items format for non-string keys
Set<Map.Entry<Object, Object>> entries = ((Map<Object, Object>) map).entrySet();
int size = entries.size();
Object[] keys = new Object[size];
Object[] values = new Object[size];
int i = 0;
for (Map.Entry<Object, Object> entry : entries) {
keys[i] = entry.getKey();
values[i] = entry.getValue();
i++;
}
// Write @keys array
output.write("\"@keys\":");
context.writeImpl(keys, showType);
// Write @items array
output.write(",\"@items\":");
context.writeImpl(values, showType);
}
output.write("}");
}
@Override
public String getTypeName(Object o) {
return CompactMap.class.getName();
}
}