-
Notifications
You must be signed in to change notification settings - Fork 322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for jackson field ids #868
Open
brenbar
wants to merge
9
commits into
msgpack:main
Choose a base branch
from
brenbar:field-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−4
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
534fa03
Add test demonstrating field id case
brenbar 481c223
Fix java 8 error
brenbar d8a3716
Add missing import
brenbar 757b6f9
Add missing throws
brenbar 546d48b
Cleanup unused imports
brenbar 3c08b90
Cleanup test
brenbar 84cbc79
Implement jackson field ids
brenbar 8247f24
Address feedback from @komamitsu
brenbar a66fc2e
Address feedback from @komamitsu
brenbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...son/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatForFieldIdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// MessagePack for Java | ||
// | ||
// 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 | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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. | ||
// | ||
package org.msgpack.jackson.dataformat; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.KeyDeserializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.deser.NullValueProvider; | ||
import com.fasterxml.jackson.databind.deser.impl.JDKValueInstantiators; | ||
import com.fasterxml.jackson.databind.deser.std.MapDeserializer; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.LinkedHashMap; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class MessagePackDataformatForFieldIdTest | ||
{ | ||
static class MessagePackMapDeserializer extends MapDeserializer | ||
{ | ||
public static KeyDeserializer keyDeserializer = new KeyDeserializer() | ||
{ | ||
@Override | ||
public Object deserializeKey(String s, DeserializationContext deserializationContext) | ||
throws IOException | ||
{ | ||
JsonParser parser = deserializationContext.getParser(); | ||
if (parser instanceof MessagePackParser) { | ||
MessagePackParser p = (MessagePackParser) parser; | ||
if (p.isCurrentFieldId()) { | ||
return Integer.valueOf(s); | ||
} | ||
} | ||
return s; | ||
} | ||
}; | ||
|
||
public MessagePackMapDeserializer() | ||
{ | ||
super( | ||
TypeFactory.defaultInstance().constructMapType(Map.class, Object.class, Object.class), | ||
JDKValueInstantiators.findStdValueInstantiator(null, LinkedHashMap.class), | ||
keyDeserializer, null, null); | ||
} | ||
|
||
public MessagePackMapDeserializer(MapDeserializer src, KeyDeserializer keyDeser, | ||
JsonDeserializer<Object> valueDeser, TypeDeserializer valueTypeDeser, NullValueProvider nuller, | ||
Set<String> ignorable, Set<String> includable) | ||
{ | ||
super(src, keyDeser, valueDeser, valueTypeDeser, nuller, ignorable, includable); | ||
} | ||
|
||
@Override | ||
protected MapDeserializer withResolved(KeyDeserializer keyDeser, TypeDeserializer valueTypeDeser, | ||
JsonDeserializer<?> valueDeser, NullValueProvider nuller, Set<String> ignorable, | ||
Set<String> includable) | ||
{ | ||
return new MessagePackMapDeserializer(this, keyDeser, (JsonDeserializer<Object>) valueDeser, valueTypeDeser, | ||
nuller, ignorable, includable); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMixedKeys() | ||
throws IOException | ||
{ | ||
ObjectMapper mapper = new ObjectMapper(new MessagePackFactory()) | ||
.registerModule(new SimpleModule() | ||
.addDeserializer(Map.class, new MessagePackMapDeserializer())); | ||
|
||
Map<Object, Object> map = new HashMap<>(); | ||
map.put(1, "one"); | ||
map.put("2", "two"); | ||
|
||
byte[] bytes = mapper.writeValueAsBytes(map); | ||
Map<Object, Object> deserializedInit = mapper.readValue(bytes, new TypeReference<Map<Object, Object>>() {}); | ||
|
||
Map<Object, Object> expected = new HashMap<>(map); | ||
Map<Object, Object> actual = new HashMap<>(deserializedInit); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@komamitsu @xerial For backwards compatibility, I imagine we need some sort of feature flag that defaults to the prior implementation. Do you guys have any suggestions for how that flag should be defined? Should I just use the
MessagePack.PackerConfig
, or should I pass it directly from theMessagePackFactory
to theMessagePackGenerator
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, for the delayed reply.
PackerConfig
is for thecore
configuration and shouldn't be used forjackson-dataformat-msgpack
. I think it's okay to pass the flag likereuseResource
.