
org.gedcomx.rt.json.ExtensibleObjectSerializer Maven / Gradle / Ivy
/**
* Copyright Intellectual Reserve, Inc.
*
* 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.gedcomx.rt.json;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanSerializer;
import org.gedcomx.rt.GedcomNamespaceManager;
import org.gedcomx.rt.SupportsExtensionAttributes;
import org.gedcomx.rt.SupportsExtensionElements;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import jakarta.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Custom JSON serializer for @XmlAnyElement fields/properties
*
* @author Ryan Heaton
*/
public class ExtensibleObjectSerializer extends BeanSerializer {
public ExtensibleObjectSerializer(BeanSerializer src) {
super(src);
}
@Override
protected void serializeFields(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
super.serializeFields(bean, jgen, provider);
if (bean instanceof SupportsExtensionAttributes) {
serializeExtensionAttributes((SupportsExtensionAttributes) bean, jgen, provider);
}
if (bean instanceof SupportsExtensionElements) {
serializeExtensionElements((SupportsExtensionElements) bean, jgen, provider);
}
}
@Override
protected void serializeFieldsFiltered(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
super.serializeFieldsFiltered(bean, jgen, provider);
if (bean instanceof SupportsExtensionAttributes) {
serializeExtensionAttributes((SupportsExtensionAttributes) bean, jgen, provider);
}
if (bean instanceof SupportsExtensionElements) {
serializeExtensionElements((SupportsExtensionElements) bean, jgen, provider);
}
}
private void serializeExtensionAttributes(SupportsExtensionAttributes value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
Map extensionAttributes = value.getExtensionAttributes();
if (extensionAttributes != null) {
for (Map.Entry attr : extensionAttributes.entrySet()) {
jgen.writeStringField(attr.getKey().getNamespaceURI() + attr.getKey().getLocalPart(), attr.getValue());
}
}
}
public void serializeExtensionElements(SupportsExtensionElements value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy