Description
serialize
License
EUPL
Parameter
Parameter | Description |
---|
obj | a parameter |
Exception
Parameter | Description |
---|
JAXBException | an exception |
Declaration
public static byte[] serialize(Object obj) throws JAXBException
Method Source Code
//package com.java2s;
/*/*from w w w . ja v a 2 s. c o m*/
* Copyright 2015, Supreme Court Republic of Slovenia
*
* Licensed under the EUPL, Version 1.1 or ? as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in
* compliance with the Licence. You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the Licence
* is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the Licence for the specific language governing permissions and limitations under
* the Licence.
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.Writer;
import static java.lang.Boolean.TRUE;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import static javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT;
import static javax.xml.bind.Marshaller.JAXB_FRAGMENT;
import org.w3c.dom.Document;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
public class Main {
/**
*
* @param obj
* @return
* @throws JAXBException
*/
public static byte[] serialize(Object obj) throws JAXBException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
serialize(obj, bos);
return bos.toByteArray();
}
/**
*
* @param obj
* @param os
* @throws JAXBException
*/
public static void serialize(Object obj, OutputStream os) throws JAXBException {
final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
m.setProperty(JAXB_FRAGMENT, TRUE);
m.marshal(obj, os);
}
/**
*
* @param obj
* @param filename
* @throws JAXBException
* @throws FileNotFoundException
*/
public static void serialize(Object obj, String filename) throws JAXBException, FileNotFoundException {
final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
m.setProperty(JAXB_FRAGMENT, TRUE);
m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
m.marshal(obj, new FileOutputStream(filename));
}
/**
*
* @param obj
* @param file
* @throws JAXBException
* @throws FileNotFoundException
*/
public static void serialize(Object obj, File file) throws JAXBException, FileNotFoundException {
final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
m.setProperty(JAXB_FRAGMENT, TRUE);
m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
m.marshal(obj, new FileOutputStream(file));
}
/**
*
* @param obj
* @param w
* @throws JAXBException
* @throws FileNotFoundException
*/
public static void serialize(Object obj, Writer w) throws JAXBException, FileNotFoundException {
final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
m.setProperty(JAXB_FRAGMENT, TRUE);
m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
m.marshal(obj, w);
}
public static boolean serialize(Document doc, boolean setXmlDecl, File f) throws FileNotFoundException {
DOMImplementationLS lsImpl = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = lsImpl.createLSSerializer();
serializer.getDomConfig().setParameter("xml-declaration", setXmlDecl); // set it to false to get
LSOutput output = lsImpl.createLSOutput();
output.setByteStream(new FileOutputStream(f));
return serializer.write(doc, output);
}
}
Related
- serialize(final Object object)
- serialize(JAXBElement emp, Class clazz, OutputStream out)
- serialize(JAXBElement object)
- serialize(Object o, OutputStream os, Boolean format)
- serialize(Object o, OutputStream os, Boolean format)
- serialize(Object obj)
- serialize(Object object, Class clazz, String filename)
- serialize(T object, Class objectClass, OutputStream resultStream)
- serialize(T object, Path path)