Example usage for com.fasterxml.jackson.databind JsonNode longValue

List of usage examples for com.fasterxml.jackson.databind JsonNode longValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode longValue.

Prototype

public long longValue() 

Source Link

Usage

From source file:com.ning.metrics.serialization.hadoop.pig.SmileStorage.java

private Object getJsonValue(final SchemaFieldType type, final JsonNode node) {
    switch (type) {
    case BOOLEAN:
        return node.intValue();
    case BYTE://w w  w  . j a v  a2 s  . c  o  m
        return new Byte(node.textValue());
    case SHORT:
    case INTEGER:
        return node.intValue();
    case LONG:
    case DATE:
        return node.longValue();
    case DOUBLE:
        return node.doubleValue();
    case IP:
    case STRING:
    default:
        return node.textValue();
    }
}

From source file:com.liferay.sync.engine.SyncSystemTest.java

protected long getLong(JsonNode jsonNode, String key, long defaultValue) {
    JsonNode childJsonNode = jsonNode.get(key);

    if (childJsonNode == null) {
        return defaultValue;
    }/*from   w  ww.  j av  a2 s .c o m*/

    return childJsonNode.longValue();
}

From source file:eu.europa.ec.fisheries.uvms.reporting.service.util.ReportDeserializer.java

private void addAssetFilterDTO(List<FilterDTO> filterDTOList, Long reportId, JsonNode next) {
    AssetFilterDTO asset = new AssetFilterDTO();
    asset.setReportId(reportId);/* w w  w.  jav a 2 s  .c o m*/

    JsonNode idNode = next.get(FilterDTO.ID);
    if (idNode != null) {
        long id = idNode.longValue();
        asset.setId(id);
    }

    JsonNode guidNode = next.get(GUID);
    if (guidNode != null) {
        String guidValue = guidNode.textValue();
        asset.setGuid(guidValue);
    }

    JsonNode nameNode = next.get(NAME);
    if (nameNode != null) {
        String nameValue = nameNode.textValue();
        asset.setName(nameValue);
    }

    filterDTOList.add(asset);
}

From source file:org.envirocar.server.rest.decoding.json.SensorDecoder.java

private Object parseValueNode(JsonNode value) {
    if (value.isNull()) {
        return null;
    } else if (value.isNumber()) {
        if (value.isFloatingPointNumber()) {
            if (value.isBigDecimal()) {
                return value.decimalValue();
            } else if (value.isDouble()) {
                return value.asDouble();
            }//from   w  ww . j a v  a  2 s.co m
        } else if (value.isIntegralNumber()) {
            if (value.isBigInteger()) {
                return value.bigIntegerValue();
            } else if (value.isLong()) {
                return value.longValue();
            } else if (value.isInt()) {
                return value.intValue();
            }
        }
        return value.numberValue();
    } else if (value.isTextual()) {
        return value.asText();
    } else if (value.isBoolean()) {
        return value.booleanValue();
    }
    throw new IllegalArgumentException("can not decode " + value);
}

From source file:eu.europa.ec.fisheries.uvms.reporting.service.util.ReportDeserializer.java

@Override
public ReportDTO deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {

    ObjectCodec oc = jsonParser.getCodec();
    JsonNode node = oc.readTree(jsonParser);

    ReportTypeEnum reportTypeEnum = node.get(REPORT_TYPE) != null
            ? ReportTypeEnum.getReportTypeEnum(node.get(REPORT_TYPE).textValue())
            : ReportTypeEnum.STANDARD;//from w ww  . ja v a  2  s  . co  m

    List<FilterDTO> filterDTOList = new ArrayList<>();

    JsonNode reportIdNode = node.get(ID);
    Long reportId = null;
    if (reportIdNode != null) {
        reportId = reportIdNode.longValue();
    }

    JsonNode filterNode = node.get(FILTER_EXPRESSION);

    if (filterNode != null) {
        addVmsFilters(filterNode.get("vms"), filterDTOList, reportId);
        addAssets(filterNode.get(ASSETS), filterDTOList, reportId);
        addArea(filterNode.get("areas"), filterDTOList, reportId);
        addCommon(filterNode.get("common"), filterDTOList, reportId);
        addFaFilters(filterNode.get("fa"), filterDTOList, reportId);
        if (ReportTypeEnum.SUMMARY.equals(reportTypeEnum)) {
            addGroupCriteria(filterNode.get("criteria"), filterDTOList, reportId, jsonParser);
        }
    }

    boolean withMap = false;
    JsonNode witMapNode = node.get(WITH_MAP);
    if (witMapNode != null) {
        withMap = witMapNode.booleanValue();
    }

    VisibilityEnum visibilityEnum = null;
    JsonNode visibilityNode = node.get(VISIBILITY);
    if (visibilityNode != null) {
        String s = visibilityNode.textValue();
        if (s != null) {
            visibilityEnum = VisibilityEnum.valueOf(s.toUpperCase());
        }
    }

    String nameValue = null;
    JsonNode nameNode = node.get(NAME);
    if (nameNode != null) {
        nameValue = nameNode.textValue();
    }

    JsonNode createdOnNode = node.get(CREATED_ON);
    String createdOnValue = null;
    if (createdOnNode != null) {
        createdOnValue = createdOnNode.textValue();
    }

    JsonNode editableNode = node.get("editable");
    boolean editableValue = false;
    if (createdOnNode != null) {
        editableValue = editableNode.booleanValue();
    }

    ReportDTO build = ReportDTO.builder()
            .description(node.get(DESC) != null ? node.get(DESC).textValue() : null)
            .id(node.get(ID) != null ? node.get(ID).longValue() : null).name(nameValue).withMap(withMap)
            .createdBy(node.get(CREATED_BY) != null ? node.get(CREATED_BY).textValue() : null)
            .filters(filterDTOList).visibility(visibilityEnum)
            .mapConfiguration(createMapConfigurationDTO(withMap, node.get(MAP_CONFIGURATION))).build();
    build.setReportTypeEnum(reportTypeEnum);
    build.setAudit(new AuditDTO(createdOnValue));
    build.setEditable(editableValue);
    return build;
}

From source file:com.redhat.lightblue.metadata.parser.JSONMetadataParserTest.java

License:asdf

@Test
public void putValueLong() {
    ObjectNode parent = new ObjectNode(factory);
    String name = "foo";
    Long value = 1272722l;

    parser.putValue(parent, name, value);

    JsonNode x = parent.get(name);//from   w  ww  .  ja v a  2s. c  o  m

    Assert.assertNotNull(x);
    Assert.assertEquals(value.longValue(), x.longValue());
}

From source file:com.amazonaws.services.iot.client.shadow.AwsIotDeviceCommandManager.java

private AwsIotDeviceCommand getPendingCommand(AWSIotMessage message) {
    String payload = message.getStringPayload();
    if (payload == null) {
        return null;
    }//  w w w .  j  av a2  s . com

    try {
        JsonNode jsonNode = objectMapper.readTree(payload);
        if (!jsonNode.isObject()) {
            return null;
        }

        JsonNode node = jsonNode.get(COMMAND_ID_FIELD);
        if (node == null) {
            return null;
        }

        String commandId = node.textValue();
        AwsIotDeviceCommand command = pendingCommands.remove(commandId);
        if (command == null) {
            return null;
        }

        node = jsonNode.get(ERROR_CODE_FIELD);
        if (node != null) {
            command.setErrorCode(AWSIotDeviceErrorCode.valueOf(node.longValue()));
        }

        node = jsonNode.get(ERROR_MESSAGE_FIELD);
        if (node != null) {
            command.setErrorMessage(node.textValue());
        }

        return command;
    } catch (IOException e) {
        return null;
    }
}

From source file:controllers.EntryController.java

@Transactional
@BodyParser.Of(BodyParser.Json.class)
public Result enter() {
    Entry entry = new Entry();
    ArrayList<String> missing = new ArrayList<String>();
    JsonNode json = request().body().asJson();
    Logger.debug("GOT: " + json.toString());
    boolean retServerId = false;
    JsonNode value;
    value = json.findValue("tech_id");
    if (value == null) {
        missing.add("tech_id");
    } else {//from   w  ww .jav a2 s  .  c  om
        entry.tech_id = value.intValue();
    }
    value = json.findValue("date");
    if (value == null) {
        missing.add("date");
    } else {
        entry.entry_time = new Date(value.longValue());
    }
    value = json.findValue("server_id");
    if (value != null) {
        entry.id = value.longValue();
        retServerId = true;
        Entry existing;
        if (entry.id > 0) {
            existing = Entry.find.byId(entry.id);
            existing.entry_time = entry.entry_time;
            existing.tech_id = entry.tech_id;
        } else {
            existing = Entry.findByDate(entry.tech_id, entry.entry_time);
        }
        if (existing != null) {
            entry = existing;
        }
    }
    int truck_number;
    String license_plate;
    value = json.findValue("truck_number");
    if (value != null) {
        truck_number = value.intValue();
    } else {
        truck_number = 0;
    }
    value = json.findValue("license_plate");
    if (value != null) {
        license_plate = value.textValue();
    } else {
        license_plate = null;
    }
    if (truck_number == 0 && license_plate == null) {
        missing.add("truck_number");
        missing.add("license_plate");
    } else {
        // Note: I don't call Version.inc(Version.VERSION_TRUCK) intentionally.
        // The reason is that other techs don't need to know about a local techs truck updates.
        Truck truck = Truck.add(truck_number, license_plate, entry.tech_id);
        entry.truck_id = truck.id;
    }
    value = json.findValue("project_id");
    if (value == null) {
        missing.add("project_id");
    } else {
        entry.project_id = value.longValue();
    }
    value = json.findValue("status");
    if (value != null) {
        entry.status = Entry.Status.from(value.textValue());
    }
    value = json.findValue("address_id");
    if (value == null) {
        value = json.findValue("address");
        if (value == null) {
            missing.add("address_id");
        } else {
            String address = value.textValue().trim();
            if (address.length() > 0) {
                try {
                    Company company = Company.parse(address);
                    Company existing = Company.has(company);
                    if (existing != null) {
                        company = existing;
                    } else {
                        company.created_by = entry.tech_id;
                        company.save();
                        Version.inc(Version.VERSION_COMPANY);
                    }
                    entry.company_id = company.id;
                } catch (Exception ex) {
                    return badRequest2("address: " + ex.getMessage());
                }
            } else {
                return badRequest2("address");
            }
        }
    } else {
        entry.company_id = value.longValue();
    }
    value = json.findValue("equipment");
    if (value != null) {
        if (value.getNodeType() != JsonNodeType.ARRAY) {
            Logger.error("Expected array for element 'equipment'");
        } else {
            int collection_id;
            if (entry.equipment_collection_id > 0) {
                collection_id = (int) entry.equipment_collection_id;
                EntryEquipmentCollection.deleteByCollectionId(entry.equipment_collection_id);
            } else {
                collection_id = Version.inc(Version.NEXT_EQUIPMENT_COLLECTION_ID);
            }
            boolean newEquipmentCreated = false;
            Iterator<JsonNode> iterator = value.elements();
            while (iterator.hasNext()) {
                JsonNode ele = iterator.next();
                EntryEquipmentCollection collection = new EntryEquipmentCollection();
                collection.collection_id = (long) collection_id;
                JsonNode subvalue = ele.findValue("equipment_id");
                if (subvalue == null) {
                    subvalue = ele.findValue("equipment_name");
                    if (subvalue == null) {
                        missing.add("equipment_id");
                        missing.add("equipment_name");
                    } else {
                        String name = subvalue.textValue();
                        List<Equipment> equipments = Equipment.findByName(name);
                        Equipment equipment;
                        if (equipments.size() == 0) {
                            equipment = new Equipment();
                            equipment.name = name;
                            equipment.created_by = entry.tech_id;
                            equipment.save();
                        } else {
                            if (equipments.size() > 1) {
                                Logger.error("Too many equipments found with name: " + name);
                            }
                            equipment = equipments.get(0);
                        }
                        collection.equipment_id = equipment.id;
                        newEquipmentCreated = true;
                    }
                } else {
                    collection.equipment_id = subvalue.longValue();
                }
                collection.save();
            }
            entry.equipment_collection_id = collection_id;
            if (newEquipmentCreated) {
                Version.inc(Version.VERSION_EQUIPMENT);
            }
        }
    }
    value = json.findValue("picture");
    if (value != null) {
        if (value.getNodeType() != JsonNodeType.ARRAY) {
            Logger.error("Expected array for element 'picture'");
        } else {
            int collection_id;
            if (entry.picture_collection_id > 0) {
                collection_id = (int) entry.picture_collection_id;
                PictureCollection.deleteByCollectionId(entry.picture_collection_id, null);
            } else {
                collection_id = Version.inc(Version.NEXT_PICTURE_COLLECTION_ID);
            }
            Iterator<JsonNode> iterator = value.elements();
            while (iterator.hasNext()) {
                JsonNode ele = iterator.next();
                PictureCollection collection = new PictureCollection();
                collection.collection_id = (long) collection_id;
                JsonNode subvalue = ele.findValue("note");
                if (subvalue != null) {
                    collection.note = subvalue.textValue();
                }
                subvalue = ele.findValue("filename");
                if (subvalue == null) {
                    missing.add("filename");
                } else {
                    collection.picture = subvalue.textValue();
                    collection.save();
                }
            }
            entry.picture_collection_id = collection_id;
        }
    }
    value = json.findValue("notes");
    if (value != null) {
        if (value.getNodeType() != JsonNodeType.ARRAY) {
            Logger.error("Expected array for element 'notes'");
        } else {
            int collection_id;
            if (entry.note_collection_id > 0) {
                collection_id = (int) entry.note_collection_id;
                EntryNoteCollection.deleteByCollectionId(entry.note_collection_id);
            } else {
                collection_id = Version.inc(Version.NEXT_NOTE_COLLECTION_ID);
            }
            Iterator<JsonNode> iterator = value.elements();
            while (iterator.hasNext()) {
                JsonNode ele = iterator.next();
                EntryNoteCollection collection = new EntryNoteCollection();
                collection.collection_id = (long) collection_id;
                JsonNode subvalue = ele.findValue("id");
                if (subvalue == null) {
                    subvalue = ele.findValue("name");
                    if (subvalue == null) {
                        missing.add("note:id, note:name");
                    } else {
                        String name = subvalue.textValue();
                        List<Note> notes = Note.findByName(name);
                        if (notes == null || notes.size() == 0) {
                            missing.add("note:" + name);
                        } else if (notes.size() > 1) {
                            Logger.error("Too many notes with name: " + name);
                            missing.add("note:" + name);
                        } else {
                            collection.note_id = notes.get(0).id;
                        }
                    }
                } else {
                    collection.note_id = subvalue.longValue();
                }
                subvalue = ele.findValue("value");
                if (value == null) {
                    missing.add("note:value");
                } else {
                    collection.note_value = subvalue.textValue();
                }
                collection.save();
            }
            entry.note_collection_id = collection_id;
        }
    }
    if (missing.size() > 0) {
        return missingRequest(missing);
    }
    if (entry.id != null && entry.id > 0) {
        entry.update();
        Logger.debug("Updated entry " + entry.id);
    } else {
        entry.save();
        Logger.debug("Created new entry " + entry.id);
    }
    long ret_id;
    if (retServerId) {
        ret_id = entry.id;
    } else {
        ret_id = 0;
    }
    return ok(Long.toString(ret_id));
}

From source file:com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.JsonRenderedValueConverter.java

@Override
public Object convertRenderedValue(String rendered) {
    if (NumberUtils.isNumber(rendered)) {
        if (rendered.contains(".")) {
            return NumberUtils.createDouble(rendered);
        }//from  www  . j a v  a2s  . co m
        try {
            return NumberUtils.createInteger(rendered);
        } catch (NumberFormatException ignored) {
            return NumberUtils.createLong(rendered);
        }
    } else if (rendered.equals("true") || rendered.equals("false")) {
        return Boolean.parseBoolean(rendered);
    } else if (rendered.startsWith("{{") || (!rendered.startsWith("{") && !rendered.startsWith("["))) {
        return rendered;
    }

    JsonNode node;
    try {
        node = pipelineTemplateObjectMapper.readTree(rendered);
    } catch (IOException e) {
        throw new TemplateRenderException("template produced invalid json", e);
    }

    try {
        if (node.isArray()) {
            return pipelineTemplateObjectMapper.readValue(rendered, Collection.class);
        }
        if (node.isObject()) {
            return pipelineTemplateObjectMapper.readValue(rendered, HashMap.class);
        }
        if (node.isBoolean()) {
            return Boolean.parseBoolean(node.asText());
        }
        if (node.isDouble()) {
            return node.doubleValue();
        }
        if (node.canConvertToInt()) {
            return node.intValue();
        }
        if (node.canConvertToLong()) {
            return node.longValue();
        }
        if (node.isTextual()) {
            return node.textValue();
        }
        if (node.isNull()) {
            return null;
        }
    } catch (IOException e) {
        throw new TemplateRenderException("template produced invalid json", e);
    }

    throw new TemplateRenderException("unknown rendered object type");
}

From source file:com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.HandlebarsRenderer.java

@Override
public Object renderGraph(String template, RenderContext context) {
    String rendered = render(template, context);

    // Short-circuit primitive values.
    // TODO rz - having trouble getting jackson to parse primitive values outside of unit tests
    if (NumberUtils.isNumber(rendered)) {
        if (rendered.contains(".")) {
            return NumberUtils.createDouble(rendered);
        }//from w w w .j a  v  a 2s  .c o  m
        try {
            return NumberUtils.createInteger(rendered);
        } catch (NumberFormatException ignored) {
            return NumberUtils.createLong(rendered);
        }
    } else if (rendered.equals("true") || rendered.equals("false")) {
        return Boolean.parseBoolean(rendered);
    } else if (!rendered.startsWith("{") && !rendered.startsWith("[")) {
        return rendered;
    }

    JsonNode node;
    try {
        node = pipelineTemplateObjectMapper.readTree(rendered);
    } catch (IOException e) {
        throw new TemplateRenderException("template produced invalid json", e);
    }

    try {
        if (node.isArray()) {
            return pipelineTemplateObjectMapper.readValue(rendered, Collection.class);
        }
        if (node.isObject()) {
            return pipelineTemplateObjectMapper.readValue(rendered, HashMap.class);
        }
        if (node.isBoolean()) {
            return Boolean.parseBoolean(node.asText());
        }
        if (node.isDouble()) {
            return node.doubleValue();
        }
        if (node.canConvertToInt()) {
            return node.intValue();
        }
        if (node.canConvertToLong()) {
            return node.longValue();
        }
        if (node.isTextual()) {
            return node.textValue();
        }
        if (node.isNull()) {
            return null;
        }
    } catch (IOException e) {
        throw new TemplateRenderException("template produced invalid json", e);
    }

    throw new TemplateRenderException("unknown rendered object type");
}