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 <code>Accept</code> types: <code><b>application/json, text/json</b></code> 023 * <p> 024 * Produces <code>Content-Type</code> types: <code><b>application/json+simple</b></code> 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 * <h5 class='section'>See Also:</h5> 035 * <ul class='doctree'> 036 * <li class='link'>{@doc juneau-marshall.JsonDetails.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 JsonSerializer DEFAULT = new SimpleJsonSerializer(PropertyStore.DEFAULT); 047 048 /** Default serializer, single quotes, simple mode, with whitespace. */ 049 public static final JsonSerializer 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 .set(JSON_simpleMode, true) 067 .set(WSERIALIZER_quoteChar, '\'') 068 .set(SERIALIZER_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 .set(JSON_simpleMode, true) 083 .set(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}