Search in sources :

Example 66 with ArrayNode

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

the class JsonNamedBeanHttpService method getNamedBean.

/**
     * Create the JsonNode for a {@link jmri.NamedBean} object.
     *
     * @param bean   the bean to create the node for
     * @param name   the name of the bean; used only if the bean is null
     * @param type   the JSON type of the bean
     * @param locale the locale used for any error messages
     * @return a JSON node
     * @throws JsonException if the bean is null
     */
@Nonnull
protected ObjectNode getNamedBean(NamedBean bean, @Nonnull String name, @Nonnull String type, @Nonnull Locale locale) throws JsonException {
    if (bean == null) {
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", type, name));
    }
    ObjectNode root = mapper.createObjectNode();
    root.put(JSON.TYPE, type);
    ObjectNode data = root.putObject(JSON.DATA);
    data.put(JSON.NAME, bean.getSystemName());
    data.put(JSON.USERNAME, bean.getUserName());
    data.put(JSON.COMMENT, bean.getComment());
    ArrayNode properties = root.putArray(JSON.PROPERTIES);
    bean.getPropertyKeys().stream().forEach((key) -> {
        Object value = bean.getProperty(key);
        if (value != null) {
            properties.add(mapper.createObjectNode().put(key, value.toString()));
        } else {
            properties.add(mapper.createObjectNode().putNull(key));
        }
    });
    return data;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Nonnull(javax.annotation.Nonnull)

Example 67 with ArrayNode

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

the class JsonConsistHttpService 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 JsonNode getConsist(Locale locale, DccLocoAddress address) throws JsonException {
    if (this.manager.getConsistList().contains(address)) {
        ObjectNode root = mapper.createObjectNode();
        root.put(TYPE, CONSIST);
        ObjectNode data = root.putObject(DATA);
        Consist consist = this.manager.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);
        consist.getConsistList().stream().forEach((locomotive) -> {
            ObjectNode engine = mapper.createObjectNode();
            engine.put(ADDRESS, locomotive.getNumber());
            engine.put(IS_LONG_ADDRESS, locomotive.isLongAddress());
            engine.put(FORWARD, consist.getLocoDirection(locomotive));
            engine.put(POSITION, consist.getPosition(locomotive));
            engines.add(engine);
        });
        data.put(ID, consist.getConsistID());
        data.put(SIZE_LIMIT, consist.sizeLimit());
        return root;
    } else {
        // NOI18N
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", CONSIST, address.toString()));
    }
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Consist(jmri.Consist) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 68 with ArrayNode

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

the class JsonOperationsHttpService method getCars.

public ArrayNode getCars(Locale locale) {
    ArrayNode root = mapper.createArrayNode();
    CarManager.instance().getByIdList().forEach((rs) -> {
        root.add(this.utilities.getCar(locale, rs.getId()));
    });
    return root;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 69 with ArrayNode

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

the class JsonOperationsHttpService method getEngines.

public ArrayNode getEngines(Locale locale) {
    ArrayNode root = mapper.createArrayNode();
    EngineManager.instance().getByIdList().forEach((rs) -> {
        root.add(this.utilities.getEngine(locale, rs.getId()));
    });
    return root;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 70 with ArrayNode

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

the class JsonRosterHttpService method getRoster.

public ArrayNode getRoster(@Nonnull Locale locale, @Nonnull JsonNode data) {
    String group = (!data.path(GROUP).isMissingNode()) ? data.path(GROUP).asText() : null;
    if (Roster.ALLENTRIES.equals(group) || Roster.AllEntries(locale).equals(group)) {
        group = null;
    }
    String roadName = (!data.path(ROAD).isMissingNode()) ? data.path(ROAD).asText() : null;
    String roadNumber = (!data.path(NUMBER).isMissingNode()) ? data.path(NUMBER).asText() : null;
    String dccAddress = (!data.path(ADDRESS).isMissingNode()) ? data.path(ADDRESS).asText() : null;
    String mfg = (!data.path(MFG).isMissingNode()) ? data.path(MFG).asText() : null;
    String decoderModel = (!data.path(DECODER_MODEL).isMissingNode()) ? data.path(DECODER_MODEL).asText() : null;
    String decoderFamily = (!data.path(DECODER_FAMILY).isMissingNode()) ? data.path(DECODER_FAMILY).asText() : null;
    String id = (!data.path(NAME).isMissingNode()) ? data.path(NAME).asText() : null;
    ArrayNode root = this.mapper.createArrayNode();
    for (RosterEntry entry : Roster.getDefault().getEntriesMatchingCriteria(roadName, roadNumber, dccAddress, mfg, decoderModel, decoderFamily, id, group)) {
        root.add(getRosterEntry(locale, entry));
    }
    return root;
}
Also used : RosterEntry(jmri.jmrit.roster.RosterEntry) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

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