001/* 002 * Copyright (C) 2014 Square, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.avaje.json; 017 018import io.avaje.json.view.ViewBuilderAware; 019 020/** 021 * The core API for serialization to and from json. 022 */ 023public interface JsonAdapter<T> { 024 025 /** 026 * Write the value to the writer. 027 */ 028 void toJson(JsonWriter writer, T value); 029 030 /** 031 * Read the type from the reader. 032 */ 033 T fromJson(JsonReader reader); 034 035 /** 036 * Return a null safe version of this adapter. 037 */ 038 default JsonAdapter<T> nullSafe() { 039 if (this instanceof NullSafeAdapter) { 040 return this; 041 } 042 return new NullSafeAdapter<>(this); 043 } 044 045 /** 046 * Return true if this adapter represents a json object or json array of objects that supports json views. 047 */ 048 default boolean isViewBuilderAware() { 049 return false; 050 } 051 052 /** 053 * Return the ViewBuilderAware for this adapter. 054 */ 055 default ViewBuilderAware viewBuild() { 056 throw new UnsupportedOperationException(); 057 } 058}