Skip to content

Commit ab62c4d

Browse files
committed
Add TSFBuilder support via MessagePackFactoryBuilder
Implement MessagePackFactoryBuilder extending DecorableTSFBuilder, wire it into MessagePackFactory via a builder constructor and rebuild(), and fix snapshot() to return a copy instead of this. Add tests covering rebuild() and snapshot() behavior.
1 parent c33d896 commit ab62c4d

3 files changed

Lines changed: 195 additions & 1 deletion

File tree

msgpack-jackson3/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public MessagePackFactory(MessagePackFactory src)
7575
}
7676
}
7777

78+
protected MessagePackFactory(MessagePackFactoryBuilder b)
79+
{
80+
super(b);
81+
this.packerConfig = b.packerConfig().clone();
82+
this.reuseResourceInGenerator = b.reuseResourceInGenerator();
83+
this.reuseResourceInParser = b.reuseResourceInParser();
84+
this.supportIntegerKeys = b.supportIntegerKeys();
85+
this.extTypeCustomDesers = b.extTypeCustomDesers();
86+
}
87+
7888
public MessagePackFactory setReuseResourceInGenerator(boolean reuseResourceInGenerator)
7989
{
8090
this.reuseResourceInGenerator = reuseResourceInGenerator;
@@ -172,7 +182,7 @@ public TokenStreamFactory snapshot()
172182
@Override
173183
public TSFBuilder<?, ?> rebuild()
174184
{
175-
throw new UnsupportedOperationException("MessagePackFactory does not support TSFBuilder yet");
185+
return new MessagePackFactoryBuilder(this);
176186
}
177187

178188
@Override
@@ -187,12 +197,24 @@ MessagePack.PackerConfig getPackerConfig()
187197
return packerConfig;
188198
}
189199

200+
@VisibleForTesting
201+
boolean isReuseResourceInGenerator()
202+
{
203+
return reuseResourceInGenerator;
204+
}
205+
190206
@VisibleForTesting
191207
boolean isReuseResourceInParser()
192208
{
193209
return reuseResourceInParser;
194210
}
195211

212+
@VisibleForTesting
213+
boolean isSupportIntegerKeys()
214+
{
215+
return supportIntegerKeys;
216+
}
217+
196218
@VisibleForTesting
197219
ExtensionTypeCustomDeserializers getExtTypeCustomDesers()
198220
{
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 tools.jackson.core.ErrorReportConfiguration;
19+
import tools.jackson.core.StreamReadConstraints;
20+
import tools.jackson.core.StreamWriteConstraints;
21+
import tools.jackson.core.base.DecorableTSFactory;
22+
import org.msgpack.core.MessagePack;
23+
24+
public class MessagePackFactoryBuilder
25+
extends DecorableTSFactory.DecorableTSFBuilder<MessagePackFactory, MessagePackFactoryBuilder>
26+
{
27+
private MessagePack.PackerConfig packerConfig;
28+
private boolean reuseResourceInGenerator;
29+
private boolean reuseResourceInParser;
30+
private boolean supportIntegerKeys;
31+
private ExtensionTypeCustomDeserializers extTypeCustomDesers;
32+
33+
public MessagePackFactoryBuilder()
34+
{
35+
super(StreamReadConstraints.defaults(), StreamWriteConstraints.defaults(),
36+
ErrorReportConfiguration.defaults(), 0, 0);
37+
this.packerConfig = MessagePack.DEFAULT_PACKER_CONFIG;
38+
this.reuseResourceInGenerator = true;
39+
this.reuseResourceInParser = true;
40+
this.supportIntegerKeys = false;
41+
}
42+
43+
public MessagePackFactoryBuilder(MessagePackFactory base)
44+
{
45+
super(base);
46+
this.packerConfig = base.getPackerConfig().clone();
47+
this.reuseResourceInGenerator = base.isReuseResourceInGenerator();
48+
this.reuseResourceInParser = base.isReuseResourceInParser();
49+
this.supportIntegerKeys = base.isSupportIntegerKeys();
50+
this.extTypeCustomDesers = base.getExtTypeCustomDesers();
51+
}
52+
53+
public MessagePackFactoryBuilder packerConfig(MessagePack.PackerConfig config)
54+
{
55+
this.packerConfig = config;
56+
return this;
57+
}
58+
59+
public MessagePackFactoryBuilder reuseResourceInGenerator(boolean v)
60+
{
61+
this.reuseResourceInGenerator = v;
62+
return this;
63+
}
64+
65+
public MessagePackFactoryBuilder reuseResourceInParser(boolean v)
66+
{
67+
this.reuseResourceInParser = v;
68+
return this;
69+
}
70+
71+
public MessagePackFactoryBuilder supportIntegerKeys(boolean v)
72+
{
73+
this.supportIntegerKeys = v;
74+
return this;
75+
}
76+
77+
public MessagePackFactoryBuilder extTypeCustomDesers(ExtensionTypeCustomDeserializers desers)
78+
{
79+
this.extTypeCustomDesers = desers;
80+
return this;
81+
}
82+
83+
public MessagePack.PackerConfig packerConfig()
84+
{
85+
return packerConfig;
86+
}
87+
88+
public boolean reuseResourceInGenerator()
89+
{
90+
return reuseResourceInGenerator;
91+
}
92+
93+
public boolean reuseResourceInParser()
94+
{
95+
return reuseResourceInParser;
96+
}
97+
98+
public boolean supportIntegerKeys()
99+
{
100+
return supportIntegerKeys;
101+
}
102+
103+
public ExtensionTypeCustomDeserializers extTypeCustomDesers()
104+
{
105+
return extTypeCustomDesers;
106+
}
107+
108+
@Override
109+
public MessagePackFactory build()
110+
{
111+
return new MessagePackFactory(this);
112+
}
113+
}

msgpack-jackson3/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import tools.jackson.core.JsonEncoding;
1919
import tools.jackson.core.JsonGenerator;
2020
import tools.jackson.core.JsonParser;
21+
import tools.jackson.core.TSFBuilder;
2122
import tools.jackson.core.TokenStreamFactory;
2223
import tools.jackson.core.type.TypeReference;
2324
import tools.jackson.databind.ObjectMapper;
@@ -30,8 +31,10 @@
3031

3132
import static org.hamcrest.CoreMatchers.instanceOf;
3233
import static org.hamcrest.CoreMatchers.is;
34+
import static org.hamcrest.CoreMatchers.not;
3335
import static org.hamcrest.CoreMatchers.notNullValue;
3436
import static org.hamcrest.CoreMatchers.nullValue;
37+
import static org.hamcrest.CoreMatchers.sameInstance;
3538
import static org.junit.Assert.assertEquals;
3639
import static org.hamcrest.MatcherAssert.assertThat;
3740

@@ -86,6 +89,62 @@ public void testCopyWithDefaultConfig()
8689
assertThat(deserialized.get("one"), is(1));
8790
}
8891

92+
@Test
93+
public void testRebuildWithDefaultConfig()
94+
throws IOException
95+
{
96+
MessagePackFactory messagePackFactory = new MessagePackFactory();
97+
TSFBuilder<?, ?> builder = messagePackFactory.rebuild();
98+
assertThat(builder, is(instanceOf(MessagePackFactoryBuilder.class)));
99+
100+
MessagePackFactory rebuilt = (MessagePackFactory) builder.build();
101+
assertThat(rebuilt, is(not(sameInstance(messagePackFactory))));
102+
assertThat(rebuilt.getPackerConfig().isStr8FormatSupport(), is(true));
103+
assertThat(rebuilt.getExtTypeCustomDesers(), is(nullValue()));
104+
105+
ObjectMapper rebuiltObjectMapper = new MessagePackMapper(rebuilt);
106+
byte[] bytes = rebuiltObjectMapper.writeValueAsBytes(42);
107+
assertThat(rebuiltObjectMapper.readValue(bytes, Integer.class), is(42));
108+
}
109+
110+
@Test
111+
public void testRebuildWithAdvancedConfig()
112+
throws IOException
113+
{
114+
ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers();
115+
extTypeCustomDesers.addCustomDeser((byte) 42,
116+
new ExtensionTypeCustomDeserializers.Deser()
117+
{
118+
@Override
119+
public Object deserialize(byte[] data)
120+
throws IOException
121+
{
122+
TinyPojo pojo = new TinyPojo();
123+
pojo.t = new String(data);
124+
return pojo;
125+
}
126+
}
127+
);
128+
MessagePack.PackerConfig packerConfig = new MessagePack.PackerConfig().withStr8FormatSupport(false);
129+
MessagePackFactory messagePackFactory = new MessagePackFactory(packerConfig);
130+
messagePackFactory.setExtTypeCustomDesers(extTypeCustomDesers);
131+
132+
MessagePackFactory rebuilt = (MessagePackFactory) messagePackFactory.rebuild().build();
133+
assertThat(rebuilt, is(not(sameInstance(messagePackFactory))));
134+
assertThat(rebuilt.getPackerConfig().isStr8FormatSupport(), is(false));
135+
assertThat(rebuilt.getExtTypeCustomDesers().getDeser((byte) 42), is(notNullValue()));
136+
assertThat(rebuilt.getExtTypeCustomDesers().getDeser((byte) 43), is(nullValue()));
137+
}
138+
139+
@Test
140+
public void testSnapshotReturnsNewInstance()
141+
{
142+
MessagePackFactory messagePackFactory = new MessagePackFactory();
143+
TokenStreamFactory snapshot = messagePackFactory.snapshot();
144+
assertThat(snapshot, is(not(sameInstance(messagePackFactory))));
145+
assertThat(snapshot, is(instanceOf(MessagePackFactory.class)));
146+
}
147+
89148
@Test
90149
public void testCopyWithAdvancedConfig()
91150
throws IOException

0 commit comments

Comments
 (0)