Example usage for com.google.gson JsonElement getAsFloat

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

Introduction

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

Prototype

public float getAsFloat() 

Source Link

Document

convenience method to get this element as a primitive float value.

Usage

From source file:com.graphaware.module.es.Neo4jElasticVerifier.java

License:Open Source License

private void checkFloatArray(JsonObject source, String key, Map<String, Object> properties) {
    assertTrue(source.get(key) instanceof JsonArray);
    JsonArray jsonArray = source.get(key).getAsJsonArray();
    TreeSet<Float> esSet = new TreeSet();
    for (JsonElement element : jsonArray) {
        esSet.add(element.getAsFloat());
    }/*from w w  w .  j  a va2  s  .co  m*/
    TreeSet<Float> nodeSet = new TreeSet();
    float[] propertyArray = (float[]) properties.get(key);
    for (float element : propertyArray) {
        nodeSet.add(element);
    }
    assertEquals(esSet, nodeSet);
}

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  ww.jav a 2s  .com*/
        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.sldeditor.exportdata.esri.symbols.CartographicLineSymbol.java

License:Open Source License

/**
 * Convert./*from  ww w  . j  av a2  s.c o  m*/
 *
 * @param element the element
 * @return the list of strokes
 */
@SuppressWarnings("deprecation")
@Override
public List<Stroke> convert(JsonElement element) {

    if (element == null)
        return null;

    JsonObject obj = element.getAsJsonObject();

    List<Stroke> strokeList = new ArrayList<Stroke>();

    double width = getDouble(obj, CommonSymbolKeys.WIDTH);

    Stroke stroke = styleFactory.createStroke(getColour(obj.get(CommonSymbolKeys.COLOUR)), ff.literal(width));

    // Line dash pattern
    JsonElement templateElement = obj.get(CartographicLineSymbolKeys.TEMPLATE);
    if (templateElement != null) {
        JsonArray templateArray = templateElement.getAsJsonArray();
        List<Float> patternList = new ArrayList<Float>();

        for (int index = 0; index < templateArray.size(); index++) {
            JsonObject patternElement = templateArray.get(index).getAsJsonObject();
            float mark = 0.0f;
            JsonElement markElement = patternElement.get(CartographicLineSymbolKeys.MARK);
            if (markElement != null) {
                mark = markElement.getAsFloat();
            }
            patternList.add(mark);

            float gap = 0.0f;
            JsonElement gapElement = patternElement.get(CartographicLineSymbolKeys.GAP);
            if (gapElement != null) {
                gap = gapElement.getAsFloat();
            }
            patternList.add(gap);
        }

        float[] dashArray = new float[patternList.size()];

        int index = 0;
        for (Float value : patternList) {
            dashArray[index] = value;
            index++;
        }

        stroke.setDashArray(dashArray);

        // Start of line offset
        JsonElement lineStartOffsetElement = obj.get(CartographicLineSymbolKeys.LINE_START_OFFSET);

        if (lineStartOffsetElement != null) {
            stroke.setDashOffset(ff.literal(lineStartOffsetElement.getAsDouble()));
        }
    }

    strokeList.add(stroke);

    return strokeList;
}

From source file:com.stratio.decision.serializer.gson.impl.ColumnNameTypeValueDeserializer.java

License:Apache License

@Override
public ColumnNameTypeValue deserialize(JsonElement element, Type type, JsonDeserializationContext ctx)
        throws JsonParseException {
    final JsonObject object = element.getAsJsonObject();
    String name = null;/*from   w  w  w .j a v a 2 s . c om*/
    ColumnType columnType = null;
    Object value = null;
    if (object != null && object.has(COLUMN_FIELD) && object.has(TYPE_FIELD)) {
        name = object.get(COLUMN_FIELD).getAsString();
        columnType = ColumnType.valueOf(object.get(TYPE_FIELD).getAsString());

        if (object.has(VALUE_FIELD)) {
            JsonElement jsonValue = object.get(VALUE_FIELD);
            switch (columnType) {
            case BOOLEAN:
                value = jsonValue.getAsBoolean();
                break;
            case DOUBLE:
                value = jsonValue.getAsDouble();
                break;
            case FLOAT:
                value = jsonValue.getAsFloat();
                break;
            case INTEGER:
                value = jsonValue.getAsInt();
                break;
            case LONG:
                value = jsonValue.getAsLong();
                break;
            case STRING:
                value = jsonValue.getAsString();
                break;
            default:
                break;
            }
        } else {
            log.warn("Column with name {} has no value", name);
        }

        if (log.isDebugEnabled()) {
            log.debug("Values obtained into ColumnNameTypeValue deserialization: "
                    + "NAME: {}, VALUE: {}, COLUMNTYPE: {}", name, value, columnType);
        }
    } else {
        log.warn("Error deserializing ColumnNameTypeValue from json. JsonObject is not complete: {}", element);
    }

    return new ColumnNameTypeValue(name, columnType, value);
}

From source file:com.tsc9526.monalisa.orm.tools.converters.impl.ArrayTypeConversion.java

License:Open Source License

protected Object convertJsonToArray(JsonArray array, Class<?> type) {
    Object value = null;/*from  w w  w .  j  av  a2 s  .  c o  m*/
    if (type == int[].class) {
        int[] iv = new int[array.size()];
        for (int i = 0; i < array.size(); i++) {
            JsonElement e = array.get(i);
            iv[i] = e.getAsInt();
        }
        value = iv;
    } else if (type == float[].class) {
        float[] iv = new float[array.size()];
        for (int i = 0; i < array.size(); i++) {
            JsonElement e = array.get(i);
            iv[i] = e.getAsFloat();
        }
        value = iv;
    } else if (type == long[].class) {
        long[] iv = new long[array.size()];
        for (int i = 0; i < array.size(); i++) {
            JsonElement e = array.get(i);
            iv[i] = e.getAsLong();
        }
        value = iv;
    } else if (type == double[].class) {
        double[] iv = new double[array.size()];
        for (int i = 0; i < array.size(); i++) {
            JsonElement e = array.get(i);
            iv[i] = e.getAsDouble();
        }
        value = iv;
    } else {//String[]
        String[] iv = new String[array.size()];
        for (int i = 0; i < array.size(); i++) {
            JsonElement e = array.get(i);
            if (e.isJsonPrimitive()) {
                iv[i] = e.getAsString();
            } else {
                iv[i] = e.toString();
            }
        }
        value = iv;
    }

    return value;
}

From source file:com.unovo.frame.utils.gson.deserializer.FloatJsonDeserializer.java

License:Open Source License

@Override
public Float deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/* ww  w .j a  v  a  2 s  . c om*/
        return json.getAsFloat();
    } catch (Exception e) {
        Logger.i("FloatJsonDeserializer-deserialize-error:" + (json != null ? json.toString() : ""));
        return 0F;
    }
}

From source file:com.wallellen.wechat.common.util.json.GsonHelper.java

License:Open Source License

public static Float getAsFloat(JsonElement element) {
    return isNull(element) ? null : element.getAsFloat();
}

From source file:de.azapps.mirakel.model.task.TaskDeserializer.java

License:Open Source License

@Override
public Task deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject el = json.getAsJsonObject();
    Optional<ListMirakel> taskList = absent();
    final JsonElement id = el.get("id");
    if (id == null) {
        throw new JsonParseException("Json malformed");
    }/*from w  w  w. j  a va2s . com*/
    final Task task = Task.get(id.getAsLong()).or(new Task());
    // Name
    final Set<Entry<String, JsonElement>> entries = el.entrySet();
    for (final Entry<String, JsonElement> entry : entries) {
        final String key = entry.getKey();
        final JsonElement val = entry.getValue();
        if ((key == null) || "id".equalsIgnoreCase(key)) {
            continue;
        }
        switch (key.toLowerCase()) {
        case "name":
            task.setName(val.getAsString());
            break;
        case "content":
            String content = val.getAsString();
            if (content == null) {
                content = "";
            }
            task.setContent(content);
            break;
        case "priority":
            task.setPriority((int) val.getAsFloat());
            break;
        case "progress":
            task.setProgress((int) val.getAsDouble());
            break;
        case "list_id":
            taskList = ListMirakel.get(val.getAsInt());
            if (!taskList.isPresent()) {
                taskList = fromNullable(SpecialList.firstSpecialSafe().getDefaultList());
            }
            break;
        case "created_at":
            try {
                task.setCreatedAt(DateTimeHelper.parseDateTime(val.getAsString().replace(":", "")));
            } catch (final ParseException e) {
                Log.wtf(TAG, "invalid dateformat: ", e);
            }
            break;
        case "updated_at":
            try {
                task.setUpdatedAt(DateTimeHelper.parseDateTime(val.getAsString().replace(":", "")));
            } catch (final ParseException e) {
                Log.wtf(TAG, "invalid dateformat: ", e);
            }
            break;
        case "done":
            task.setDone(val.getAsBoolean());
            break;
        case "due":
            try {
                task.setDue(of(DateTimeHelper.createLocalCalendar(val.getAsLong())));
            } catch (final NumberFormatException ignored) {
                task.setDue(Optional.<Calendar>absent());
            }
            break;
        case "reminder":
            try {
                task.setReminder(of(DateTimeHelper.createLocalCalendar(val.getAsLong())));
            } catch (final NumberFormatException ignored) {
                task.setReminder(Optional.<Calendar>absent());
            }
            break;
        case "tags":
            handleTags(task, val);
            break;
        case "sync_state":
            task.setSyncState(DefinitionsHelper.SYNC_STATE.valueOf((short) val.getAsFloat()));
            break;
        case "show_recurring":
            task.setIsRecurringShown(val.getAsBoolean());
            break;
        default:
            handleAdditionalEntries(task, key, val);
            break;
        }
    }
    if (!taskList.isPresent()) {
        taskList = of(ListMirakel.safeFirst());
    }
    task.setList(taskList.get(), true);
    return task;
}

From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java

License:Creative Commons License

public static float getFloatVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }//w  ww.jav  a  2s . c  om

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsFloat();
}

From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java

License:Creative Commons License

public static float getFloatVal(JsonElement json, float defVal) {
    if (json == null || !json.isJsonPrimitive()) {
        return defVal;
    }//  w  ww . j a va2s.  co m

    return json.getAsFloat();
}