001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.marshaller; 018 019import java.io.*; 020import java.lang.reflect.*; 021 022import org.apache.juneau.*; 023import org.apache.juneau.msgpack.*; 024import org.apache.juneau.parser.*; 025import org.apache.juneau.serializer.*; 026 027/** 028 * A pairing of a {@link MsgPackSerializer} and {@link MsgPackParser} into a single class with convenience read/write methods. 029 * 030 * <p> 031 * The general idea is to combine a single serializer and parser inside a simplified API for reading and writing POJOs. 032 * 033 * <h5 class='figure'>Examples:</h5> 034 * <p class='bjava'> 035 * <jc>// Using instance.</jc> 036 * MsgPack <jv>msgPack</jv> = <jk>new</jk> MsgPack(); 037 * MyPojo <jv>myPojo</jv> = <jv>msgPack</jv>.read(<jv>bytes</jv>, MyPojo.<jk>class</jk>); 038 * <jk>byte</jk>[] <jv>bytes</jv> = <jv>msgPack</jv>.write(<jv>myPojo</jv>); 039 * </p> 040 * <p class='bjava'> 041 * <jc>// Using DEFAULT instance.</jc> 042 * MyPojo <jv>myPojo</jv> = MsgPack.<jsf>DEFAULT</jsf>.read(<jv>bytes</jv>, MyPojo.<jk>class</jk>); 043 * <jk>byte</jk>[] <jv>bytes</jv> = MsgPack.<jsf>DEFAULT</jsf>.write(<jv>myPojo</jv>); 044 * </p> 045 * 046 * <h5 class='section'>See Also:</h5><ul> 047 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Marshallers">Marshallers</a> 048 * </ul> 049 */ 050public class MsgPack extends StreamMarshaller { 051 /** 052 * Default reusable instance. 053 */ 054 public static final MsgPack DEFAULT = new MsgPack(); 055 056 /** 057 * Serializes a Java object to a JSON string. 058 * 059 * <p> 060 * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>object</jv>)</c>. 061 * 062 * @param object The object to serialize. 063 * @return 064 * The serialized object. 065 * @throws SerializeException If a problem occurred trying to convert the output. 066 */ 067 public static byte[] of(Object object) throws SerializeException { 068 return DEFAULT.write(object); 069 } 070 071 /** 072 * Serializes a Java object to an output. 073 * 074 * <p> 075 * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>output</jv>)</c>. 076 * 077 * @param object The object to serialize. 078 * @param output 079 * The output object. 080 * <br>Can be any of the following types: 081 * <ul> 082 * <li>{@link OutputStream} 083 * <li>{@link File} 084 * </ul> 085 * @return The output object. 086 * @throws SerializeException If a problem occurred trying to convert the output. 087 * @throws IOException Thrown by underlying stream. 088 */ 089 public static Object of(Object object, Object output) throws SerializeException, IOException { 090 DEFAULT.write(object, output); 091 return output; 092 } 093 094 /** 095 * Parses a JSON input string to the specified type. 096 * 097 * <p> 098 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>. 099 * 100 * @param <T> The class type of the object being created. 101 * @param input The input. 102 * @param type The object type to create. 103 * @return The parsed object. 104 * @throws ParseException Malformed input encountered. 105 */ 106 public static <T> T to(byte[] input, Class<T> type) throws ParseException { 107 return DEFAULT.read(input, type); 108 } 109 110 /** 111 * Parses a JSON input string to the specified Java type. 112 * 113 * <p> 114 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>. 115 * 116 * @param <T> The class type of the object to create. 117 * @param input The input. 118 * @param type 119 * The object type to create. 120 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} 121 * @param args 122 * The type arguments of the class if it's a collection or map. 123 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} 124 * <br>Ignored if the main type is not a map or collection. 125 * @return The parsed object. 126 * @throws ParseException Malformed input encountered. 127 * @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections. 128 */ 129 public static <T> T to(byte[] input, Type type, Type...args) throws ParseException { 130 return DEFAULT.read(input, type, args); 131 } 132 133 /** 134 * Parses a JSON input object to the specified Java type. 135 * 136 * <p> 137 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>. 138 * 139 * @param <T> The class type of the object being created. 140 * @param input 141 * The input. 142 * <br>Can be any of the following types: 143 * <ul> 144 * <li><jk>null</jk> 145 * <li>{@link InputStream} 146 * <li><code><jk>byte</jk>[]</code> 147 * <li>{@link File} 148 * <li>{@link CharSequence} containing encoded bytes according to the {@link org.apache.juneau.parser.InputStreamParser.Builder#binaryFormat(BinaryFormat)} setting. 149 * </ul> 150 * @param type The object type to create. 151 * @return The parsed object. 152 * @throws ParseException Malformed input encountered. 153 * @throws IOException Thrown by underlying stream. 154 */ 155 public static <T> T to(Object input, Class<T> type) throws ParseException, IOException { 156 return DEFAULT.read(input, type); 157 } 158 159 /** 160 * Parses a JSON input object to the specified Java type. 161 * 162 * <p> 163 * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>. 164 * 165 * @param <T> The class type of the object to create. 166 * @param input 167 * The input. 168 * <br>Can be any of the following types: 169 * <ul> 170 * <li><jk>null</jk> 171 * <li>{@link InputStream} 172 * <li><code><jk>byte</jk>[]</code> 173 * <li>{@link File} 174 * <li>{@link CharSequence} containing encoded bytes according to the {@link org.apache.juneau.parser.InputStreamParser.Builder#binaryFormat(BinaryFormat)} setting. 175 * </ul> 176 * @param type 177 * The object type to create. 178 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} 179 * @param args 180 * The type arguments of the class if it's a collection or map. 181 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} 182 * <br>Ignored if the main type is not a map or collection. 183 * @return The parsed object. 184 * @throws ParseException Malformed input encountered. 185 * @throws IOException Thrown by underlying stream. 186 * @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections. 187 */ 188 public static <T> T to(Object input, Type type, Type...args) throws ParseException, IOException { 189 return DEFAULT.read(input, type, args); 190 } 191 192 /** 193 * Constructor. 194 * 195 * <p> 196 * Uses {@link MsgPackSerializer#DEFAULT} and {@link MsgPackParser#DEFAULT}. 197 */ 198 public MsgPack() { 199 this(MsgPackSerializer.DEFAULT, MsgPackParser.DEFAULT); 200 } 201 202 /** 203 * Constructor. 204 * 205 * @param s 206 * The serializer to use for serializing output. 207 * <br>Must not be <jk>null</jk>. 208 * @param p 209 * The parser to use for parsing input. 210 * <br>Must not be <jk>null</jk>. 211 */ 212 public MsgPack(MsgPackSerializer s, MsgPackParser p) { 213 super(s, p); 214 } 215}