001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.core.io.registry; 019 020import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; 021 022import java.io.ByteArrayInputStream; 023import java.io.ByteArrayOutputStream; 024import java.io.IOException; 025import java.lang.reflect.Type; 026import java.util.List; 027 028import org.apache.commons.lang3.reflect.TypeUtils; 029import org.nuxeo.ecm.core.io.registry.context.RenderingContext; 030import org.nuxeo.ecm.core.io.registry.context.RenderingContext.CtxBuilder; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * Quick use of {@link MarshallerRegistry}. 035 * 036 * @since 7.2 037 */ 038public final class MarshallerHelper { 039 040 /** 041 * Just call static methods. 042 */ 043 private MarshallerHelper() { 044 } 045 046 private static MarshallerRegistry getService() { 047 MarshallerRegistry registry = Framework.getService(MarshallerRegistry.class); 048 return registry; 049 } 050 051 /** 052 * Checks the marshallers isn't null. Throw an exception if it is. 053 */ 054 private static void checkMarshaller(Type type, Marshaller<?> marshaller) { 055 if (marshaller == null) { 056 throw new MarshallingException("No marshaller found for type " + type.toString()); 057 } 058 } 059 060 /** 061 * Convert the given object to json. 062 * 063 * @param object The object to convert as json. 064 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 065 * @return the resulting json. 066 * @since 7.2 067 */ 068 public static <T> String objectToJson(T object, RenderingContext ctx) throws IOException { 069 @SuppressWarnings("unchecked") 070 Class<T> type = (Class<T>) object.getClass(); 071 Writer<T> writer = getService().getWriter(ctx, type, APPLICATION_JSON_TYPE); 072 checkMarshaller(type, writer); 073 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 074 writer.write(object, type, type, APPLICATION_JSON_TYPE, baos); 075 return baos.toString(); 076 } 077 078 /** 079 * Convert the given object to json. 080 * <p> 081 * Specify its generic type to be sure to get the best marshaller to manage it. 082 * </p> 083 * 084 * @param genericType The generic type of the object. You can easily create parametrize type using 085 * {@link TypeUtils#parameterize(Class, Type...)} 086 * @param object The object to convert as json. 087 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 088 * @return the resulting json. 089 * @since 7.2 090 */ 091 public static <T> String objectToJson(Type genericType, T object, RenderingContext ctx) throws IOException { 092 @SuppressWarnings("unchecked") 093 Class<T> type = (Class<T>) object.getClass(); 094 Writer<T> writer = getService().getWriter(ctx, type, genericType, APPLICATION_JSON_TYPE); 095 checkMarshaller(genericType, writer); 096 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 097 writer.write(object, type, genericType, APPLICATION_JSON_TYPE, baos); 098 return baos.toString(); 099 } 100 101 /** 102 * Convert the given list to json. 103 * <p> 104 * Specify the list element type to get the best marshaller to manage conversion. 105 * </p> 106 * 107 * @param elementType The element type of the list. 108 * @param list The list to convert. 109 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 110 * @return the resulting json. 111 * @since 7.2 112 */ 113 public static <T> String listToJson(Class<T> elementType, List<T> list, RenderingContext ctx) throws IOException { 114 Type genericType = TypeUtils.parameterize(List.class, elementType); 115 @SuppressWarnings("rawtypes") 116 Writer<List> writer = getService().getWriter(ctx, List.class, genericType, APPLICATION_JSON_TYPE); 117 checkMarshaller(genericType, writer); 118 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 119 writer.write(list, List.class, genericType, APPLICATION_JSON_TYPE, baos); 120 return baos.toString(); 121 } 122 123 /** 124 * Read an object of the given type from given json. 125 * 126 * @param type The type of the read object. 127 * @param json The json to parse. 128 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 129 * @return the resulting object. 130 * @since 7.2 131 */ 132 public static <T> T objectToJson(Class<T> type, String json, RenderingContext ctx) throws IOException { 133 Reader<T> reader = getService().getReader(ctx, type, APPLICATION_JSON_TYPE); 134 checkMarshaller(type, reader); 135 return reader.read(type, type, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes())); 136 } 137 138 /** 139 * Read an object of the given type from given json. 140 * <p> 141 * Specify its generic type to be sure to get the best marshaller to manage it. 142 * </p> 143 * 144 * @param type The type of the read object. 145 * @param genericType The generic type of the object. You can easily create parametrize type using 146 * {@link TypeUtils#parameterize(Class, Type...)} 147 * @param json The json to parse. 148 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 149 * @return the resulting object. 150 * @since 7.2 151 */ 152 public static <T> T objectToJson(Class<T> type, Type genericType, String json, RenderingContext ctx) 153 throws IOException { 154 Reader<T> reader = getService().getReader(ctx, type, genericType, APPLICATION_JSON_TYPE); 155 checkMarshaller(genericType, reader); 156 return reader.read(type, genericType, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes())); 157 } 158 159 /** 160 * Read an object of the given type from given json. 161 * <p> 162 * Specify the list element type to get the best marshaller to manage conversion. 163 * </p> 164 * 165 * @param elementType The element type of the list. 166 * @param json The json to parse. 167 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 168 * @return the resulting list. 169 * @since 7.2 170 */ 171 @SuppressWarnings("unchecked") 172 public static <T> List<T> listToJson(Class<T> elementType, String json, RenderingContext ctx) throws IOException { 173 Type genericType = TypeUtils.parameterize(List.class, elementType); 174 @SuppressWarnings("rawtypes") 175 Reader<List> reader = getService().getReader(ctx, List.class, genericType, APPLICATION_JSON_TYPE); 176 checkMarshaller(genericType, reader); 177 return reader.read(List.class, genericType, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes())); 178 } 179 180}