com.heroiclabs.sdk.android.util.json.JsonDeserializerUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.heroiclabs.sdk.android.util.json.JsonDeserializerUtils.java

Source

/*
 * Copyright 2016 Heroic Labs
 *
 * 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.heroiclabs.sdk.android.util.json;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import lombok.NonNull;

/**
 * Collection of utility methods used in custom JSON deserializer implementations.
 */
class JsonDeserializerUtils {

    /**
     * Extract a JsonElement member as a String from a JsonObject, if it exists.
     *
     * @param object The JsonObject to extract from.
     * @param memberName The member name to extract.
     * @return A String representation of the corresponding JsonElement.
     */
    protected static String extractAsString(final @NonNull JsonObject object, final @NonNull String memberName) {
        if (object.has(memberName)) {
            final JsonElement member = object.get(memberName);
            if (member.isJsonNull()) {
                return null;
            }
            return member.toString();
        }
        return null;
    }

    /**
     * Extract a JsonElement member as a Long from a JsonObject, if it exists.
     *
     * @param object The JsonObject to extract from.
     * @param memberName The member name to extract.
     * @return A Long representation of the corresponding JsonElement.
     */
    protected static Long extractAsLong(final @NonNull JsonObject object, final @NonNull String memberName) {
        if (object.has(memberName)) {
            final JsonElement member = object.get(memberName);
            if (member.isJsonNull()) {
                return null;
            }
            return member.getAsLong();
        }
        return null;
    }

}