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.jsonschema.*; 017import org.apache.juneau.serializer.*; 018 019/** 020 * Serializes POJO metadata to HTTP responses as JSON-Schema. 021 * 022 * <h5 class='topic'>Media types</h5> 023 * 024 * Handles <code>Accept</code> types: <code><b>application/json+schema, text/json+schema</b></code> 025 * <p> 026 * Produces <code>Content-Type</code> types: <code><b>application/json</b></code> 027 * 028 * <h5 class='topic'>Description</h5> 029 * 030 * Produces the JSON-schema for the JSON produced by the {@link JsonSerializer} class with the same properties. 031 */ 032public class JsonSchemaSerializer extends JsonSerializer { 033 034 035 //------------------------------------------------------------------------------------------------------------------- 036 // Predefined instances 037 //------------------------------------------------------------------------------------------------------------------- 038 039 /** Default serializer, all default settings.*/ 040 public static final JsonSchemaSerializer DEFAULT = new JsonSchemaSerializer(PropertyStore.DEFAULT); 041 042 /** Default serializer, all default settings.*/ 043 public static final JsonSchemaSerializer DEFAULT_READABLE = new Readable(PropertyStore.DEFAULT); 044 045 /** Default serializer, single quotes, simple mode. */ 046 public static final JsonSchemaSerializer DEFAULT_SIMPLE = new Simple(PropertyStore.DEFAULT); 047 048 /** Default serializer, single quotes, simple mode, with whitespace. */ 049 public static final JsonSchemaSerializer DEFAULT_SIMPLE_READABLE = new SimpleReadable(PropertyStore.DEFAULT); 050 051 052 //------------------------------------------------------------------------------------------------------------------- 053 // Predefined subclasses 054 //------------------------------------------------------------------------------------------------------------------- 055 056 /** Default serializer, with whitespace. */ 057 public static class Readable extends JsonSchemaSerializer { 058 059 /** 060 * Constructor. 061 * 062 * @param ps The property store containing all the settings for this object. 063 */ 064 public Readable(PropertyStore ps) { 065 super( 066 ps.builder().set(SERIALIZER_useWhitespace, true).build() 067 ); 068 } 069 } 070 071 /** Default serializer, single quotes, simple mode. */ 072 public static class Simple extends JsonSchemaSerializer { 073 074 /** 075 * Constructor. 076 * 077 * @param ps The property store containing all the settings for this object. 078 */ 079 public Simple(PropertyStore ps) { 080 super( 081 ps.builder() 082 .set(JSON_simpleMode, true) 083 .set(WSERIALIZER_quoteChar, '\'') 084 .build() 085 ); 086 } 087 } 088 089 /** Default serializer, single quotes, simple mode, with whitespace. */ 090 public static class SimpleReadable extends JsonSchemaSerializer { 091 092 /** 093 * Constructor. 094 * 095 * @param ps The property store containing all the settings for this object. 096 */ 097 public SimpleReadable(PropertyStore ps) { 098 super( 099 ps.builder() 100 .set(JSON_simpleMode, true) 101 .set(WSERIALIZER_quoteChar, '\'') 102 .set(SERIALIZER_useWhitespace, true) 103 .build() 104 ); 105 } 106 } 107 108 109 //------------------------------------------------------------------------------------------------------------------- 110 // Instance 111 //------------------------------------------------------------------------------------------------------------------- 112 113 private final JsonSchemaGenerator generator; 114 115 /** 116 * Constructor. 117 * 118 * @param ps Initialize with the specified config property store. 119 */ 120 public JsonSchemaSerializer(PropertyStore ps) { 121 super( 122 ps.builder() 123 .set(BEANTRAVERSE_detectRecursions, true) 124 .set(BEANTRAVERSE_ignoreRecursions, true) 125 .build(), 126 "application/json", "application/json+schema,text/json+schema" 127 ); 128 129 generator = JsonSchemaGenerator.create().apply(getPropertyStore()).build(); 130 } 131 132 @Override /* Context */ 133 public JsonSchemaSerializerBuilder builder() { 134 return new JsonSchemaSerializerBuilder(getPropertyStore()); 135 } 136 137 /** 138 * Instantiates a new clean-slate {@link JsonSerializerBuilder} object. 139 * 140 * <p> 141 * This is equivalent to simply calling <code><jk>new</jk> JsonSerializerBuilder()</code>. 142 * 143 * @return A new {@link JsonSerializerBuilder} object. 144 */ 145 public static JsonSchemaSerializerBuilder create() { 146 return new JsonSchemaSerializerBuilder(); 147 } 148 149 @Override /* Context */ 150 public JsonSchemaSerializerSession createSession() { 151 return createSession(createDefaultSessionArgs()); 152 } 153 154 @Override /* Serializer */ 155 public JsonSchemaSerializerSession createSession(SerializerSessionArgs args) { 156 return new JsonSchemaSerializerSession(this, args); 157 } 158 159 JsonSchemaGenerator getGenerator() { 160 return generator; 161 } 162}