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

The following examples show how to use org.nd4j.shade.jackson.core.JsonGenerator#writeString() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: StorageLevelSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(StorageLevel storageLevel, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException, JsonProcessingException {
    //This is a little ugly, but Spark doesn't provide many options here...
    String s = null;
    if (storageLevel != null) {
        s = map.get(storageLevel);
    }
    jsonGenerator.writeString(s);
}
 
Example 7
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();
}
 
Example 8
Source File: DataFormatSerializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(DataFormat dataFormat, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeString(dataFormat.toString());
}