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 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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,13 @@ private void writeByteArrayTextKey(byte[] text, int offset, int len) throws IOEx
addValueNode(new String(text, offset, len, DEFAULT_CHARSET));
}

// TODO: Uncomment
//@Override
//public void writeFieldId(long id) throws IOException
//{
// addKeyToStackTop(id);
brenbar marked this conversation as resolved.
Show resolved Hide resolved
//}

@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();
}

// TODO: Uncomment
//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,115 @@
//
// 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.databind.ObjectMapper;
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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collections;
import java.util.LinkedHashMap;

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 p) {
// TODO: Uncomment
//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 mixed()
{
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> deserialized = mapper.readValue(bytes, new TypeReference<Map<Object, Object>>() {});

assertEquals(map, deserialized);
}
}
Loading