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