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.marshall.json5; 018 019import org.apache.juneau.marshall.json.*; 020import org.apache.juneau.marshall.serializer.*; 021 022/** 023 * Session object that lives for the duration of a single use of {@link Json5Serializer}. 024 * 025 * <p> 026 * Extends {@link JsonSerializerSession} to support JSON5 serialization with unquoted attribute names 027 * (when safe to do so) and single-quoted strings. 028 * 029 * <h5 class='section'>Notes:</h5><ul> 030 * <li class='warn'>This class is not thread safe and is typically discarded after one use. 031 * </ul> 032 * 033 * <h5 class='section'>See Also:</h5><ul> 034 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JsonSupport">JSON</a> 035 * </ul> 036 */ 037@SuppressWarnings({ 038 "java:S110", // Inheritance depth acceptable for Json5 serializer session hierarchy (matches JsonSerializerSession) 039 "resource" // Pipe writer is wrapped by JsonWriter; caller/pipe owns lifecycle (same as JsonSerializerSession) 040}) 041public class Json5SerializerSession extends JsonSerializerSession { 042 043 /** 044 * Builder class. 045 */ 046 public static class Builder extends JsonSerializerSession.Builder<Builder> { 047 048 /** 049 * Constructor 050 * 051 * @param ctx The context creating this session. 052 */ 053 protected Builder(Json5Serializer ctx) { 054 super(ctx); 055 } 056 057 @Override 058 public Json5SerializerSession build() { 059 return new Json5SerializerSession(this); 060 } 061 } 062 063 /** 064 * Creates a new builder for this object. 065 * 066 * @param ctx The context creating this session. 067 * @return A new builder. 068 */ 069 public static Builder create(Json5Serializer ctx) { 070 return new Builder(ctx); 071 } 072 073 /** 074 * Constructor. 075 * 076 * @param builder The builder for this object. 077 */ 078 protected Json5SerializerSession(Builder builder) { 079 super(builder); 080 } 081 082 @Override /* Overridden from JsonSerializerSession */ 083 protected boolean isSimpleAttrs() { return true; } 084 085 @Override 086 protected JsonWriter<?> getJsonWriter(SerializerPipe out) { 087 var output = out.getRawOutput(); 088 if (output instanceof JsonWriter<?> output2) 089 return output2; 090 var w = JsonWriter.create(out.getWriter(), isUseWhitespace(), getMaxIndent(), isEscapeSolidus(), getQuoteChar(), true, isTrimStrings(), getUriResolver()); 091 out.setWriter(w); 092 return w; 093 } 094}