Here you can find the source of serialize(Object obj)
public static String serialize(Object obj)
//package com.java2s; /**/*from ww w .ja va 2s. c o m*/ * This file is part of aion-lightning <aion-lightning.org>. * * aion-lightning is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * aion-lightning is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with aion-lightning. If not, see <http://www.gnu.org/licenses/>. */ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.StringWriter; public class Main { public static String serialize(Object obj) { try { JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter sw = new StringWriter(); m.marshal(obj, sw); return sw.toString(); } catch (JAXBException e) { throw new RuntimeException("Failed to marshall object of class " + obj.getClass().getName(), e); } } }