Search in sources :

Example 1 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project metro-jax-ws by eclipse-ee4j.

the class JAXBBond method marshal.

// TODO NamespaceContext nsContext
@Override
public void marshal(T object, OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException {
    JAXBMarshaller marshaller = null;
    try {
        marshaller = parent.mpool.allocate();
        marshaller.setAttachmentMarshaller(am);
        marshaller.setProperty(jakarta.xml.bind.Marshaller.JAXB_FRAGMENT, true);
        if (mappingInfo != null) {
            if (isParameterizedType) {
                JAXBTypeElement jte = new JAXBTypeElement(mappingInfo.getXmlTagName(), object, (ParameterizedType) mappingInfo.getType());
                marshaller.marshal(jte, new StreamResult(output), mappingInfo);
            } else {
                JAXBElement<T> elt = new JAXBElement<>(mappingInfo.getXmlTagName(), (Class<T>) mappingInfo.getType(), object);
                // marshaller.marshal(elt, output);
                // GAG missing
                marshaller.marshal(elt, new StreamResult(output), mappingInfo);
            }
        } else {
            marshaller.marshal(object, output);
        }
    } finally {
        if (marshaller != null) {
            marshaller.setAttachmentMarshaller(null);
            parent.mpool.replace(marshaller);
        }
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) JAXBTypeElement(org.eclipse.persistence.jaxb.JAXBTypeElement) JAXBElement(jakarta.xml.bind.JAXBElement) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 2 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project metro-jax-ws by eclipse-ee4j.

the class JAXBBond method marshal.

@Override
public void marshal(T object, Node output) throws JAXBException {
    JAXBMarshaller marshaller = null;
    try {
        marshaller = parent.mpool.allocate();
        // marshaller.setAttachmentMarshaller(am);
        marshaller.setProperty(jakarta.xml.bind.Marshaller.JAXB_FRAGMENT, true);
        if (mappingInfo != null) {
            if (isParameterizedType) {
                JAXBTypeElement jte = new JAXBTypeElement(mappingInfo.getXmlTagName(), object, (ParameterizedType) mappingInfo.getType());
                marshaller.marshal(jte, new DOMResult(output), mappingInfo);
            } else {
                JAXBElement<T> elt = new JAXBElement<>(mappingInfo.getXmlTagName(), (Class<T>) mappingInfo.getType(), object);
                // marshaller.marshal(elt, output);
                marshaller.marshal(elt, new DOMResult(output), mappingInfo);
            }
        } else {
            marshaller.marshal(object, output);
        }
    } finally {
        if (marshaller != null) {
            marshaller.setAttachmentMarshaller(null);
            parent.mpool.replace(marshaller);
        }
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) JAXBTypeElement(org.eclipse.persistence.jaxb.JAXBTypeElement) JAXBElement(jakarta.xml.bind.JAXBElement) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 3 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project metro-jax-ws by eclipse-ee4j.

the class JAXBBond method marshal.

@Override
public void marshal(T object, Result result) throws JAXBException {
    JAXBMarshaller marshaller = null;
    try {
        marshaller = parent.mpool.allocate();
        marshaller.setAttachmentMarshaller(null);
        marshaller.setProperty(jakarta.xml.bind.Marshaller.JAXB_FRAGMENT, true);
        if (mappingInfo != null) {
            if (isParameterizedType) {
                JAXBTypeElement jte = new JAXBTypeElement(mappingInfo.getXmlTagName(), object, (ParameterizedType) mappingInfo.getType());
                marshaller.marshal(jte, result, mappingInfo);
            } else {
                JAXBElement<T> elt = new JAXBElement<>(mappingInfo.getXmlTagName(), (Class<T>) mappingInfo.getType(), object);
                // marshaller.marshal(elt, result);
                marshaller.marshal(elt, result, mappingInfo);
            }
        } else {
            TypeMappingInfo tmi = null;
            if (object instanceof JAXBElement) {
                QName q = ((JAXBElement) object).getName();
                JAXBContext ctx = (JAXBContext) parent.getJAXBContext();
                Map<TypeMappingInfo, QName> mtq = ctx.getTypeMappingInfoToSchemaType();
                for (Map.Entry<TypeMappingInfo, QName> es : mtq.entrySet()) {
                    if (q.equals(es.getValue())) {
                        tmi = es.getKey();
                        break;
                    }
                }
            }
            if (tmi != null) {
                marshaller.marshal(object, result, tmi);
            } else {
                marshaller.marshal(object, result);
            }
        }
    } finally {
        if (marshaller != null) {
            marshaller.setAttachmentMarshaller(null);
            parent.mpool.replace(marshaller);
        }
    }
}
Also used : JAXBTypeElement(org.eclipse.persistence.jaxb.JAXBTypeElement) QName(javax.xml.namespace.QName) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) Map(java.util.Map)

Example 4 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project aai-aai-common by onap.

the class PojoUtils method getJsonFromDynamicObject.

/**
 * Gets the json from dynamic object.
 *
 * @param ent the ent
 * @param jaxbContext the jaxb context
 * @param includeRoot the include root
 * @return the json from dynamic object
 * @throws JsonGenerationException the json generation exception
 * @throws JsonMappingException the json mapping exception
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws JAXBException the JAXB exception
 */
public String getJsonFromDynamicObject(DynamicEntity ent, org.eclipse.persistence.jaxb.JAXBContext jaxbContext, boolean includeRoot) throws JsonGenerationException, JsonMappingException, IOException, JAXBException {
    JAXBMarshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, false);
    marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, Boolean.FALSE);
    marshaller.setProperty("eclipselink.json.include-root", includeRoot);
    marshaller.setProperty("eclipselink.media-type", "application/json");
    StringWriter writer = new StringWriter();
    marshaller.marshal(ent, writer);
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 5 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project aai-aai-common by onap.

the class PojoUtilsTest method testGetJsonFromDynamicObject.

@Test
public void testGetJsonFromDynamicObject() throws Exception {
    DynamicEntity dynamicEntity = Mockito.mock(DynamicEntity.class);
    JAXBContext jaxbContext = Mockito.mock(JAXBContext.class);
    JAXBMarshaller marshaller = Mockito.mock(JAXBMarshaller.class);
    Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
    String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
    assertEquals("", output);
}
Also used : DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller) Test(org.junit.Test)

Aggregations

JAXBMarshaller (org.eclipse.persistence.jaxb.JAXBMarshaller)41 HashMap (java.util.HashMap)16 StringWriter (java.io.StringWriter)14 JAXBUnmarshaller (org.eclipse.persistence.jaxb.JAXBUnmarshaller)7 JAXBContext (jakarta.xml.bind.JAXBContext)6 JAXBElement (jakarta.xml.bind.JAXBElement)6 BeanValidationException (org.eclipse.persistence.exceptions.BeanValidationException)6 Map (java.util.Map)5 ConstraintViolationWrapper (org.eclipse.persistence.jaxb.ConstraintViolationWrapper)5 JAXBContext (org.eclipse.persistence.jaxb.JAXBContext)5 JAXBTypeElement (org.eclipse.persistence.jaxb.JAXBTypeElement)5 Marshaller (jakarta.xml.bind.Marshaller)4 StringReader (java.io.StringReader)4 StreamResult (javax.xml.transform.stream.StreamResult)4 QName (javax.xml.namespace.QName)3 TypeMappingInfo (org.eclipse.persistence.jaxb.TypeMappingInfo)3 XmlList (jakarta.xml.bind.annotation.XmlList)2 Annotation (java.lang.annotation.Annotation)2 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)2 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)2