Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2011 Google 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 com.squareup.moshi.adapters;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
/**
* A JsonAdapter factory for objects that include type information in the JSON. When decoding JSON
* Moshi uses this type information to determine which class to decode to. When encoding Moshi uses
* the object’s class to determine what type information to include.
*
*
Suppose we have an interface, its implementations, and a class that uses them:
*
*
{@code
* interface HandOfCards {
* }
*
* class BlackjackHand implements HandOfCards {
* Card hidden_card;
* List visible_cards;
* }
*
* class HoldemHand implements HandOfCards {
* Set hidden_cards;
* }
*
* class Player {
* String name;
* HandOfCards hand;
* }
* }
*
*
We want to decode the following JSON into the player model above:
*
*
Left unconfigured, Moshi would incorrectly attempt to decode the hand object to the abstract
* {@code HandOfCards} interface. We configure it to use the appropriate subtype instead:
*
*
This class imposes strict requirements on its use:
*
*
*
Base types may be classes or interfaces.
*
Subtypes must encode as JSON objects.
*
Type information must be in the encoded object. Each message must have a type label like
* {@code hand_type} whose value is a string like {@code blackjack} that identifies which type
* to use.
*
Each type identifier must be unique.
*
*
*
For best performance type information should be the first field in the object. Otherwise Moshi
* must reprocess the JSON stream once it knows the object's type.
*
*
If an unknown subtype is encountered when decoding:
*
*
*
If {@link #withDefaultValue(Object)} is used, then {@code defaultValue} will be returned.
*
If {@link #withFallbackJsonAdapter(JsonAdapter)} is used, then the {@code
* fallbackJsonAdapter.fromJson(reader)} result will be returned.
*
Otherwise a {@link JsonDataException} will be thrown.
*
*
*
If an unknown type is encountered when encoding:
*
*
*
If {@link #withFallbackJsonAdapter(JsonAdapter)} is used, then the {@code
* fallbackJsonAdapter.toJson(writer, value)} result will be returned.
*
Otherwise a {@link IllegalArgumentException} will be thrown.
*
*
*
If the same subtype has multiple labels the first one is used when encoding.
*/
public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Factory {
final Class baseType;
final String labelKey;
final List labels;
final List subtypes;
@Nullable final JsonAdapter