Java Code Examples for org.nd4j.shade.jackson.core.JsonGenerator#writeFieldName()
The following examples show how to use
org.nd4j.shade.jackson.core.JsonGenerator#writeFieldName() .
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 |
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: Configuration.java From DataVec with Apache License 2.0 | 6 votes |
/** * 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: Configuration.java From deeplearning4j with Apache License 2.0 | 6 votes |
/** * 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 4
Source File: DataJsonSerializer.java From konduit-serving with Apache License 2.0 | 5 votes |
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 5
Source File: DataJsonSerializer.java From konduit-serving with Apache License 2.0 | 5 votes |
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 6
Source File: DataJsonSerializer.java From konduit-serving with Apache License 2.0 | 5 votes |
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 7
Source File: DataJsonSerializer.java From konduit-serving with Apache License 2.0 | 5 votes |
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 8
Source File: DataJsonSerializer.java From konduit-serving with Apache License 2.0 | 4 votes |
@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(); }