|
7 | 7 | import javax.xml.datatype.XMLGregorianCalendar;
|
8 | 8 | import javax.xml.namespace.QName;
|
9 | 9 |
|
| 10 | +import com.fasterxml.jackson.annotation.JsonFormat; |
10 | 11 | import com.fasterxml.jackson.core.*;
|
11 | 12 | import com.fasterxml.jackson.core.type.WritableTypeId;
|
12 | 13 | import com.fasterxml.jackson.databind.*;
|
@@ -34,9 +35,12 @@ public JsonSerializer<?> findSerializer(SerializationConfig config,
|
34 | 35 | JavaType type, BeanDescription beanDesc)
|
35 | 36 | {
|
36 | 37 | Class<?> raw = type.getRawClass();
|
37 |
| - if (Duration.class.isAssignableFrom(raw) || QName.class.isAssignableFrom(raw)) { |
| 38 | + if (Duration.class.isAssignableFrom(raw)) { |
38 | 39 | return ToStringSerializer.instance;
|
39 | 40 | }
|
| 41 | + if (QName.class.isAssignableFrom(raw)) { |
| 42 | + return QNameSerializer.instance; |
| 43 | + } |
40 | 44 | if (XMLGregorianCalendar.class.isAssignableFrom(raw)) {
|
41 | 45 | return XMLGregorianCalendarSerializer.instance;
|
42 | 46 | }
|
@@ -116,4 +120,73 @@ protected Calendar _convert(XMLGregorianCalendar input) {
|
116 | 120 | return (input == null) ? null : input.toGregorianCalendar();
|
117 | 121 | }
|
118 | 122 | }
|
| 123 | + |
| 124 | + /** |
| 125 | + * @since 2.19 |
| 126 | + */ |
| 127 | + public static class QNameSerializer |
| 128 | + extends StdSerializer<QName> |
| 129 | + implements ContextualSerializer |
| 130 | + { |
| 131 | + private static final long serialVersionUID = 1L; |
| 132 | + |
| 133 | + public final static JsonSerializer<?> instance = new QNameSerializer(); |
| 134 | + |
| 135 | + public QNameSerializer() { |
| 136 | + super(QName.class); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public JsonSerializer<?> createContextual(SerializerProvider serializers, BeanProperty property) |
| 141 | + throws JsonMappingException |
| 142 | + { |
| 143 | + JsonFormat.Value format = findFormatOverrides(serializers, property, handledType()); |
| 144 | + if (format != null) { |
| 145 | + JsonFormat.Shape shape = format.getShape(); |
| 146 | + if (shape == JsonFormat.Shape.OBJECT) { |
| 147 | + return this; |
| 148 | + } |
| 149 | + } |
| 150 | + return ToStringSerializer.instance; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void serialize(QName value, JsonGenerator g, SerializerProvider ctxt) |
| 155 | + throws IOException |
| 156 | + { |
| 157 | + g.writeStartObject(value); |
| 158 | + serializeProperties(value, g, ctxt); |
| 159 | + g.writeEndObject(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public final void serializeWithType(QName value, JsonGenerator g, SerializerProvider ctxt, |
| 164 | + TypeSerializer typeSer) |
| 165 | + throws IOException |
| 166 | + { |
| 167 | + WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, |
| 168 | + typeSer.typeId(value, JsonToken.START_OBJECT)); |
| 169 | + serializeProperties(value, g, ctxt); |
| 170 | + typeSer.writeTypeSuffix(g, typeIdDef); |
| 171 | + } |
| 172 | + |
| 173 | + private void serializeProperties(QName value, JsonGenerator g, SerializerProvider ctxt) |
| 174 | + throws IOException |
| 175 | + { |
| 176 | + g.writeStringField("localPart", value.getLocalPart()); |
| 177 | + if (!value.getNamespaceURI().isEmpty()) { |
| 178 | + g.writeStringField("namespaceURI", value.getNamespaceURI()); |
| 179 | + } |
| 180 | + if (!value.getPrefix().isEmpty()) { |
| 181 | + g.writeStringField("prefix", value.getPrefix()); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) |
| 187 | + throws JsonMappingException { |
| 188 | + /*JsonObjectFormatVisitor v =*/ visitor.expectObjectFormat(typeHint); |
| 189 | + // TODO: would need to visit properties too, see `BeanSerializerBase` |
| 190 | + } |
| 191 | + } |
119 | 192 | }
|
0 commit comments