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 046 //------------------------------------------------------------------------------------------------------------------- 047 // Static 048 //------------------------------------------------------------------------------------------------------------------- 049 050 /** Default serializer, single quotes, {@link JsonSerializer.Builder#simpleAttrs() simple mode}. */ 051 public static final Json5Serializer DEFAULT = new Json5Serializer(create()); 052 053 /** Default serializer, single quotes, {@link JsonSerializer.Builder#simpleAttrs() simple mode}, sorted bean properties. */ 054 public static final Json5Serializer DEFAULT_SORTED = new Json5Serializer(create().sortProperties()); 055 056 /** Default serializer, single quotes, simple mode, with whitespace. */ 057 public static final Json5Serializer DEFAULT_READABLE = new Readable(create()); 058 059 /** 060 * Creates a new builder for this object. 061 * 062 * @return A new builder. 063 */ 064 public static JsonSerializer.Builder create() { 065 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); 066 } 067 068 //------------------------------------------------------------------------------------------------------------------- 069 // Static subclasses 070 //------------------------------------------------------------------------------------------------------------------- 071 072 /** Default serializer, single quotes, simple mode, with whitespace. */ 073 public static class Readable extends Json5Serializer { 074 075 /** 076 * Constructor. 077 * 078 * @param builder The builder for this object. 079 */ 080 public Readable(JsonSerializer.Builder builder) { 081 super(builder.simpleAttrs().quoteChar('\'').useWhitespace()); 082 } 083 } 084 085 //------------------------------------------------------------------------------------------------------------------- 086 // Instance 087 //------------------------------------------------------------------------------------------------------------------- 088 089 /** 090 * Constructor. 091 * 092 * @param builder The builder for this object. 093 */ 094 public Json5Serializer(JsonSerializer.Builder builder) { 095 super(builder.simpleAttrs().quoteChar('\'')); 096 } 097 098 @Override /* Context */ 099 public Builder copy() { 100 return new Builder(this); 101 } 102}