Skip to content

Commit 9aae392

Browse files
committed
Merge pull request #95 from pgelinas/custom-object-id-serialization
Enables object id serialization with type information.
2 parents 136e5d3 + af3fee5 commit 9aae392

File tree

3 files changed

+68
-39
lines changed

3 files changed

+68
-39
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlBeanSerializer.java

+28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.fasterxml.jackson.databind.ser.BeanSerializer;
1313
import com.fasterxml.jackson.databind.ser.PropertyFilter;
1414
import com.fasterxml.jackson.databind.ser.impl.ObjectIdWriter;
15+
import com.fasterxml.jackson.databind.ser.impl.WritableObjectId;
1516
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;
1617
import com.fasterxml.jackson.databind.util.NameTransformer;
1718
import com.fasterxml.jackson.dataformat.xml.util.XmlInfo;
@@ -249,6 +250,11 @@ public void serializeWithType(Object bean, JsonGenerator jgen, SerializerProvide
249250
TypeSerializer typeSer)
250251
throws IOException, JsonGenerationException
251252
{
253+
if (_objectIdWriter != null) {
254+
_serializeWithObjectId(bean, jgen, provider, typeSer);
255+
return;
256+
}
257+
252258
/* Ok: let's serialize type id as attribute, but if (and only if!)
253259
* we are using AS_PROPERTY
254260
*/
@@ -264,6 +270,28 @@ public void serializeWithType(Object bean, JsonGenerator jgen, SerializerProvide
264270
}
265271
}
266272

273+
@Override
274+
protected void _serializeObjectId(Object bean,
275+
JsonGenerator jgen,
276+
SerializerProvider provider,
277+
TypeSerializer typeSer,
278+
WritableObjectId objectId) throws IOException, JsonProcessingException,
279+
JsonGenerationException {
280+
/* Ok: let's serialize type id as attribute, but if (and only if!)
281+
* we are using AS_PROPERTY
282+
*/
283+
if (typeSer.getTypeInclusion() == JsonTypeInfo.As.PROPERTY) {
284+
ToXmlGenerator xgen = (ToXmlGenerator)jgen;
285+
xgen.setNextIsAttribute(true);
286+
super._serializeObjectId(bean, jgen, provider, typeSer, objectId);
287+
if (_attributeCount == 0) { // if no attributes, need to reset
288+
xgen.setNextIsAttribute(false);
289+
}
290+
} else {
291+
super._serializeObjectId(bean, jgen, provider, typeSer, objectId);
292+
}
293+
}
294+
267295
/*
268296
/**********************************************************
269297
/* Helper methods

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/TestPolymorphic.java

+2-36
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.fasterxml.jackson.dataformat.xml.failing;
22

3-
import java.util.*;
4-
5-
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
63
import com.fasterxml.jackson.annotation.JsonTypeInfo;
7-
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
8-
import com.fasterxml.jackson.dataformat.xml.*;
4+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
5+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
96

107
public class TestPolymorphic extends XmlTestBase
118
{
@@ -56,20 +53,6 @@ public ClassArrayWrapper() { }
5653
public ClassArrayWrapper(String s) { wrapped = new SubTypeWithClassArray(s); }
5754
}
5855

59-
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
60-
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
61-
protected static class TypeWithClassPropertyAndObjectId {
62-
public String id;
63-
64-
public TypeWithClassPropertyAndObjectId(String id) { this.id = id; }
65-
}
66-
67-
protected static class Wrapper {
68-
public List<TypeWithClassPropertyAndObjectId> data;
69-
70-
public Wrapper(List<TypeWithClassPropertyAndObjectId> data) { this.data = data; }
71-
}
72-
7356
/*
7457
/**********************************************************
7558
/* Set up
@@ -114,22 +97,5 @@ public void testAsWrappedClassArray() throws Exception
11497
assertEquals(SubTypeWithClassArray.class, result.wrapped.getClass());
11598
assertEquals("Foobar", ((SubTypeWithClassArray) result.wrapped).name);
11699
}
117-
118-
/**
119-
* Test for issue 81
120-
*/
121-
public void testAsPropertyWithObjectId() throws Exception
122-
{
123-
List<TypeWithClassPropertyAndObjectId> data = new ArrayList<TestPolymorphic.TypeWithClassPropertyAndObjectId>();
124-
TypeWithClassPropertyAndObjectId object = new TypeWithClassPropertyAndObjectId("Foobar");
125-
data.add(object);
126-
// This will be written as an id reference instead of object; as such, no type info will be written.
127-
data.add(object);
128-
String xml = _xmlMapper.writeValueAsString(new Wrapper(data));
129-
Wrapper result = _xmlMapper.readValue(xml, Wrapper.class);
130-
assertNotNull(result);
131-
assertSame(result.data.get(0), result.data.get(1));
132-
assertEquals("Foobar", result.data.get(0).id);
133-
}
134100
}
135101

src/test/java/com/fasterxml/jackson/dataformat/xml/types/TestPolymorphic.java

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.fasterxml.jackson.dataformat.xml.types;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
37
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8+
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
49
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
510
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
611

@@ -22,9 +27,6 @@ public SubTypeWithClassProperty() { }
2227
public SubTypeWithClassProperty(String s) { name = s; }
2328
}
2429

25-
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.WRAPPER_ARRAY)
26-
static class BaseTypeWithClassArray { }
27-
2830
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.WRAPPER_OBJECT)
2931
protected static class BaseTypeWithClassObject { }
3032

@@ -35,6 +37,22 @@ public SubTypeWithClassObject() { }
3537
public SubTypeWithClassObject(String s) { name = s; }
3638
}
3739

40+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
41+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
42+
protected static class TypeWithClassPropertyAndObjectId {
43+
public String id;
44+
45+
public TypeWithClassPropertyAndObjectId() {}
46+
public TypeWithClassPropertyAndObjectId(String id) { this.id = id; }
47+
}
48+
49+
protected static class Wrapper {
50+
public List<TypeWithClassPropertyAndObjectId> data;
51+
52+
public Wrapper(){}
53+
public Wrapper(List<TypeWithClassPropertyAndObjectId> data) { this.data = data; }
54+
}
55+
3856
/*
3957
/**********************************************************
4058
/* Set up
@@ -85,5 +103,22 @@ public void testAsClassObject() throws Exception
85103
assertEquals(SubTypeWithClassObject.class, result.getClass());
86104
assertEquals("Foobar", ((SubTypeWithClassObject) result).name);
87105
}
106+
107+
/**
108+
* Test for issue 81
109+
*/
110+
public void testAsPropertyWithObjectId() throws Exception
111+
{
112+
List<TypeWithClassPropertyAndObjectId> data = new ArrayList<TestPolymorphic.TypeWithClassPropertyAndObjectId>();
113+
TypeWithClassPropertyAndObjectId object = new TypeWithClassPropertyAndObjectId("Foobar");
114+
data.add(object);
115+
// This will be written as an id reference instead of object; as such, no type info will be written.
116+
data.add(object);
117+
String xml = _xmlMapper.writeValueAsString(new Wrapper(data));
118+
Wrapper result = _xmlMapper.readValue(xml, Wrapper.class);
119+
assertNotNull(result);
120+
assertSame(result.data.get(0), result.data.get(1));
121+
assertEquals("Foobar", result.data.get(0).id);
122+
}
88123
}
89124

0 commit comments

Comments
 (0)