io.avaje.json.core.BaseAdapters Maven / Gradle / Ivy
/*
* Copyright (C) 2014 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.avaje.json.core;
import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonDataException;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import java.lang.reflect.Type;
import static java.util.Objects.requireNonNull;
final class BaseAdapters {
static JsonAdapter> create(Type type) {
if (type == Boolean.TYPE) return new BooleanAdapter();
if (type == Double.TYPE) return new DoubleAdapter();
if (type == Float.TYPE) return new FloatAdapter();
if (type == Integer.TYPE) return new IntegerAdapter();
if (type == Long.TYPE) return new LongAdapter();
if (type == Boolean.class) return new BooleanAdapter().nullSafe();
if (type == Double.class) return new DoubleAdapter().nullSafe();
if (type == Float.class) return new FloatAdapter().nullSafe();
if (type == Integer.class) return new IntegerAdapter().nullSafe();
if (type == Long.class) return new LongAdapter().nullSafe();
if (type == String.class) return new StringAdapter().nullSafe();
return null;
}
private static final class BooleanAdapter implements JsonAdapter {
@Override
public Boolean fromJson(JsonReader reader) {
return reader.readBoolean();
}
@Override
public void toJson(JsonWriter writer, Boolean value) {
writer.value(value);
}
@Override
public String toString() {
return "JsonAdapter(Boolean)";
}
}
private static final class DoubleAdapter implements JsonAdapter {
@Override
public Double fromJson(JsonReader reader) {
return reader.readDouble();
}
@Override
public void toJson(JsonWriter writer, Double value) {
writer.value(value);
}
@Override
public String toString() {
return "JsonAdapter(Double)";
}
}
private static final class FloatAdapter implements JsonAdapter {
@Override
public Float fromJson(JsonReader reader) {
float value = (float) reader.readDouble();
if (Float.isInfinite(value)) { // !reader.isLenient() &&
throw new JsonDataException("JSON forbids NaN and infinities: " + value + " at path " + reader.location());
} else {
return value;
}
}
@Override
public void toJson(JsonWriter writer, Float value) {
requireNonNull(value);
writer.value(value);
}
@Override
public String toString() {
return "JsonAdapter(Float)";
}
}
private static final class IntegerAdapter implements JsonAdapter {
@Override
public Integer fromJson(JsonReader reader) {
return reader.readInt();
}
@Override
public void toJson(JsonWriter writer, Integer value) {
writer.value(value);
}
@Override
public String toString() {
return "JsonAdapter(Integer)";
}
}
private static final class LongAdapter implements JsonAdapter {
@Override
public Long fromJson(JsonReader reader) {
return reader.readLong();
}
@Override
public void toJson(JsonWriter writer, Long value) {
writer.value(value);
}
@Override
public String toString() {
return "JsonAdapter(Long)";
}
}
private static final class StringAdapter implements JsonAdapter {
@Override
public String fromJson(JsonReader reader) {
return reader.readString();
}
@Override
public void toJson(JsonWriter writer, String value) {
writer.value(value);
}
@Override
public String toString() {
return "JsonAdapter(String)";
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy