Java Code Examples for org.nd4j.shade.jackson.core.JsonGenerator#writeStartObject()

The following examples show how to use org.nd4j.shade.jackson.core.JsonGenerator#writeStartObject() . 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: ConfusionMatrixSerializer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ConfusionMatrix<Integer> cm, JsonGenerator gen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
    List<Integer> classes = cm.getClasses();
    Map<Integer, Multiset<Integer>> matrix = cm.getMatrix();

    Map<Integer, int[][]> m2 = new LinkedHashMap<>();
    for (Integer i : matrix.keySet()) { //i = Actual class
        Multiset<Integer> ms = matrix.get(i);
        int[][] arr = new int[2][ms.size()];
        int used = 0;
        for (Integer j : ms.elementSet()) {
            int count = ms.count(j);
            arr[0][used] = j; //j = Predicted class
            arr[1][used] = count; //prediction count
            used++;
        }
        m2.put(i, arr);
    }

    gen.writeStartObject();
    gen.writeObjectField("classes", classes);
    gen.writeObjectField("matrix", m2);
    gen.writeEndObject();
}
 
Example 2
Source File: Configuration.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 *  Writes out all the parameters and their properties (final and resource) to
 *  the given {@link Writer}
 *  The format of the output would be
 *  { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2,
 *  key2.isFinal,key2.resource}... ] }
 *  It does not output the parameters of the configuration object which is
 *  loaded from an input stream.
 * @param out the Writer to write to
 * @throws IOException
 */
public static void dumpConfiguration(Configuration conf, Writer out) throws IOException {
    Configuration config = new Configuration(conf, true);
    config.reloadConfiguration();
    JsonFactory dumpFactory = new JsonFactory();
    JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
    dumpGenerator.writeStartObject();
    dumpGenerator.writeFieldName("properties");
    dumpGenerator.writeStartArray();
    dumpGenerator.flush();
    for (Map.Entry<Object, Object> item : config.getProps().entrySet()) {
        dumpGenerator.writeStartObject();
        dumpGenerator.writeStringField("key", (String) item.getKey());
        dumpGenerator.writeStringField("value", config.get((String) item.getKey()));
        dumpGenerator.writeBooleanField("isFinal", config.finalParameters.contains(item.getKey()));
        dumpGenerator.writeStringField("resource", config.updatingResource.get(item.getKey()));
        dumpGenerator.writeEndObject();
    }
    dumpGenerator.writeEndArray();
    dumpGenerator.writeEndObject();
    dumpGenerator.flush();
}
 
Example 3
Source File: DataJsonSerializer.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
private void writePoint(JsonGenerator jg, Point p) throws IOException {
    jg.writeStartObject();
    jg.writeFieldName(Data.RESERVED_KEY_POINT_COORDS);
    jg.writeStartArray(p.dimensions());
    for (int i = 0; i < p.dimensions(); i++) {
        writeDouble(jg, p.get(i));
    }
    jg.writeEndArray();

    if(p.label() != null){
        jg.writeFieldName("label");
        jg.writeString(p.label());
    }
    if(p.probability() != null){
        jg.writeFieldName("probability");
        jg.writeNumber(p.probability());
    }

    jg.writeEndObject();
}
 
Example 4
Source File: Configuration.java    From DataVec with Apache License 2.0 6 votes vote down vote up
/**
 *  Writes out all the parameters and their properties (final and resource) to
 *  the given {@link Writer}
 *  The format of the output would be
 *  { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2,
 *  key2.isFinal,key2.resource}... ] }
 *  It does not output the parameters of the configuration object which is
 *  loaded from an input stream.
 * @param out the Writer to write to
 * @throws IOException
 */
public static void dumpConfiguration(Configuration conf, Writer out) throws IOException {
    Configuration config = new Configuration(conf, true);
    config.reloadConfiguration();
    JsonFactory dumpFactory = new JsonFactory();
    JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
    dumpGenerator.writeStartObject();
    dumpGenerator.writeFieldName("properties");
    dumpGenerator.writeStartArray();
    dumpGenerator.flush();
    for (Map.Entry<Object, Object> item : config.getProps().entrySet()) {
        dumpGenerator.writeStartObject();
        dumpGenerator.writeStringField("key", (String) item.getKey());
        dumpGenerator.writeStringField("value", config.get((String) item.getKey()));
        dumpGenerator.writeBooleanField("isFinal", config.finalParameters.contains(item.getKey()));
        dumpGenerator.writeStringField("resource", config.updatingResource.get(item.getKey()));
        dumpGenerator.writeEndObject();
    }
    dumpGenerator.writeEndArray();
    dumpGenerator.writeEndObject();
    dumpGenerator.flush();
}
 
Example 5
Source File: NDArraySerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    String toBase64 = Nd4jBase64.base64String(indArray);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("array", toBase64);
    jsonGenerator.writeEndObject();
}
 
Example 6
Source File: ROCArraySerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ROC[] rocs, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException, JsonProcessingException {
    jsonGenerator.writeStartArray();
    for (ROC r : rocs) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("@class", ROC.class.getName());
        serializer.serialize(r, jsonGenerator, serializerProvider);
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
}
 
Example 7
Source File: DateTimeFieldTypeSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(DateTimeFieldType dateTimeFieldType, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("fieldType", dateTimeFieldType.getName());
    jsonGenerator.writeEndObject();
}
 
Example 8
Source File: TDigestSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
    try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
        oos.writeObject(td);
        oos.close();
        byte[] bytes = baos.toByteArray();
        Base64 b = new Base64();
        String str = b.encodeAsString(bytes);
        j.writeStartObject();
        j.writeStringField("digest", str);
        j.writeEndObject();
    }
}
 
Example 9
Source File: NDArraySerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    String toBase64 = Nd4jBase64.base64String(indArray);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("array", toBase64);
    jsonGenerator.writeEndObject();
}
 
Example 10
Source File: VectorSerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    if (indArray.isView())
        indArray = indArray.dup(indArray.ordering());
    jsonGenerator.writeStartObject();
    DataBuffer view = indArray.data();
    jsonGenerator.writeArrayFieldStart("dataBuffer");
    for (int i = 0; i < view.length(); i++) {
        jsonGenerator.writeNumber(view.getDouble(i));
    }

    jsonGenerator.writeEndArray();

    jsonGenerator.writeArrayFieldStart("shapeField");
    for (int i = 0; i < indArray.rank(); i++) {
        jsonGenerator.writeNumber(indArray.size(i));
    }
    jsonGenerator.writeEndArray();

    jsonGenerator.writeArrayFieldStart("strideField");
    for (int i = 0; i < indArray.rank(); i++)
        jsonGenerator.writeNumber(indArray.stride(i));
    jsonGenerator.writeEndArray();

    jsonGenerator.writeNumberField("offsetField", indArray.offset());
    jsonGenerator.writeStringField("typeField", indArray instanceof IComplexNDArray ? "complex" : "real");
    jsonGenerator.writeNumberField("rankField", indArray.rank());
    jsonGenerator.writeNumberField("numElements", view.length());
    jsonGenerator.writeStringField("orderingField", String.valueOf(indArray.ordering()));
    jsonGenerator.writeEndObject();
}
 
Example 11
Source File: DateTimeFieldTypeSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(DateTimeFieldType dateTimeFieldType, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("fieldType", dateTimeFieldType.getName());
    jsonGenerator.writeEndObject();
}
 
Example 12
Source File: TDigestSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
    try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
        oos.writeObject(td);
        oos.close();
        byte[] bytes = baos.toByteArray();
        Base64 b = new Base64();
        String str = b.encodeAsString(bytes);
        j.writeStartObject();
        j.writeStringField("digest", str);
        j.writeEndObject();
    }
}
 
Example 13
Source File: DataJsonSerializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public static void writeBB(JsonGenerator jg, BoundingBox bb) throws IOException {
    //We'll keep it in the original format, if possible - but encode it as a X/Y format otherwise
    jg.writeStartObject();
    if(bb instanceof BBoxCHW){
        BBoxCHW b = (BBoxCHW)bb;
        jg.writeFieldName(Data.RESERVED_KEY_BB_CX);
        jg.writeNumber(b.cx());
        jg.writeFieldName(Data.RESERVED_KEY_BB_CY);
        jg.writeNumber(b.cy());
        jg.writeFieldName(Data.RESERVED_KEY_BB_H);
        jg.writeNumber(b.h());
        jg.writeFieldName(Data.RESERVED_KEY_BB_W);
        jg.writeNumber(b.w());
    } else {
        jg.writeFieldName(Data.RESERVED_KEY_BB_X1);
        jg.writeNumber(bb.x1());
        jg.writeFieldName(Data.RESERVED_KEY_BB_X2);
        jg.writeNumber(bb.x2());
        jg.writeFieldName(Data.RESERVED_KEY_BB_Y1);
        jg.writeNumber(bb.y1());
        jg.writeFieldName(Data.RESERVED_KEY_BB_Y2);
        jg.writeNumber(bb.y2());
    }
    if(bb.label() != null){
        jg.writeFieldName("label");
        jg.writeString(bb.label());
    }
    if(bb.probability() != null){
        jg.writeFieldName("probability");
        jg.writeNumber(bb.probability());
    }

    jg.writeEndObject();
}
 
Example 14
Source File: DataJsonSerializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
private void writeNDArray(JsonGenerator jg, NDArray n) throws IOException {
    jg.writeStartObject();

    SerializedNDArray sn = n.getAs(SerializedNDArray.class);
    NDArrayType type = sn.getType();
    long[] shape = sn.getShape();
    jg.writeFieldName(Data.RESERVED_KEY_NDARRAY_TYPE);
    jg.writeString(type.toString());
    jg.writeFieldName(Data.RESERVED_KEY_NDARRAY_SHAPE);
    jg.writeArray(shape, 0, shape.length);

    ByteBuffer bb = sn.getBuffer();
    bb.rewind();
    byte[] array;
    if (bb.hasArray()) {
        array = bb.array();
    } else {
        int size = bb.remaining();
        array = new byte[size];
        for (int i = 0; i < size; i++) {
            array[i] = bb.get(i);
        }
    }

    String base64 = Base64.getEncoder().encodeToString(array);
    jg.writeFieldName(Data.RESERVED_KEY_NDARRAY_DATA_ARRAY);
    jg.writeString(base64);
    jg.writeEndObject();
}
 
Example 15
Source File: DataJsonSerializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
private void writeImage(JsonGenerator jg, Image i) throws IOException {
    Png png = i.getAs(Png.class);
    byte[] imgData = png.getBytes();
    jg.writeStartObject();
    jg.writeFieldName(Data.RESERVED_KEY_IMAGE_FORMAT);
    jg.writeString("PNG");      //TODO No magic constant
    jg.writeFieldName(Data.RESERVED_KEY_IMAGE_DATA);
    String base64 = Base64.getEncoder().encodeToString(imgData);
    jg.writeString(base64);
    jg.writeEndObject();
}
 
Example 16
Source File: DataJsonSerializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
private void writeBytes(JsonGenerator jg, byte[] bytes) throws IOException {
    //TODO add option to do raw bytes array - [0, 1, 2, ...] style
    jg.writeStartObject();
    jg.writeFieldName(Data.RESERVED_KEY_BYTES_BASE64);
    String base64 = Base64.getEncoder().encodeToString(bytes);
    jg.writeString(base64);
    jg.writeEndObject();
}
 
Example 17
Source File: DataJsonSerializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Data data, JsonGenerator jg, SerializerProvider sp) throws IOException {
    //TODO do we serialize in any particular order?

    jg.writeStartObject();

    List<String> l = data.keys();

    for (String s : l) {
        ValueType vt = data.type(s);
        jg.writeFieldName(s);

        switch (vt) {
            case NDARRAY:
                NDArray n = data.getNDArray(s);
                writeNDArray(jg, n);
                break;
            case STRING:
                String str = data.getString(s);
                jg.writeString(str);
                break;
            case BYTES:
                writeBytes(jg, data.getBytes(s));
                break;
            case IMAGE:
                writeImage(jg, data.getImage(s));
                break;
            case DOUBLE:
                writeDouble(jg, data.getDouble(s));
                break;
            case INT64:
                writeLong(jg, data.getLong(s));
                break;
            case BOOLEAN:
                boolean b = data.getBoolean(s);
                jg.writeBoolean(b);
                break;
            case DATA:
                Data d = data.getData(s);
                writeNestedData(jg, d);
                break;
            case LIST:
                /*
                Format:
                "myList" : ["x", "y", "z"]
                 */
                ValueType listVt = data.listType(s);
                List<?> list = data.getList(s, listVt);
                writeList(jg, list, listVt);
                break;
            case BOUNDING_BOX:
                BoundingBox bb = data.getBoundingBox(s);
                writeBB(jg, bb);
                break;
            case POINT:
                Point p = data.getPoint(s);
                writePoint(jg, p);
                break;
            default:
                throw new IllegalStateException("Value type not yet supported/implemented: " + vt);
        }
    }

    if (data.getMetaData() != null) {
        Data md = data.getMetaData();
        jg.writeFieldName(Data.RESERVED_KEY_METADATA);
        writeNestedData(jg, md);
    }

    jg.writeEndObject();
}