001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nicolas Chapurlat <[email protected]> 018 */ 019 020package org.nuxeo.ecm.core.io.registry; 021 022import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; 023 024import java.io.ByteArrayInputStream; 025import java.io.ByteArrayOutputStream; 026import java.io.IOException; 027import java.lang.reflect.Type; 028import java.util.List; 029 030import org.apache.commons.lang3.reflect.TypeUtils; 031import org.nuxeo.ecm.core.io.registry.context.RenderingContext; 032import org.nuxeo.ecm.core.io.registry.context.RenderingContext.CtxBuilder; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * Quick use of {@link MarshallerRegistry}. 037 * 038 * @since 7.2 039 */ 040public final class MarshallerHelper { 041 042 /** 043 * Just call static methods. 044 */ 045 private MarshallerHelper() { 046 } 047 048 private static MarshallerRegistry getService() { 049 MarshallerRegistry registry = Framework.getService(MarshallerRegistry.class); 050 return registry; 051 } 052 053 /** 054 * Checks the marshallers isn't null. Throw an exception if it is. 055 */ 056 private static void checkMarshaller(Type type, Marshaller<?> marshaller) { 057 if (marshaller == null) { 058 throw new MarshallingException("No marshaller found for type " + type.toString()); 059 } 060 } 061 062 /** 063 * Convert the given object to json. 064 * 065 * @param object The object to convert as json. 066 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 067 * @return the resulting json. 068 * @since 7.2 069 */ 070 public static <T> String objectToJson(T object, RenderingContext ctx) throws IOException { 071 @SuppressWarnings("unchecked") 072 Class<T> type = (Class<T>) object.getClass(); 073 Writer<T> writer = getService().getWriter(ctx, type, APPLICATION_JSON_TYPE); 074 checkMarshaller(type, writer); 075 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 076 writer.write(object, type, type, APPLICATION_JSON_TYPE, baos); 077 return baos.toString(); 078 } 079 080 /** 081 * Convert the given object to json. 082 * <p> 083 * Specify its generic type to be sure to get the best marshaller to manage it. 084 * </p> 085 * 086 * @param genericType The generic type of the object. You can easily create parametrize type using 087 * {@link TypeUtils#parameterize(Class, Type...)} 088 * @param object The object to convert as json. 089 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 090 * @return the resulting json. 091 * @since 7.2 092 */ 093 public static <T> String objectToJson(Type genericType, T object, RenderingContext ctx) throws IOException { 094 @SuppressWarnings("unchecked") 095 Class<T> type = (Class<T>) object.getClass(); 096 Writer<T> writer = getService().getWriter(ctx, type, genericType, APPLICATION_JSON_TYPE); 097 checkMarshaller(genericType, writer); 098 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 099 writer.write(object, type, genericType, APPLICATION_JSON_TYPE, baos); 100 return baos.toString(); 101 } 102 103 /** 104 * Convert the given list to json. 105 * <p> 106 * Specify the list element type to get the best marshaller to manage conversion. 107 * </p> 108 * 109 * @param elementType The element type of the list. 110 * @param list The list to convert. 111 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 112 * @return the resulting json. 113 * @since 7.2 114 */ 115 public static <T> String listToJson(Class<T> elementType, List<T> list, RenderingContext ctx) throws IOException { 116 Type genericType = TypeUtils.parameterize(List.class, elementType); 117 @SuppressWarnings("rawtypes") 118 Writer<List> writer = getService().getWriter(ctx, List.class, genericType, APPLICATION_JSON_TYPE); 119 checkMarshaller(genericType, writer); 120 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 121 writer.write(list, List.class, genericType, APPLICATION_JSON_TYPE, baos); 122 return baos.toString(); 123 } 124 125 /** 126 * Read an object of the given type from given json. 127 * 128 * @param type The type of the read object. 129 * @param json The json to parse. 130 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 131 * @return the resulting object. 132 * @since 7.2 133 * @deprecated since 10.2 use {@link #jsonToObject(Class, String, RenderingContext)} instead. 134 */ 135 @Deprecated 136 public static <T> T objectToJson(Class<T> type, String json, RenderingContext ctx) throws IOException { 137 return jsonToObject(type, json, ctx); 138 } 139 140 /** 141 * @since 10.2 142 */ 143 public static <T> T jsonToObject(Class<T> type, String json, RenderingContext ctx) throws IOException { 144 Reader<T> reader = getService().getReader(ctx, type, APPLICATION_JSON_TYPE); 145 checkMarshaller(type, reader); 146 return reader.read(type, type, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes())); 147 } 148 149 /** 150 * Read an object of the given type from given json. 151 * <p> 152 * Specify its generic type to be sure to get the best marshaller to manage it. 153 * </p> 154 * 155 * @param type The type of the read object. 156 * @param genericType The generic type of the object. You can easily create parametrize type using 157 * {@link TypeUtils#parameterize(Class, Type...)} 158 * @param json The json to parse. 159 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 160 * @return the resulting object. 161 * @since 7.2 162 * @deprecated since 10.2, use {@link #jsonToObject(Class, Type, String, RenderingContext)} instead. 163 */ 164 @Deprecated 165 public static <T> T objectToJson(Class<T> type, Type genericType, String json, RenderingContext ctx) 166 throws IOException { 167 return jsonToObject(type, genericType, json, ctx); 168 } 169 170 /** 171 * @since 10.2 172 */ 173 public static <T> T jsonToObject(Class<T> type, Type genericType, String json, RenderingContext ctx) 174 throws IOException { 175 Reader<T> reader = getService().getReader(ctx, type, genericType, APPLICATION_JSON_TYPE); 176 checkMarshaller(genericType, reader); 177 return reader.read(type, genericType, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes())); 178 } 179 180 /** 181 * Read an object of the given type from given json. 182 * <p> 183 * Specify the list element type to get the best marshaller to manage conversion. 184 * </p> 185 * 186 * @param elementType The element type of the list. 187 * @param json The json to parse. 188 * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. 189 * @return the resulting list. 190 * @since 7.2 191 * @deprecated since 10.2, use {@link #jsonToList(Class, String, RenderingContext)} instead. 192 */ 193 @Deprecated 194 @SuppressWarnings("unchecked") 195 public static <T> List<T> listToJson(Class<T> elementType, String json, RenderingContext ctx) throws IOException { 196 return jsonToList(elementType, json, ctx); 197 } 198 199 /** 200 * @since 10.2 201 */ 202 @SuppressWarnings("unchecked") 203 public static <T> List<T> jsonToList(Class<T> elementType, String json, RenderingContext ctx) throws IOException { 204 Type genericType = TypeUtils.parameterize(List.class, elementType); 205 @SuppressWarnings("rawtypes") 206 Reader<List> reader = getService().getReader(ctx, List.class, genericType, APPLICATION_JSON_TYPE); 207 checkMarshaller(genericType, reader); 208 return reader.read(List.class, genericType, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes())); 209 } 210 211}