com.squareup.moshi.ClassJsonAdapter Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2015 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
*
* http://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 com.squareup.moshi;
import com.jn.langx.annotation.Nullable;
import com.squareup.moshi.internal.Util;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import static com.squareup.moshi.internal.Util.resolve;
/**
* Emits a regular class as a JSON object by mapping Java fields to JSON object properties.
*
* Platform Types
* Fields from platform classes are omitted from both serialization and deserialization unless
* they are either public or protected. This includes the following packages and their subpackages:
*
*
* - android.*
*
- androidx.*
*
- java.*
*
- javax.*
*
- kotlin.*
*
- kotlinx.*
*
- scala.*
*
*/
final class ClassJsonAdapter extends JsonAdapter {
public static final JsonAdapter.Factory FACTORY = new JsonAdapter.Factory() {
@Override
public @Nullable
JsonAdapter> create(
Type type, Set extends Annotation> annotations, Moshi moshi) {
if (!(type instanceof Class) && !(type instanceof ParameterizedType)) {
return null;
}
Class> rawType = Types.getRawType(type);
if (rawType.isInterface() || rawType.isEnum()) {
return null;
}
if (!annotations.isEmpty()) {
return null;
}
if (Util.isPlatformType(rawType)) {
String messagePrefix = "Platform " + rawType;
if (type instanceof ParameterizedType) {
messagePrefix += " in " + type;
}
throw new IllegalArgumentException(
messagePrefix + " requires explicit JsonAdapter to be registered");
}
if (rawType.isAnonymousClass()) {
throw new IllegalArgumentException("Cannot serialize anonymous class " + rawType.getName());
}
if (rawType.isLocalClass()) {
throw new IllegalArgumentException("Cannot serialize local class " + rawType.getName());
}
if (rawType.getEnclosingClass() != null && !Modifier.isStatic(rawType.getModifiers())) {
throw new IllegalArgumentException(
"Cannot serialize non-static nested class " + rawType.getName());
}
if (Modifier.isAbstract(rawType.getModifiers())) {
throw new IllegalArgumentException("Cannot serialize abstract class " + rawType.getName());
}
if (Util.isKotlin(rawType)) {
throw new IllegalArgumentException("Cannot serialize Kotlin type " + rawType.getName()
+ ". Reflective serialization of Kotlin classes without using kotlin-reflect has "
+ "undefined and unexpected behavior. Please use KotlinJsonAdapter from the "
+ "moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.");
}
ClassFactory