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.json;
014
015import org.apache.juneau.*;
016
017/**
018 * Serializes POJO models to Simplified JSON.
019 *
020 * <h5 class='topic'>Media types</h5>
021 *
022 * Handles <c>Accept</c> types:  <bc>application/json, text/json</bc>
023 * <p>
024 * Produces <c>Content-Type</c> types:  <bc>application/json+simple</bc>
025 *
026 * <h5 class='topic'>Description</h5>
027 * <p>
028 *    This is a JSON serializer that uses simplified notation:
029 * <ul class='spaced-list'>
030 *    <li>Lax quoting of JSON attribute names.
031 *    <li>Single quotes.
032 * </ul>
033 *
034 * <ul class='seealso'>
035 *    <li class='link'>{@doc SimpleJson}
036 * </ul>
037 */
038public class SimpleJsonSerializer extends JsonSerializer {
039
040   //-------------------------------------------------------------------------------------------------------------------
041   // Predefined instances
042   //-------------------------------------------------------------------------------------------------------------------
043
044   /** Default serializer, single quotes, {@link #JSON_simpleMode simple mode}. */
045   public static final JsonSerializer DEFAULT = new SimpleJsonSerializer(PropertyStore.DEFAULT);
046
047   /** Default serializer, single quotes, simple mode, with whitespace. */
048   public static final JsonSerializer DEFAULT_READABLE = new Readable(PropertyStore.DEFAULT);
049
050   //-------------------------------------------------------------------------------------------------------------------
051   // Predefined subclasses
052   //-------------------------------------------------------------------------------------------------------------------
053
054   /** Default serializer, single quotes, simple mode, with whitespace. */
055   public static class Readable extends SimpleJsonSerializer {
056
057      /**
058       * Constructor.
059       *
060       * @param ps The property store containing all the settings for this object.
061       */
062      public Readable(PropertyStore ps) {
063         super(
064            ps.builder()
065               .set(JSON_simpleMode, true)
066               .set(WSERIALIZER_quoteChar, '\'')
067               .set(WSERIALIZER_useWhitespace, true)
068               .build()
069         );
070      }
071   }
072
073   /**
074    * Constructor.
075    *
076    * @param ps The property store containing all the settings for this object.
077    */
078   public SimpleJsonSerializer(PropertyStore ps) {
079      super(
080         ps.builder()
081            .set(JSON_simpleMode, true)
082            .set(WSERIALIZER_quoteChar, '\'')
083            .build(),
084         "application/json", "application/json+simple,text/json+simple,application/json;q=0.9,text/json;q=0.9"
085      );
086   }
087
088   //-----------------------------------------------------------------------------------------------------------------
089   // Other methods
090   //-----------------------------------------------------------------------------------------------------------------
091
092   @Override /* Context */
093   public ObjectMap toMap() {
094      return super.toMap()
095         .append("SimpleJsonSerializer", new DefaultFilteringObjectMap()
096         );
097   }
098}