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.addBeanTypeProperties.b"</js>
041    *    <li><b>Data type:</b>  <code>Boolean</code>
042    *    <li><b>Default:</b>  <jk>false</jk>
043    *    <li><b>Session-overridable:</b>  <jk>true</jk>
044    *    <li><b>Methods:</b> 
045    *       <ul>
046    *          <li class='jm'>{@link MsgPackSerializerBuilder#addBeanTypeProperties(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_addBeanTypeProperties} setting and is
057    * provided to customize the behavior of specific serializers in a {@link SerializerGroup}.
058    */
059   public static final String MSGPACK_addBeanTypeProperties = PREFIX + "addBeanTypeProperties.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
070   //-------------------------------------------------------------------------------------------------------------------
071   // Instance
072   //-------------------------------------------------------------------------------------------------------------------
073
074   final boolean
075      addBeanTypeProperties;
076
077   /**
078    * Constructor.
079    * 
080    * @param ps The property store containing all the settings for this object.
081    */
082   public MsgPackSerializer(PropertyStore ps) {
083      super(ps, "octal/msgpack");
084      this.addBeanTypeProperties = getBooleanProperty(MSGPACK_addBeanTypeProperties, getBooleanProperty(SERIALIZER_addBeanTypeProperties, true));
085   }
086
087   @Override /* Context */
088   public MsgPackSerializerBuilder builder() {
089      return new MsgPackSerializerBuilder(getPropertyStore());
090   }
091
092   /**
093    * Instantiates a new clean-slate {@link MsgPackSerializerBuilder} object.
094    * 
095    * <p>
096    * This is equivalent to simply calling <code><jk>new</jk> MsgPackSerializerBuilder()</code>.
097    * 
098    * <p>
099    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 
100    * the settings of the object called on.
101    * 
102    * @return A new {@link MsgPackSerializerBuilder} object.
103    */
104   public static MsgPackSerializerBuilder create() {
105      return new MsgPackSerializerBuilder();
106   }
107   
108   @Override /* Serializer */
109   public OutputStreamSerializerSession createSession(SerializerSessionArgs args) {
110      return new MsgPackSerializerSession(this, args);
111   }
112
113   @Override /* Context */
114   public ObjectMap asMap() {
115      return super.asMap()
116         .append("MsgPackSerializer", new ObjectMap()
117            .append("addBeanTypeProperties", addBeanTypeProperties)
118         );
119   }
120}