Skip to content
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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,12 @@ private void writeByteArrayTextKey(byte[] text, int offset, int len) throws IOEx
addValueNode(new String(text, offset, len, DEFAULT_CHARSET));
}

@Override
public void writeFieldId(long id) throws IOException
{
addKeyNode(id);
Copy link
Author

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 the MessagePackFactory to the MessagePackGenerator?

Suggested change
addKeyNode(id);
if (writeIntegerMapKeysAsStringKeys) {
super.writeFieldId(id);
} else {
addKeyNode(id);
}

Copy link
Member

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 the core configuration and shouldn't be used for jackson-dataformat-msgpack. I think it's okay to pass the flag like reuseResource.

}

@Override
public void writeFieldName(String name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,11 @@ public String currentName()
return streamReadContext.getCurrentName();
}

public boolean isCurrentFieldId()
{
return this.type == Type.INT || this.type == Type.LONG;
}

@Override
public String getCurrentName()
throws IOException
Expand Down
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);
}
}
Loading