Skip to content

Commit ca723f8

Browse files
committed
making stf8 format support configurable for providing backward compatibility between different serializers versions
1 parent 215df17 commit ca723f8

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessagePack.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ public static class PackerConfig
248248

249249
private int bufferSize = 8192;
250250

251+
private boolean str8FormatSupport = true;
252+
251253
public PackerConfig()
252254
{ }
253255

@@ -366,6 +368,23 @@ public int getBufferSize()
366368
{
367369
return bufferSize;
368370
}
371+
372+
/**
373+
* Disable str8 format when needed backward compatibility between
374+
* different msgpack serializer versions.
375+
* default true (str8 supported enabled)
376+
*/
377+
public PackerConfig withStr8FormatSupport(boolean str8FormatSupport)
378+
{
379+
PackerConfig copy = clone();
380+
copy.str8FormatSupport = str8FormatSupport;
381+
return copy;
382+
}
383+
384+
public boolean isStr8FormatSupport()
385+
{
386+
return str8FormatSupport;
387+
}
369388
}
370389

371390
/**

msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public class MessagePacker
8888

8989
private final int bufferFlushThreshold;
9090

91+
private final boolean str8FormatSupport;
92+
9193
protected MessageBufferOutput out;
9294

9395
private MessageBuffer buffer;
@@ -116,6 +118,7 @@ protected MessagePacker(MessageBufferOutput out, MessagePack.PackerConfig config
116118
this.out = checkNotNull(out, "MessageBufferOutput is null");
117119
this.smallStringOptimizationThreshold = config.getSmallStringOptimizationThreshold();
118120
this.bufferFlushThreshold = config.getBufferFlushThreshold();
121+
this.str8FormatSupport = config.isStr8FormatSupport();
119122
this.position = 0;
120123
this.totalFlushBytes = 0;
121124
}
@@ -667,7 +670,7 @@ public MessagePacker packRawStringHeader(int len)
667670
if (len < (1 << 5)) {
668671
writeByte((byte) (FIXSTR_PREFIX | len));
669672
}
670-
else if (len < (1 << 8)) {
673+
else if (str8FormatSupport && len < (1 << 8)) {
671674
writeByteAndByte(STR8, (byte) len);
672675
}
673676
else if (len < (1 << 16)) {

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackGeneratorTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import static org.junit.Assert.assertArrayEquals;
4747
import static org.junit.Assert.assertEquals;
4848
import static org.junit.Assert.assertFalse;
49+
import static org.junit.Assert.assertNotEquals;
4950
import static org.junit.Assert.assertTrue;
5051

5152
public class MessagePackGeneratorTest
@@ -435,4 +436,22 @@ public Exception call()
435436
}
436437
}
437438
}
439+
440+
@Test
441+
public void testDisableStr8Support()
442+
throws Exception
443+
{
444+
String str8LengthString = new String(new char[32]).replace("\0", "a");
445+
446+
// Test that produced value having str8 format
447+
ObjectMapper defaultMapper = new ObjectMapper(new MessagePackFactory());
448+
byte[] resultWithStr8Format = defaultMapper.writeValueAsBytes(str8LengthString);
449+
assertEquals(resultWithStr8Format[0], MessagePack.Code.STR8);
450+
451+
// Test that produced value does not having str8 format
452+
MessagePack.PackerConfig config = new MessagePack.PackerConfig().withStr8FormatSupport(false);
453+
ObjectMapper mapperWithConfig = new ObjectMapper(new MessagePackFactory(config));
454+
byte[] resultWithoutStr8Format = mapperWithConfig.writeValueAsBytes(str8LengthString);
455+
assertNotEquals(resultWithoutStr8Format[0], MessagePack.Code.STR8);
456+
}
438457
}

0 commit comments

Comments
 (0)