Java Code Examples for org.nd4j.shade.jackson.databind.JsonNode#fieldNames()

The following examples show how to use org.nd4j.shade.jackson.databind.JsonNode#fieldNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
public Data deserialize(JsonParser jp, JsonNode n) {
    JData d = new JData();

    Iterator<String> names = n.fieldNames();
    while (names.hasNext()) {
        String s = names.next();
        JsonNode n2 = n.get(s);

        if (Data.RESERVED_KEY_METADATA.equalsIgnoreCase(s)) {
            Data meta = deserialize(jp, n2);
            d.setMetaData(meta);
        } else {
            if (n2.isTextual()) {                     //String
                String str = n2.textValue();
                d.put(s, str);
            } else if (n2.isDouble()) {               //Double
                double dVal = n2.doubleValue();
                d.put(s, dVal);
            } else if (n2.isInt() || n2.isLong()) {   //Long
                long lVal = n2.longValue();
                d.put(s, lVal);
            } else if (n2.isBoolean()) {              //Boolean
                boolean b = n2.booleanValue();
                d.put(s, b);
            } else if (n2.isArray()){
                Pair<List<Object>, ValueType> p = deserializeList(jp, n2);
                d.putList(s, p.getFirst(), p.getSecond());
            } else if (n2.isObject()) {
                //Could be: Bytes, image, NDArray, BoundingBox, Point or Data
                if (n2.has(Data.RESERVED_KEY_BYTES_BASE64) || n2.has(Data.RESERVED_KEY_BYTES_ARRAY)) {
                    //byte[] stored in base64 or byte[] as JSON array
                    byte[] bytes = deserializeBytes(n2);
                    d.put(s, bytes);
                } else if (n2.has(Data.RESERVED_KEY_NDARRAY_TYPE)) {
                    //NDArray
                    d.put(s, deserializeNDArray(n2));
                } else if (n2.has(Data.RESERVED_KEY_IMAGE_DATA)) {
                    //Image
                    d.put(s, deserializeImage(n2));
                } else if(n2.has(Data.RESERVED_KEY_BB_CY) || n2.has(Data.RESERVED_KEY_BB_X1)){
                    d.put(s, deserializeBB(n2));
                } else if(n2.has(Data.RESERVED_KEY_POINT_COORDS)){
                    d.put(s, deserializePoint(n2));
                } else {
                    //Must be data
                    Data dInner = deserialize(jp, n2);
                    d.put(s, dInner);
                }
            } else {
                throw new UnsupportedOperationException("Type not yet implemented");
            }
        }
    }

    return d;
}
 
Example 2
Source File: DataAnalysis.java    From DataVec with Apache License 2.0 4 votes vote down vote up
private static DataAnalysis fromMapper(ObjectMapper om, String json) {

        List<ColumnMetaData> meta = new ArrayList<>();
        List<ColumnAnalysis> analysis = new ArrayList<>();
        try {
            JsonNode node = om.readTree(json);
            Iterator<String> fieldNames = node.fieldNames();
            boolean hasDataAnalysis = false;
            while (fieldNames.hasNext()) {
                if ("DataAnalysis".equals(fieldNames.next())) {
                    hasDataAnalysis = true;
                    break;
                }
            }
            if (!hasDataAnalysis) {
                throw new RuntimeException();
            }

            ArrayNode arrayNode = (ArrayNode) node.get("DataAnalysis");
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode analysisNode = arrayNode.get(i);
                String name = analysisNode.get(COL_NAME).asText();
                int idx = analysisNode.get(COL_IDX).asInt();
                ColumnType type = ColumnType.valueOf(analysisNode.get(COL_TYPE).asText());

                JsonNode daNode = analysisNode.get(ANALYSIS);
                ColumnAnalysis dataAnalysis = om.treeToValue(daNode, ColumnAnalysis.class);

                if (type == ColumnType.Categorical) {
                    ArrayNode an = (ArrayNode) analysisNode.get(CATEGORICAL_STATE_NAMES);
                    List<String> stateNames = new ArrayList<>(an.size());
                    Iterator<JsonNode> iter = an.elements();
                    while (iter.hasNext()) {
                        stateNames.add(iter.next().asText());
                    }
                    meta.add(new CategoricalMetaData(name, stateNames));
                } else {
                    meta.add(type.newColumnMetaData(name));
                }

                analysis.add(dataAnalysis);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Schema schema = new Schema(meta);
        return new DataAnalysis(schema, analysis);
    }
 
Example 3
Source File: DataAnalysis.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private static DataAnalysis fromMapper(ObjectMapper om, String json) {

        List<ColumnMetaData> meta = new ArrayList<>();
        List<ColumnAnalysis> analysis = new ArrayList<>();
        try {
            JsonNode node = om.readTree(json);
            Iterator<String> fieldNames = node.fieldNames();
            boolean hasDataAnalysis = false;
            while (fieldNames.hasNext()) {
                if ("DataAnalysis".equals(fieldNames.next())) {
                    hasDataAnalysis = true;
                    break;
                }
            }
            if (!hasDataAnalysis) {
                throw new RuntimeException();
            }

            ArrayNode arrayNode = (ArrayNode) node.get("DataAnalysis");
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode analysisNode = arrayNode.get(i);
                String name = analysisNode.get(COL_NAME).asText();
                int idx = analysisNode.get(COL_IDX).asInt();
                ColumnType type = ColumnType.valueOf(analysisNode.get(COL_TYPE).asText());

                JsonNode daNode = analysisNode.get(ANALYSIS);
                ColumnAnalysis dataAnalysis = om.treeToValue(daNode, ColumnAnalysis.class);

                if (type == ColumnType.Categorical) {
                    ArrayNode an = (ArrayNode) analysisNode.get(CATEGORICAL_STATE_NAMES);
                    List<String> stateNames = new ArrayList<>(an.size());
                    Iterator<JsonNode> iter = an.elements();
                    while (iter.hasNext()) {
                        stateNames.add(iter.next().asText());
                    }
                    meta.add(new CategoricalMetaData(name, stateNames));
                } else {
                    meta.add(type.newColumnMetaData(name));
                }

                analysis.add(dataAnalysis);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Schema schema = new Schema(meta);
        return new DataAnalysis(schema, analysis);
    }