Skip to content

Commit 567f2a6

Browse files
committed
Merge pull request msgpack#361 from komamitsu/non-str-key
Support non-string key in MessagePackGenerator
2 parents ba9d992 + e198ffd commit 567f2a6

File tree

5 files changed

+472
-16
lines changed

5 files changed

+472
-16
lines changed

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import com.fasterxml.jackson.core.Base64Variant;
1919
import com.fasterxml.jackson.core.JsonGenerationException;
2020
import com.fasterxml.jackson.core.ObjectCodec;
21+
import com.fasterxml.jackson.core.SerializableString;
2122
import com.fasterxml.jackson.core.base.GeneratorBase;
23+
import com.fasterxml.jackson.core.io.SerializedString;
2224
import com.fasterxml.jackson.core.json.JsonWriteContext;
2325
import org.msgpack.core.MessagePack;
2426
import org.msgpack.core.MessagePacker;
@@ -45,17 +47,17 @@ public class MessagePackGenerator
4547

4648
private abstract static class StackItem
4749
{
48-
protected List<String> objectKeys = new ArrayList<String>();
50+
protected List<Object> objectKeys = new ArrayList<Object>();
4951
protected List<Object> objectValues = new ArrayList<Object>();
5052

51-
abstract void addKey(String key);
53+
abstract void addKey(Object key);
5254

5355
void addValue(Object value)
5456
{
5557
objectValues.add(value);
5658
}
5759

58-
abstract List<String> getKeys();
60+
abstract List<Object> getKeys();
5961

6062
List<Object> getValues()
6163
{
@@ -67,13 +69,13 @@ private static class StackItemForObject
6769
extends StackItem
6870
{
6971
@Override
70-
void addKey(String key)
72+
void addKey(Object key)
7173
{
7274
objectKeys.add(key);
7375
}
7476

7577
@Override
76-
List<String> getKeys()
78+
List<Object> getKeys()
7779
{
7880
return objectKeys;
7981
}
@@ -83,13 +85,13 @@ private static class StackItemForArray
8385
extends StackItem
8486
{
8587
@Override
86-
void addKey(String key)
88+
void addKey(Object key)
8789
{
8890
throw new IllegalStateException("This method shouldn't be called");
8991
}
9092

9193
@Override
92-
List<String> getKeys()
94+
List<Object> getKeys()
9395
{
9496
throw new IllegalStateException("This method shouldn't be called");
9597
}
@@ -164,7 +166,7 @@ public void writeEndObject()
164166
popStackAndStoreTheItemAsValue();
165167
}
166168

167-
private void packValue(Object v)
169+
private void pack(Object v)
168170
throws IOException
169171
{
170172
MessagePacker messagePacker = getMessagePacker();
@@ -256,16 +258,15 @@ private void packBigDecimal(BigDecimal decimal)
256258
private void packObject(StackItemForObject stackItem)
257259
throws IOException
258260
{
259-
List<String> keys = stackItem.getKeys();
261+
List<Object> keys = stackItem.getKeys();
260262
List<Object> values = stackItem.getValues();
261263

262264
MessagePacker messagePacker = getMessagePacker();
263265
messagePacker.packMapHeader(keys.size());
264266

265267
for (int i = 0; i < keys.size(); i++) {
266-
messagePacker.packString(keys.get(i));
267-
Object v = values.get(i);
268-
packValue(v);
268+
pack(keys.get(i));
269+
pack(values.get(i));
269270
}
270271
}
271272

@@ -279,7 +280,7 @@ private void packArray(StackItemForArray stackItem)
279280

280281
for (int i = 0; i < values.size(); i++) {
281282
Object v = values.get(i);
282-
packValue(v);
283+
pack(v);
283284
}
284285
}
285286

@@ -290,6 +291,22 @@ public void writeFieldName(String name)
290291
addKeyToStackTop(name);
291292
}
292293

294+
@Override
295+
public void writeFieldName(SerializableString name)
296+
throws IOException
297+
{
298+
if (name instanceof MessagePackSerializedString) {
299+
addKeyToStackTop(((MessagePackSerializedString) name).getRawValue());
300+
}
301+
else if (name instanceof SerializedString) {
302+
addKeyToStackTop(name.getValue());
303+
}
304+
else {
305+
System.out.println(name.getClass());
306+
throw new IllegalArgumentException("Unsupported key: " + name);
307+
}
308+
}
309+
293310
@Override
294311
public void writeString(String text)
295312
throws IOException, JsonGenerationException
@@ -504,7 +521,7 @@ private StackItemForArray getStackTopForArray()
504521
return (StackItemForArray) stackTop;
505522
}
506523

507-
private void addKeyToStackTop(String key)
524+
private void addKeyToStackTop(Object key)
508525
{
509526
getStackTop().addKey(key);
510527
}
@@ -513,7 +530,7 @@ private void addValueToStackTop(Object value)
513530
throws IOException
514531
{
515532
if (stack.isEmpty()) {
516-
packValue(value);
533+
pack(value);
517534
flushMessagePacker();
518535
}
519536
else {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.jackson.dataformat;
17+
18+
import com.fasterxml.jackson.core.JsonGenerationException;
19+
import com.fasterxml.jackson.core.JsonGenerator;
20+
import com.fasterxml.jackson.databind.SerializerProvider;
21+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
22+
23+
import java.io.IOException;
24+
25+
public class MessagePackKeySerializer
26+
extends StdSerializer<Object>
27+
{
28+
public MessagePackKeySerializer()
29+
{
30+
super(Object.class);
31+
}
32+
33+
@Override
34+
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
35+
throws JsonGenerationException, IOException
36+
{
37+
jgen.writeFieldName(new MessagePackSerializedString(value));
38+
}
39+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.jackson.dataformat;
17+
18+
import com.fasterxml.jackson.core.SerializableString;
19+
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
import java.nio.ByteBuffer;
23+
import java.nio.charset.Charset;
24+
25+
public class MessagePackSerializedString
26+
implements SerializableString
27+
{
28+
private static final Charset UTF8 = Charset.forName("UTF-8");
29+
private final Object value;
30+
31+
public MessagePackSerializedString(Object value)
32+
{
33+
this.value = value;
34+
}
35+
36+
@Override
37+
public String getValue()
38+
{
39+
return value.toString();
40+
}
41+
42+
@Override
43+
public int charLength()
44+
{
45+
return getValue().length();
46+
}
47+
48+
@Override
49+
public char[] asQuotedChars()
50+
{
51+
return getValue().toCharArray();
52+
}
53+
54+
@Override
55+
public byte[] asUnquotedUTF8()
56+
{
57+
return getValue().getBytes(UTF8);
58+
}
59+
60+
@Override
61+
public byte[] asQuotedUTF8()
62+
{
63+
return ("\"" + getValue() + "\"").getBytes(UTF8);
64+
}
65+
66+
@Override
67+
public int appendQuotedUTF8(byte[] bytes, int i)
68+
{
69+
return 0;
70+
}
71+
72+
@Override
73+
public int appendQuoted(char[] chars, int i)
74+
{
75+
return 0;
76+
}
77+
78+
@Override
79+
public int appendUnquotedUTF8(byte[] bytes, int i)
80+
{
81+
return 0;
82+
}
83+
84+
@Override
85+
public int appendUnquoted(char[] chars, int i)
86+
{
87+
return 0;
88+
}
89+
90+
@Override
91+
public int writeQuotedUTF8(OutputStream outputStream)
92+
throws IOException
93+
{
94+
return 0;
95+
}
96+
97+
@Override
98+
public int writeUnquotedUTF8(OutputStream outputStream)
99+
throws IOException
100+
{
101+
return 0;
102+
}
103+
104+
@Override
105+
public int putQuotedUTF8(ByteBuffer byteBuffer)
106+
throws IOException
107+
{
108+
return 0;
109+
}
110+
111+
@Override
112+
public int putUnquotedUTF8(ByteBuffer byteBuffer)
113+
throws IOException
114+
{
115+
return 0;
116+
}
117+
118+
public Object getRawValue()
119+
{
120+
return value;
121+
}
122+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.jackson.dataformat;
17+
18+
import com.fasterxml.jackson.databind.JavaType;
19+
import com.fasterxml.jackson.databind.JsonSerializer;
20+
import com.fasterxml.jackson.databind.SerializationConfig;
21+
import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig;
22+
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
23+
24+
public class MessagePackSerializerFactory
25+
extends BeanSerializerFactory
26+
{
27+
/**
28+
* Constructor for creating instances with specified configuration.
29+
*
30+
* @param config
31+
*/
32+
protected MessagePackSerializerFactory(SerializerFactoryConfig config)
33+
{
34+
super(config);
35+
}
36+
37+
@Override
38+
public JsonSerializer<Object> createKeySerializer(SerializationConfig config, JavaType keyType, JsonSerializer<Object> defaultImpl)
39+
{
40+
return new MessagePackKeySerializer();
41+
}
42+
}

0 commit comments

Comments
 (0)