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
015/**
016 * Serializes POJO models to Simplified JSON.
017 *
018 * <h5 class='topic'>Media types</h5>
019 *
020 * Handles <c>Accept</c> types:  <bc>application/json, text/json</bc>
021 * <p>
022 * Produces <c>Content-Type</c> types:  <bc>application/json5</bc>
023 *
024 * <h5 class='topic'>Description</h5>
025 * <p>
026 *    This is a JSON serializer that uses simplified notation:
027 * <ul class='spaced-list'>
028 *    <li>Lax quoting of JSON attribute names.
029 *    <li>Single quotes.
030 * </ul>
031 *
032 * <h5 class='section'>Notes:</h5><ul>
033 *    <li class='note'>This class is thread safe and reusable.
034 * </ul>
035 *
036 * <h5 class='section'>See Also:</h5><ul>
037 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.JsonDetails">JSON Details</a>
038 * </ul>
039 */
040public class Json5Serializer extends JsonSerializer {
041
042   //-------------------------------------------------------------------------------------------------------------------
043   // Static
044   //-------------------------------------------------------------------------------------------------------------------
045
046   /** Default serializer, single quotes, {@link JsonSerializer.Builder#simpleAttrs() simple mode}. */
047   public static final Json5Serializer DEFAULT = new Json5Serializer(create());
048
049   /** Default serializer, single quotes, simple mode, with whitespace. */
050   public static final Json5Serializer DEFAULT_READABLE = new Readable(create());
051
052   /**
053    * Creates a new builder for this object.
054    *
055    * @return A new builder.
056    */
057   public static JsonSerializer.Builder create() {
058      return JsonSerializer.create().simpleAttrs().quoteChar('\'').produces("application/json5").accept("application/json5,text/json5,application/json;q=0.9,text/json;q=0.9").type(Json5Serializer.class);
059   }
060
061   //-------------------------------------------------------------------------------------------------------------------
062   // Static subclasses
063   //-------------------------------------------------------------------------------------------------------------------
064
065   /** Default serializer, single quotes, simple mode, with whitespace. */
066   public static class Readable extends Json5Serializer {
067
068      /**
069       * Constructor.
070       *
071       * @param builder The builder for this object.
072       */
073      public Readable(JsonSerializer.Builder builder) {
074         super(builder.simpleAttrs().quoteChar('\'').useWhitespace());
075      }
076   }
077
078   //-------------------------------------------------------------------------------------------------------------------
079   // Instance
080   //-------------------------------------------------------------------------------------------------------------------
081
082   /**
083    * Constructor.
084    *
085    * @param builder The builder for this object.
086    */
087   public Json5Serializer(JsonSerializer.Builder builder) {
088      super(builder.simpleAttrs().quoteChar('\''));
089   }
090
091   @Override /* Context */
092   public Builder copy() {
093      return new Builder(this);
094   }
095}