Search in sources :

Example 81 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project JMRI by JMRI.

the class JsonUtil method getRosterEntry.

/**
     * Returns the JSON representation of a roster entry.
     *
     * Note that this returns, for images and icons, a URL relative to the root
     * folder of the JMRI server. It is expected that clients will fill in the
     * server IP address and port as they know it to be.
     *
     * @param locale The client Locale
     * @param re     A RosterEntry that may or may not be in the roster.
     * @return a roster entry in JSON notation
     * @deprecated since 4.3.5
     */
@Deprecated
public static JsonNode getRosterEntry(Locale locale, RosterEntry re) {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, ROSTER_ENTRY);
    ObjectNode entry = root.putObject(DATA);
    entry.put(NAME, re.getId());
    entry.put(ADDRESS, re.getDccAddress());
    entry.put(IS_LONG_ADDRESS, re.isLongAddress());
    entry.put(ROAD, re.getRoadName());
    entry.put(NUMBER, re.getRoadNumber());
    entry.put(MFG, re.getMfg());
    entry.put(DECODER_MODEL, re.getDecoderModel());
    entry.put(DECODER_FAMILY, re.getDecoderFamily());
    entry.put(MODEL, re.getModel());
    entry.put(COMMENT, re.getComment());
    entry.put(MAX_SPD_PCT, Integer.toString(re.getMaxSpeedPCT()));
    entry.put(IMAGE, (re.getImagePath() != null) ? "/" + ROSTER + "/" + re.getId() + "/" + IMAGE : (String) null);
    entry.put(ICON, (re.getIconPath() != null) ? "/" + ROSTER + "/" + re.getId() + "/" + ICON : (String) null);
    entry.put(SHUNTING_FUNCTION, re.getShuntingFunction());
    ArrayNode labels = entry.putArray(FUNCTION_KEYS);
    for (int i = 0; i <= re.getMAXFNNUM(); i++) {
        ObjectNode label = mapper.createObjectNode();
        label.put(NAME, F + i);
        label.put(LABEL, re.getFunctionLabel(i));
        label.put(LOCKABLE, re.getFunctionLockable(i));
        label.put(ICON, (re.getFunctionImage(i) != null) ? "/" + ROSTER + "/" + re.getId() + "/" + F + i + "/" + ICON : (String) null);
        label.put(SELECTED_ICON, (re.getFunctionSelectedImage(i) != null) ? "/" + ROSTER + "/" + re.getId() + "/" + F + i + "/" + SELECTED_ICON : (String) null);
        labels.add(label);
    }
    ArrayNode rga = entry.putArray(ROSTER_GROUPS);
    for (RosterGroup group : re.getGroups()) {
        rga.add(group.getName());
    }
    return root;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TrainCommon.splitString(jmri.jmrit.operations.trains.TrainCommon.splitString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RosterGroup(jmri.jmrit.roster.rostergroup.RosterGroup)

Example 82 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project JMRI by JMRI.

the class JsonUtil method getConsist.

/**
     * Get the JSON representation of a consist.
     *
     * The JSON representation is an object with the following data attributes:
     * <ul>
     * <li>address - integer address</li>
     * <li>isLongAddress - boolean true if address is long, false if short</li>
     * <li>type - integer, see {@link jmri.Consist#getConsistType() }</li>
     * <li>id - string with consist Id</li>
     * <li>sizeLimit - the maximum number of locomotives the consist can
     * contain</li>
     * <li>engines - array listing every locomotive in the consist. Each entry
     * in the array contains the following attributes:
     * <ul>
     * <li>address - integer address</li>
     * <li>isLongAddress - boolean true if address is long, false if short</li>
     * <li>forward - boolean true if the locomotive running is forward in the
     * consists</li>
     * <li>position - integer locomotive's position in the consist</li>
     * </ul>
     * </ul>
     *
     * @param locale  The locale to throw exceptions in.
     * @param address The address of the consist to get.
     * @return The JSON representation of the consist.
     * @throws JsonException This exception has code 404 if the consist does not
     *                       exist.
     */
public static JsonNode getConsist(Locale locale, DccLocoAddress address) throws JsonException {
    try {
        if (InstanceManager.getDefault(jmri.ConsistManager.class).getConsistList().contains(address)) {
            ObjectNode root = mapper.createObjectNode();
            root.put(TYPE, CONSIST);
            ObjectNode data = root.putObject(DATA);
            Consist consist = InstanceManager.getDefault(jmri.ConsistManager.class).getConsist(address);
            data.put(ADDRESS, consist.getConsistAddress().getNumber());
            data.put(IS_LONG_ADDRESS, consist.getConsistAddress().isLongAddress());
            data.put(TYPE, consist.getConsistType());
            ArrayNode engines = data.putArray(ENGINES);
            for (DccLocoAddress l : consist.getConsistList()) {
                ObjectNode engine = mapper.createObjectNode();
                engine.put(ADDRESS, l.getNumber());
                engine.put(IS_LONG_ADDRESS, l.isLongAddress());
                engine.put(FORWARD, consist.getLocoDirection(l));
                engine.put(POSITION, consist.getPosition(l));
                engines.add(engine);
            }
            data.put(ID, consist.getConsistID());
            data.put(SIZE_LIMIT, consist.sizeLimit());
            return root;
        } else {
            throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", CONSIST, address.toString()));
        }
    } catch (NullPointerException ex) {
        // NOI18N
        throw new JsonException(503, Bundle.getMessage(locale, "ErrorNoConsistManager"));
    }
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Consist(jmri.Consist) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DccLocoAddress(jmri.DccLocoAddress)

Example 83 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project JMRI by JMRI.

the class JsonUtilHttpService method getPanels.

public JsonNode getPanels(Locale locale, String format) {
    ArrayNode root = mapper.createArrayNode();
    // list loaded Panels (ControlPanelEditor, PanelEditor, LayoutEditor, SwitchboardEditor)
    // list ControlPanelEditors
    Editor.getEditors(ControlPanelEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
        root.add(panel);
    });
    // list LayoutEditors and PanelEditors
    Editor.getEditors(PanelEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
        root.add(panel);
    });
    // list SwitchboardEditors
    Editor.getEditors(SwitchboardEditor.class).stream().map((editor) -> this.getPanel(locale, editor, format)).filter((panel) -> (panel != null)).forEach((panel) -> {
        root.add(panel);
    });
    return root;
}
Also used : ControlPanelEditor(jmri.jmrit.display.controlPanelEditor.ControlPanelEditor) Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) ConnectionConfig(jmri.jmrix.ConnectionConfig) URL(jmri.server.json.JSON.URL) ProfileManager(jmri.profile.ProfileManager) DccLocoAddress(jmri.DccLocoAddress) JmriJFrame(jmri.util.JmriJFrame) PANEL(jmri.server.json.JSON.PANEL) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Editor(jmri.jmrit.display.Editor) JsonException(jmri.server.json.JsonException) SWITCHBOARD_PANEL(jmri.server.json.JSON.SWITCHBOARD_PANEL) ArrayList(java.util.ArrayList) TYPE(jmri.server.json.JSON.TYPE) JsonServerPreferences(jmri.jmris.json.JsonServerPreferences) ConnectionConfigManager(jmri.jmrix.ConnectionConfigManager) Locale(java.util.Locale) Profile(jmri.profile.Profile) SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) PanelEditor(jmri.jmrit.display.panelEditor.PanelEditor) JsonNode(com.fasterxml.jackson.databind.JsonNode) NAME(jmri.server.json.JSON.NAME) InstanceManager(jmri.InstanceManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonHttpService(jmri.server.json.JsonHttpService) Metadata(jmri.Metadata) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) WebServerPreferences(jmri.web.server.WebServerPreferences) SystemConnectionMemo(jmri.jmrix.SystemConnectionMemo) CONTROL_PANEL(jmri.server.json.JSON.CONTROL_PANEL) ConnectionNameFromSystemName(jmri.util.ConnectionNameFromSystemName) DATA(jmri.server.json.JSON.DATA) USERNAME(jmri.server.json.JSON.USERNAME) JSON(jmri.server.json.JSON) NodeIdentity(jmri.util.node.NodeIdentity) ZeroConfService(jmri.util.zeroconf.ZeroConfService) LAYOUT_PANEL(jmri.server.json.JSON.LAYOUT_PANEL) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 84 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project JMRI by JMRI.

the class JsonUtil method getRosterGroups.

/**
     *
     * @param locale The locale of the requesting client
     * @return the list of Roster groups
     * @deprecated since 4.3.5
     */
@Deprecated
public static JsonNode getRosterGroups(Locale locale) {
    ArrayNode root = mapper.createArrayNode();
    root.add(getRosterGroup(locale, Roster.ALLENTRIES));
    for (String name : Roster.getDefault().getRosterGroupList()) {
        root.add(getRosterGroup(locale, name));
    }
    return root;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TrainCommon.splitString(jmri.jmrit.operations.trains.TrainCommon.splitString)

Example 85 with ArrayNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode in project JMRI by JMRI.

the class JsonManifest method getLocations.

public ArrayNode getLocations() {
    // get engine and car lists
    List<Engine> engineList = engineManager.getByTrainBlockingList(train);
    List<Car> carList = carManager.getByTrainDestinationList(train);
    ArrayNode locations = this.mapper.createArrayNode();
    List<RouteLocation> route = train.getRoute().getLocationsBySequenceList();
    for (RouteLocation routeLocation : route) {
        String locationName = splitString(routeLocation.getName());
        ObjectNode jsonLocation = this.mapper.createObjectNode();
        ObjectNode jsonCars = this.mapper.createObjectNode();
        jsonLocation.put(JSON.NAME, StringEscapeUtils.escapeHtml4(locationName));
        jsonLocation.put(JSON.ID, routeLocation.getId());
        if (routeLocation != train.getRoute().getDepartsRouteLocation()) {
            jsonLocation.put(JSON.ARRIVAL_TIME, train.getExpectedArrivalTime(routeLocation));
        }
        if (routeLocation == train.getRoute().getDepartsRouteLocation()) {
            jsonLocation.put(JSON.DEPARTURE_TIME, train.getDepartureTime());
        } else if (!routeLocation.getDepartureTime().equals(RouteLocation.NONE)) {
            jsonLocation.put(JSON.DEPARTURE_TIME, routeLocation.getDepartureTime());
        } else {
            jsonLocation.put(JSON.EXPECTED_DEPARTURE, train.getExpectedDepartureTime(routeLocation));
        }
        // add location comment and id
        ObjectNode locationNode = this.mapper.createObjectNode();
        locationNode.put(JSON.COMMENT, StringEscapeUtils.escapeHtml4(routeLocation.getLocation().getComment()));
        locationNode.put(JSON.ID, routeLocation.getLocation().getId());
        jsonLocation.put(JsonOperations.LOCATION, locationNode);
        jsonLocation.put(JSON.COMMENT, StringEscapeUtils.escapeHtml4(routeLocation.getComment()));
        // engine change or helper service?
        if (train.getSecondLegOptions() != Train.NO_CABOOSE_OR_FRED) {
            ArrayNode options = this.mapper.createArrayNode();
            if (routeLocation == train.getSecondLegStartLocation()) {
                if ((train.getSecondLegOptions() & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) {
                    options.add(JSON.ADD_HELPERS);
                } else if ((train.getSecondLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE || (train.getSecondLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
                    options.add(JSON.CHANGE_CABOOSE);
                } else if ((train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
                    options.add(JSON.CHANGE_ENGINES);
                }
            }
            if (routeLocation == train.getSecondLegEndLocation()) {
                options.add(JSON.REMOVE_HELPERS);
            }
            jsonLocation.put(JSON.OPTIONS, options);
        }
        if (train.getThirdLegOptions() != Train.NO_CABOOSE_OR_FRED) {
            ArrayNode options = this.mapper.createArrayNode();
            if (routeLocation == train.getThirdLegStartLocation()) {
                if ((train.getThirdLegOptions() & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) {
                    options.add(JSON.ADD_HELPERS);
                } else if ((train.getThirdLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE || (train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
                    options.add(JSON.CHANGE_CABOOSE);
                } else if ((train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
                    options.add(JSON.CHANGE_ENGINES);
                }
            }
            if (routeLocation == train.getThirdLegEndLocation()) {
                options.add(JSON.ADD_HELPERS);
            }
            jsonLocation.put(JSON.OPTIONS, options);
        }
        ObjectNode engines = this.mapper.createObjectNode();
        engines.put(JSON.ADD, pickupEngines(engineList, routeLocation));
        engines.put(JSON.REMOVE, dropEngines(engineList, routeLocation));
        jsonLocation.put(JSON.ENGINES, engines);
        // block cars by destination
        ArrayNode pickups = this.mapper.createArrayNode();
        for (RouteLocation destination : route) {
            for (Car car : carList) {
                if (car.getRouteLocation() == routeLocation && car.getRouteDestination() == destination) {
                    pickups.add(this.utilities.getCar(car));
                }
            }
        }
        jsonCars.put(JSON.ADD, pickups);
        // car set outs
        ArrayNode setouts = this.mapper.createArrayNode();
        for (Car car : carList) {
            if (car.getRouteDestination() == routeLocation) {
                setouts.add(this.utilities.getCar(car));
            }
        }
        jsonCars.put(JSON.REMOVE, setouts);
        if (routeLocation != train.getRoute().getTerminatesRouteLocation()) {
            jsonLocation.put(JsonOperations.TRACK, this.getTrackComments(routeLocation, carList));
            jsonLocation.put(JSON.DIRECTION, routeLocation.getTrainDirection());
            ObjectNode length = this.mapper.createObjectNode();
            length.put(JSON.LENGTH, train.getTrainLength(routeLocation));
            length.put(JSON.UNIT, Setup.getLengthUnit());
            jsonLocation.put(JSON.LENGTH, length);
            jsonLocation.put(JsonOperations.WEIGHT, train.getTrainWeight(routeLocation));
            int cars = train.getNumberCarsInTrain(routeLocation);
            int emptyCars = train.getNumberEmptyCarsInTrain(routeLocation);
            jsonCars.put(JSON.TOTAL, cars);
            jsonCars.put(JSON.LOADS, cars - emptyCars);
            jsonCars.put(JSON.EMPTIES, emptyCars);
        } else {
            log.debug("Train terminates in {}", locationName);
            jsonLocation.put("TrainTerminatesIn", StringEscapeUtils.escapeHtml4(locationName));
        }
        jsonLocation.put(JsonOperations.CARS, jsonCars);
        locations.add(jsonLocation);
    }
    return locations;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Car(jmri.jmrit.operations.rollingstock.cars.Car) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RouteLocation(jmri.jmrit.operations.routes.RouteLocation) Engine(jmri.jmrit.operations.rollingstock.engines.Engine)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)979 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)542 JsonNode (com.fasterxml.jackson.databind.JsonNode)402 Test (org.junit.Test)140 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)125 ArrayList (java.util.ArrayList)110 IOException (java.io.IOException)93 Map (java.util.Map)74 HashMap (java.util.HashMap)68 List (java.util.List)50 TextNode (com.fasterxml.jackson.databind.node.TextNode)38 Test (org.testng.annotations.Test)35 DefaultSerializerProvider (com.fasterxml.jackson.databind.ser.DefaultSerializerProvider)30 HashSet (java.util.HashSet)30 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)25 Deployment (org.activiti.engine.test.Deployment)23 File (java.io.File)22 InputStream (java.io.InputStream)20 Iterator (java.util.Iterator)20 StringEntity (org.apache.http.entity.StringEntity)20