001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.msgpack; 014 015import org.apache.juneau.*; 016import org.apache.juneau.serializer.*; 017 018/** 019 * Serializes POJO models to MessagePack. 020 * 021 * <h5 class='section'>Media types:</h5> 022 * 023 * Handles <code>Accept</code> types: <code><b>octal/msgpack</b></code> 024 * <p> 025 * Produces <code>Content-Type</code> types: <code><b>octal/msgpack</b></code> 026 */ 027public class MsgPackSerializer extends OutputStreamSerializer { 028 029 //------------------------------------------------------------------------------------------------------------------- 030 // Configurable properties 031 //------------------------------------------------------------------------------------------------------------------- 032 033 private static final String PREFIX = "MsgPackSerializer."; 034 035 /** 036 * Configuration property: Add <js>"_type"</js> properties when needed. 037 * 038 * <h5 class='section'>Property:</h5> 039 * <ul> 040 * <li><b>Name:</b> <js>"MsgPackSerializer.addBeanTypes.b"</js> 041 * <li><b>Data type:</b> <code>Boolean</code> 042 * <li><b>Default:</b> <jk>false</jk> 043 * <li><b>Session property:</b> <jk>false</jk> 044 * <li><b>Methods:</b> 045 * <ul> 046 * <li class='jm'>{@link MsgPackSerializerBuilder#addBeanTypes(boolean)} 047 * </ul> 048 * </ul> 049 * 050 * <h5 class='section'>Description:</h5> 051 * <p> 052 * If <jk>true</jk>, then <js>"_type"</js> properties will be added to beans if their type cannot be inferred 053 * through reflection. 054 * 055 * <p> 056 * When present, this value overrides the {@link #SERIALIZER_addBeanTypes} setting and is 057 * provided to customize the behavior of specific serializers in a {@link SerializerGroup}. 058 */ 059 public static final String MSGPACK_addBeanTypes = PREFIX + "addBeanTypes.b"; 060 061 062 //------------------------------------------------------------------------------------------------------------------- 063 // Predefined instances 064 //------------------------------------------------------------------------------------------------------------------- 065 066 /** Default serializer, all default settings.*/ 067 public static final MsgPackSerializer DEFAULT = new MsgPackSerializer(PropertyStore.DEFAULT); 068 069 /** Default serializer, all default settings, spaced-hex string output.*/ 070 public static final MsgPackSerializer DEFAULT_SPACED_HEX = new SpacedHex(PropertyStore.DEFAULT); 071 072 /** Default serializer, all default settings, spaced-hex string output.*/ 073 public static final MsgPackSerializer DEFAULT_BASE64 = new Base64(PropertyStore.DEFAULT); 074 075 //------------------------------------------------------------------------------------------------------------------- 076 // Predefined subclasses 077 //------------------------------------------------------------------------------------------------------------------- 078 079 /** Default serializer, spaced-hex string output. */ 080 public static class SpacedHex extends MsgPackSerializer { 081 082 /** 083 * Constructor. 084 * 085 * @param ps The property store containing all the settings for this object. 086 */ 087 public SpacedHex(PropertyStore ps) { 088 super( 089 ps.builder().set(OSSERIALIZER_binaryFormat, BinaryFormat.SPACED_HEX).build() 090 ); 091 } 092 } 093 094 /** Default serializer, BASE64 string output. */ 095 public static class Base64 extends MsgPackSerializer { 096 097 /** 098 * Constructor. 099 * 100 * @param ps The property store containing all the settings for this object. 101 */ 102 public Base64(PropertyStore ps) { 103 super( 104 ps.builder().set(OSSERIALIZER_binaryFormat, BinaryFormat.BASE64).build() 105 ); 106 } 107 } 108 109 //------------------------------------------------------------------------------------------------------------------- 110 // Instance 111 //------------------------------------------------------------------------------------------------------------------- 112 113 private final boolean 114 addBeanTypes; 115 116 /** 117 * Constructor. 118 * 119 * @param ps The property store containing all the settings for this object. 120 */ 121 public MsgPackSerializer(PropertyStore ps) { 122 super(ps, "octal/msgpack", null); 123 this.addBeanTypes = getBooleanProperty(MSGPACK_addBeanTypes, getBooleanProperty(SERIALIZER_addBeanTypes, false)); 124 } 125 126 @Override /* Context */ 127 public MsgPackSerializerBuilder builder() { 128 return new MsgPackSerializerBuilder(getPropertyStore()); 129 } 130 131 /** 132 * Instantiates a new clean-slate {@link MsgPackSerializerBuilder} object. 133 * 134 * <p> 135 * This is equivalent to simply calling <code><jk>new</jk> MsgPackSerializerBuilder()</code>. 136 * 137 * <p> 138 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 139 * the settings of the object called on. 140 * 141 * @return A new {@link MsgPackSerializerBuilder} object. 142 */ 143 public static MsgPackSerializerBuilder create() { 144 return new MsgPackSerializerBuilder(); 145 } 146 147 @Override /* Serializer */ 148 public OutputStreamSerializerSession createSession(SerializerSessionArgs args) { 149 return new MsgPackSerializerSession(this, args); 150 } 151 152 //----------------------------------------------------------------------------------------------------------------- 153 // Properties 154 //----------------------------------------------------------------------------------------------------------------- 155 156 @Override 157 protected final boolean isAddBeanTypes() { 158 return addBeanTypes; 159 } 160 161 @Override /* Context */ 162 public ObjectMap asMap() { 163 return super.asMap() 164 .append("MsgPackSerializer", new ObjectMap() 165 .append("addBeanTypes", addBeanTypes) 166 ); 167 } 168 169 /** 170 * @deprecated Use {@link #MSGPACK_addBeanTypes}. 171 */ 172 @Deprecated 173 public static final String MSGPACK_addBeanTypeProperties = MSGPACK_addBeanTypes; 174}