Example usage for com.google.gson JsonElement isJsonPrimitive

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

Introduction

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

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:com.simiacryptus.mindseye.layers.aparapi.ConvolutionLayer.java

License:Apache License

@Nonnull
@Override//from  ww w .ja  v  a 2s .  c o m
public JsonObject getJson(Map<CharSequence, byte[]> resources, @Nonnull DataSerializer dataSerializer) {
    @Nonnull
    final JsonObject json = super.getJsonStub();
    json.add("filter", kernel.toJson(resources, dataSerializer));
    JsonElement paddingX = json.get("paddingX");
    if (null != paddingX && paddingX.isJsonPrimitive())
        this.setPaddingX((paddingX.getAsInt()));
    JsonElement paddingY = json.get("paddingY");
    if (null != paddingY && paddingY.isJsonPrimitive())
        this.setPaddingY((paddingY.getAsInt()));
    return json;
}

From source file:com.simiacryptus.mindseye.layers.cudnn.conv.ConvolutionLayer.java

License:Apache License

/**
 * Instantiates a new Convolution key./*from  w w w.j a  v a 2 s  .  co  m*/
 *
 * @param json      the json
 * @param resources the resources
 */
protected ConvolutionLayer(@Nonnull final JsonObject json, Map<CharSequence, byte[]> resources) {
    super(json);
    this.kernel = Tensor.fromJson(json.get("filter"), resources);
    assert getKernel().isValid();
    this.setBatchBands(json.get("batchBands").getAsInt());
    this.setStrideX(json.get("strideX").getAsInt());
    this.setStrideY(json.get("strideY").getAsInt());
    JsonElement paddingX = json.get("paddingX");
    if (null != paddingX && paddingX.isJsonPrimitive())
        this.setPaddingX((paddingX.getAsInt()));
    JsonElement paddingY = json.get("paddingY");
    if (null != paddingY && paddingY.isJsonPrimitive())
        this.setPaddingY((paddingY.getAsInt()));
    this.precision = Precision.valueOf(json.get("precision").getAsString());
    this.inputBands = json.get("inputBands").getAsInt();
    this.outputBands = json.get("outputBands").getAsInt();
}

From source file:com.sixt.service.framework.protobuf.ProtobufUtil.java

License:Apache License

private static Object parseField(Descriptors.FieldDescriptor field, JsonElement value,
        Message.Builder enclosingBuilder) throws Exception {
    switch (field.getType()) {
    case DOUBLE://from   w w w  .  j  a  v  a 2s .  c o m
        if (!value.isJsonPrimitive()) {
            // fail;
        }
        return value.getAsDouble();
    case FLOAT:
        if (!value.isJsonPrimitive()) {
            // fail;
        }
        return value.getAsFloat();
    case INT64:
    case UINT64:
    case FIXED64:
    case SINT64:
    case SFIXED64:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsLong();
    case INT32:
    case UINT32:
    case FIXED32:
    case SINT32:
    case SFIXED32:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsInt();
    case BOOL:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsBoolean();
    case STRING:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return value.getAsString();
    case GROUP:
    case MESSAGE:
        if (!value.isJsonObject()) {
            // fail
        }
        return fromJson(enclosingBuilder.newBuilderForField(field), value.getAsJsonObject());
    case BYTES:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        return ByteString.copyFrom(BaseEncoding.base64().decode(value.getAsString()));
    case ENUM:
        if (!value.isJsonPrimitive()) {
            // fail
        }
        String protoEnumValue = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, value.getAsString());
        return field.getEnumType().findValueByName(protoEnumValue);
    }
    return null;
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

License:Open Source License

private static boolean isString(JsonElement element) {
    assert element != null;

    return element.isJsonPrimitive() && ((JsonPrimitive) element).isString();
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

License:Open Source License

private @CheckForNull Object toSimpleJavaType(JsonElement jsonValue) {
    if (jsonValue == null)
        return null; //VR
    Object value = null;// ww  w  . ja v a  2s  .com
    if (!jsonValue.isJsonNull()) {
        if (jsonValue.isJsonPrimitive()) {
            JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
            if (primitive.isBoolean()) {
                value = Boolean.valueOf(primitive.getAsBoolean());
            } else if (primitive.isNumber()) {
                value = Double.valueOf(primitive.getAsDouble());
            } else if (primitive.isString()) {
                value = primitive.getAsString();
            } else {
                throw UnexpectedException.forUnexpectedCodeBranchExecuted();
            }
        } else if (jsonValue.isJsonArray()) {
            //This simply does not work (?) 
            JsonArray array = jsonValue.getAsJsonArray();
            Object[] result = new Object[array.size()];
            for (int i = 0; i < array.size(); i++) {
                result[i] = toSimpleJavaType(array.get(i));
            }
            value = result;
        } else if (jsonValue.isJsonObject()) {
            //This simply does not work (?)
            //value = getGson().fromJson(jsonValue, Map.class );

            JsonObject obj = jsonValue.getAsJsonObject();
            Iterator<Entry<String, JsonElement>> properties = obj.entrySet().iterator();
            Map<String, Object> result = new HashMap<String, Object>();
            while (properties.hasNext()) {
                Entry<String, JsonElement> property = properties.next();
                JsonElement propertyValue = property.getValue();
                result.put(property.getKey(), toSimpleJavaType(propertyValue));
            }
            value = result;
        } else {
            throw UnexpectedException.forUnexpectedCodeBranchExecuted();
        }
    }

    return value;
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

License:Open Source License

private static boolean isValidJsonTypeForJavaType(JsonElement jsonElement, Class<?> parameterType) {
    assert jsonElement != null;
    assert parameterType != null;

    // Check json nulls
    if (jsonElement.isJsonNull()) {
        return !parameterType.isPrimitive();
    }//from  ww w.ja  v a  2s.  com

    if (parameterType.isArray()) {
        // This is *always* ok because if the value is not a json array
        // we will instantiate a single item array and attempt conversion
        return true;
    }

    if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isBoolean();
    } else if (parameterType.equals(char.class) || parameterType.equals(Character.class)) {
        if (jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString()) {
            return jsonElement.getAsString().length() == 1;
        }
        return false;
    } else if (parameterType.equals(String.class)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString();
    } else if (ClassUtils.isNumericType(parameterType)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isNumber();
    }

    // If we arrived here, assume somebody will know how to handle the json element, maybe customizing Gson's serialization
    return true;
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

License:Open Source License

private static <T> T getNonEmptyJsonPrimitiveValue(JsonObject object, String elementName,
        PrimitiveJsonValueGetter<T> getter) {
    assert object != null;
    assert !StringUtils.isEmpty(elementName);

    try {// w  ww.  j  a va 2 s  . c o  m
        JsonElement element = object.get(elementName);
        if (element == null) {
            RequestException ex = RequestException.forJsonElementMissing(elementName);
            logger.error(ex.getMessage(), ex);
            throw ex;
        }

        // Take into account that the element must be a primitive, and then a string!
        T result = null;
        if (element.isJsonPrimitive()) {
            result = getter.checkedGet((JsonPrimitive) element);
        }

        if (result == null) {
            RequestException ex = RequestException.forJsonElementMustBeANonNullOrEmptyValue(elementName,
                    getter.getValueType());
            logger.error(ex.getMessage(), ex);
            throw ex;
        }

        return result;
    } catch (JsonParseException e) {
        String message = "Probably a DirectJNgine BUG: there should not be JSON parse exceptions: we should have checked ALL error conditions. "
                + e.getMessage();
        logger.error(message, e);
        assert false : message;
        throw e; // Just to make the compiler happy -because of the assert
    }
}

From source file:com.softwarementors.extjs.djn.test.config.GsonBuilderConfiguratorForTesting.java

License:Open Source License

private static int getIntValue(JsonObject parent, String elementName) {
    assert parent != null;
    assert !StringUtils.isEmpty(elementName);

    JsonElement element = parent.get(elementName);
    if (!element.isJsonPrimitive()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }//from w  w  w . j  a  v a2s  . c  o  m
    JsonPrimitive primitiveElement = (JsonPrimitive) element;
    if (!primitiveElement.isNumber()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }
    return primitiveElement.getAsInt();
}

From source file:com.stackmob.sdk.callback.StackMobBinaryCallback.java

License:Apache License

private boolean isTemporaryPasswordMessage(String name) {
    try {/*  w  ww. java2 s.c om*/
        String test = new String(responseBody, "UTF-8");
        JsonElement message = new JsonParser().parse(new String(responseBody, "UTF-8")).getAsJsonObject()
                .get(name);
        return message != null && message.isJsonPrimitive() && message.getAsJsonPrimitive().isString()
                && message.getAsString().startsWith("Temporary password reset required.");
    } catch (Exception e) {
        return false;
    }
}

From source file:com.stackmob.sdk.callback.StackMobCallback.java

License:Apache License

private boolean isTemporaryPasswordMessage(String name) {
    boolean matched = false;
    try {/*from   w  w  w  .j a va  2s.  co m*/
        JsonElement message = new JsonParser().parse(new String(responseBody, "UTF-8")).getAsJsonObject()
                .get(name);
        matched = message != null && message.isJsonPrimitive() && message.getAsJsonPrimitive().isString()
                && message.getAsString().startsWith("Temporary password reset required.");
    } catch (Throwable ignore) {
    }
    return matched;
}