001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.json;
018
019/**
020 * Serializes POJO models to Simplified JSON.
021 *
022 * <h5 class='topic'>Media types</h5>
023 *
024 * Handles <c>Accept</c> types:  <bc>application/json, text/json</bc>
025 * <p>
026 * Produces <c>Content-Type</c> types:  <bc>application/json5</bc>
027 *
028 * <h5 class='topic'>Description</h5>
029 * <p>
030 *    This is a JSON serializer that uses simplified notation:
031 * <ul class='spaced-list'>
032 *    <li>Lax quoting of JSON attribute names.
033 *    <li>Single quotes.
034 * </ul>
035 *
036 * <h5 class='section'>Notes:</h5><ul>
037 *    <li class='note'>This class is thread safe and reusable.
038 * </ul>
039 *
040 * <h5 class='section'>See Also:</h5><ul>
041 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JsonBasics">JSON Basics</a>
042 * </ul>
043 */
044public class Json5Serializer extends JsonSerializer {
045   /** Default serializer, single quotes, simple mode, with whitespace. */
046   public static class Readable extends Json5Serializer {
047
048      /**
049       * Constructor.
050       *
051       * @param builder The builder for this object.
052       */
053      public Readable(JsonSerializer.Builder builder) {
054         super(builder.simpleAttrs().quoteChar('\'').useWhitespace());
055      }
056   }
057
058   /** Default serializer, single quotes, {@link JsonSerializer.Builder#simpleAttrs() simple mode}. */
059   public static final Json5Serializer DEFAULT = new Json5Serializer(create());
060
061   /** Default serializer, single quotes, {@link JsonSerializer.Builder#simpleAttrs() simple mode}, sorted bean properties. */
062   public static final Json5Serializer DEFAULT_SORTED = new Json5Serializer(create().sortProperties());
063
064   /** Default serializer, single quotes, simple mode, with whitespace. */
065   public static final Json5Serializer DEFAULT_READABLE = new Readable(create());
066
067   /**
068    * Creates a new builder for this object.
069    *
070    * @return A new builder.
071    */
072   public static JsonSerializer.Builder create() {
073      return JsonSerializer.create().simpleAttrs().quoteChar('\'').produces("application/json5").accept("application/json5,text/json5,application/json;q=0.9,text/json;q=0.9")
074         .type(Json5Serializer.class);
075   }
076
077   /**
078    * Constructor.
079    *
080    * @param builder The builder for this object.
081    */
082   public Json5Serializer(JsonSerializer.Builder builder) {
083      super(builder.simpleAttrs().quoteChar('\''));
084   }
085
086   @Override /* Overridden from Context */
087   public Builder copy() {
088      return new Builder(this);
089   }
090}