Example usage for com.google.gson JsonElement toString

List of usage examples for com.google.gson JsonElement toString

Introduction

In this page you can find the example usage for com.google.gson JsonElement toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:com.flipkart.android.proteus.toolbox.ColorUtils.java

License:Apache License

/**
 * @param context                Application context used to access resources
 * @param value                  JSON representation of the Color
 * @param colorCallback          Callback for return Value if it is a Color Resource
 * @param colorStateListCallback Callback for return Value if it is a ColorStateList
 * @throws android.content.res.Resources.NotFoundException when the animation cannot be loaded
 *//* w  w  w.  jav  a 2 s  . c  om*/
public static void loadColor(Context context, JsonElement value, ValueCallback<Integer> colorCallback,
        ValueCallback<ColorStateList> colorStateListCallback) throws Resources.NotFoundException {
    if (value.isJsonPrimitive()) {
        handleString(context, value.getAsString(), colorCallback, colorStateListCallback);
    } else if (value.isJsonObject()) {
        handleElement(context, value.getAsJsonObject(), colorCallback, colorStateListCallback);
    } else {
        if (ProteusConstants.isLoggingEnabled()) {
            Log.e(TAG, "Could not color for : " + value.toString());
        }
    }
}

From source file:com.flipkart.android.proteus.toolbox.Utils.java

License:Apache License

public static String getPropertyAsString(JsonObject object, String property) {
    if (object == null || object.isJsonNull()) {
        return null;
    }// ww w  .j av a  2 s . c om
    JsonElement element = object.get(property);
    if (element == null) {
        return null;
    }
    String string;
    if (!element.isJsonNull() && element.isJsonPrimitive()) {
        string = element.getAsString();
    } else {
        string = element.toString();
    }
    return string;
}

From source file:com.getperka.flatpack.codexes.CharacterCodex.java

License:Apache License

@Override
public Character readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        // Treat a number as a BMP code point
        if (primitive.isNumber()) {
            return (char) primitive.getAsInt();
        } else {// w w  w .  j  ava  2 s.c o  m
            return primitive.getAsCharacter();
        }
    }
    throw new IllegalArgumentException("Cannot convert " + element.toString() + " to a char");
}

From source file:com.getperka.flatpack.codexes.DateCodex.java

License:Apache License

@Override
public D readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        long instant;
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            instant = primitive.getAsLong();
        } else {//from  w ww  .j av  a  2  s  . co m
            instant = ISODateTimeFormat.dateTimeParser().parseMillis(primitive.getAsString());
        }
        return constructor.newInstance(instant);
    }
    throw new IllegalArgumentException("Could not parse " + element.toString() + " as a date value");
}

From source file:com.getperka.flatpack.codexes.DynamicCodex.java

License:Apache License

/**
 * Attempt to infer the type from the JsonElement presented.
 * <ul>/* w w  w.  j a  va2  s  .  c om*/
 * <li>boolean -> {@link Boolean}
 * <li>number -> {@link BigDecimal}
 * <li>string -> {@link HasUuid} or {@link String}
 * <li>array -> {@link ListCodex}
 * <li>object -> {@link StringMapCodex}
 * </ul>
 */
@Override
public Object readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            // Always return numbers as BigDecimals for consistency
            return primitive.getAsBigDecimal();
        } else {
            String value = primitive.getAsString();

            // Interpret UUIDs as entity references
            if (UUID_PATTERN.matcher(value).matches()) {
                UUID uuid = UUID.fromString(value);
                HasUuid entity = context.getEntity(uuid);
                if (entity != null) {
                    return entity;
                }
            }

            return value;
        }
    } else if (element.isJsonArray()) {
        return listCodex.get().readNotNull(element, context);
    } else if (element.isJsonObject()) {
        return mapCodex.get().readNotNull(element, context);
    }
    context.fail(new UnsupportedOperationException("Cannot infer data type for " + element.toString()));
    return null;
}

From source file:com.getperka.flatpack.codexes.EntityCodex.java

License:Apache License

/**
 * Performs a minimal amount of work to create an empty stub object to fill in later.
 * /*from   w  ww .j  av  a 2s .  co  m*/
 * @param element a JsonObject containing a {@code uuid} property. If {@code null}, a
 *          randomly-generated UUID will be assigned to the allocated object
 * @param context this method will call {@link DeserializationContext#putEntity} to store the
 *          newly-allocated entity
 */
public T allocate(JsonElement element, DeserializationContext context) {
    JsonElement uuidElement = element.getAsJsonObject().get("uuid");
    if (uuidElement == null) {
        context.fail(new IllegalArgumentException("Data entry missing uuid:\n" + element.toString()));
    }
    UUID uuid = UUID.fromString(uuidElement.getAsString());
    T toReturn = allocate(uuid, context, true);

    // Register PostUnpack methods
    if (!postUnpackMethods.isEmpty()) {
        context.addPostWork(new PostUnpackInvoker(toReturn, postUnpackMethods));
    }
    return toReturn;
}

From source file:com.github.francescojo.gsondbg.GsonDebuggable.java

/**
 * See {@link com.google.gson.Gson#fromJson(JsonElement, Class)} for its original documentation.
 *//*from  w w  w.j  a v a 2s.c o m*/
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
    if (IS_DEBUG) {
        try {
            return gson.fromJson(json, classOfT);
        } catch (JsonSyntaxException e) {
            return inspectJson(json.toString(), classOfT, e);
        }
    } else {
        return gson.fromJson(json, classOfT);
    }
}

From source file:com.github.maoo.indexer.client.WebScriptsAlfrescoClient.java

License:Apache License

private String getUsername(JsonObject userObject) {
    if (!userObject.has(USERNAME)) {
        throw new AlfrescoParseException("Json response is missing username.");
    }//  w  ww .ja va  2 s.  c  o  m
    JsonElement usernameElement = userObject.get(USERNAME);
    if (!usernameElement.isJsonPrimitive() || !usernameElement.getAsJsonPrimitive().isString()) {
        throw new AlfrescoParseException("Username must be a string. It was: " + usernameElement.toString());
    }
    return usernameElement.getAsString();
}

From source file:com.github.maoo.indexer.client.WebScriptsAlfrescoClient.java

License:Apache License

private List<String> getAuthorities(JsonObject userObject) {
    List<String> authorities = new ArrayList<String>();
    if (!userObject.has(AUTHORITIES)) {
        throw new AlfrescoParseException("Json response is authorities.");
    }/*from  w ww .  j a  v a 2 s.c o m*/
    JsonElement authoritiesElement = userObject.get(AUTHORITIES);
    if (!authoritiesElement.isJsonArray()) {
        throw new AlfrescoParseException(
                "Authorities must be a json array. It was: " + authoritiesElement.toString());
    }
    JsonArray authoritiesArray = authoritiesElement.getAsJsonArray();
    for (JsonElement authorityElement : authoritiesArray) {
        if (!authorityElement.isJsonPrimitive()) {
            throw new AlfrescoParseException(
                    "Authority entry must be a string. It was: " + authoritiesElement.toString());
        }
        JsonPrimitive authorityPrimitive = authorityElement.getAsJsonPrimitive();
        if (!authorityPrimitive.isString()) {
            throw new AlfrescoParseException(
                    "Authority entry must be a string. It was: " + authoritiesElement.toString());
        }
        authorities.add(authorityPrimitive.getAsString());
    }
    return authorities;
}

From source file:com.github.strawberry.util.Json.java

License:Open Source License

public static Collection parseArray(String json) {
    JsonArray o = (JsonArray) parser.parse(json);
    Collection c = Lists.newArrayList();
    for (JsonElement value : o) {
        if (!value.isJsonPrimitive()) {
            if (value.isJsonArray()) {
                c.add(parseArray(value.toString()));
            } else if (value.isJsonObject()) {
                c.add(parse(value.toString()));
            }//from w ww  .j a  v  a  2s.  com

        } else {
            c.add(parsePrimitive(value));

        }
    }
    return c;
}