Java Code Examples for javax.json.stream.JsonGenerator#writeStartArray()

The following examples show how to use javax.json.stream.JsonGenerator#writeStartArray() . 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: License.java    From sonarqube-licensecheck with Apache License 2.0 6 votes vote down vote up
public static String createString(Collection<License> licenses)
{
    TreeSet<License> licenseSet = new TreeSet<>(licenses);

    StringWriter jsonString = new StringWriter();
    JsonGenerator generator = Json.createGenerator(jsonString);
    generator.writeStartArray();
    for (License license : licenseSet)
    {
        generator.writeStartObject();
        generator.write("name", license.getName());
        generator.write("identifier", license.getIdentifier());
        generator.write("status", license.getStatus());
        generator.writeEnd();
    }
    generator.writeEnd();
    generator.close();

    return jsonString.toString();
}
 
Example 2
Source File: Dependency.java    From sonarqube-licensecheck with Apache License 2.0 6 votes vote down vote up
public static String createString(Collection<Dependency> dependencies)
{
    TreeSet<Dependency> sortedDependencies = new TreeSet<>();
    sortedDependencies.addAll(dependencies);

    StringWriter jsonString = new StringWriter();
    JsonGenerator generator = Json.createGenerator(jsonString);
    generator.writeStartArray();
    for (Dependency dependency : sortedDependencies)
    {
        String license = dependency.getLicense();
        generator.writeStartObject();
        generator.write("name", dependency.getName());
        generator.write("version", dependency.getVersion());
        generator.write("license", license != null ? license : " ");
        generator.writeEnd();
    }
    generator.writeEnd();
    generator.close();
    return jsonString.toString();
}
 
Example 3
Source File: CadfMetric.java    From FHIR with Apache License 2.0 6 votes vote down vote up
public static void generate(CadfMetric obj, JsonGenerator generator)
        throws IOException {
    if (obj.getName() != null) {
        generator.write("name", obj.getName());
    }

    if (obj.getUnit() != null) {
        generator.write("unit", obj.getUnit());
    }

    if (obj.getMetricId() != null) {
        generator.write("metricId", obj.getMetricId());
    }

    //Annotations
    if (obj.getAnnotations() != null) {
        generator.writeStartArray("annotations");
        for (CadfMapItem item : obj.getAnnotations()) {
            CadfMapItem.Writer.generate(item, generator);
        }
        generator.writeEnd();
    }
}
 
Example 4
Source File: JSONWriter.java    From jcypher with Apache License 2.0 6 votes vote down vote up
private static void writeLiteral(String key, Object val,
		JsonGenerator generator) {
	if (val instanceof String)
		generator.write(key, val.toString());
	else if (val instanceof Number) {
		if (val instanceof Long)
			generator.write(key, (Long)val);
		else if (val instanceof Integer)
			generator.write(key, (Integer)val);
		else if (val instanceof Double)
			generator.write(key, (Double)val);
		else if (val instanceof Float)
			generator.write(key, (Float)val);
	} else if (val instanceof Boolean)
		generator.write(key, (Boolean)val);
	else if (val instanceof List<?>) {
		generator.writeStartArray(key);
		for (Object v : (List<?>)val) {
			writeLiteral(v, generator);
		}
		generator.writeEnd();
	} else if (val != null) // handle everything else as a string
		generator.write(key, val.toString());
}
 
Example 5
Source File: InstanceBodyWriterTools.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
private static void writeGeometries(List<Geometry> files, JsonGenerator jg) {
    jg.write("qualities", files.size());

    if (!files.isEmpty()) {
        Geometry geometry = files.get(0);
        jg.write("xMin", geometry.getxMin());
        jg.write("yMin", geometry.getyMin());
        jg.write("zMin", geometry.getzMin());
        jg.write("xMax", geometry.getxMax());
        jg.write("yMax", geometry.getyMax());
        jg.write("zMax", geometry.getzMax());
    }

    jg.writeStartArray("files");

    for (Geometry g : files) {
        jg.writeStartObject();
        jg.write("fullName", "api/files/" + g.getFullName());
        jg.writeEnd();
    }
    jg.writeEnd();
}
 
Example 6
Source File: NovaMinecraftPreloader.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static JsonGenerator generateSoundJSON(FolderResourcePack pack, JsonGenerator json) {
	for (String domain : (Set<String>) pack.getResourceDomains()) {
		//Load all sounds in the assets/domain/sounds/*
		File folder = getFileForResourcePack(pack, "assets/" + domain + "/sounds/");
		if (folder.exists()) {
			for (File listedFile : folder.listFiles()) {
				String soundName = listedFile.getName().replaceFirst("\\.[^\\.]+$", "");
				json.writeStartObject(soundName);
				json.write("category", "ambient");
				json.writeStartArray("sounds");
				if (listedFile.isFile()) {
					json.write(soundName);
				} else if (listedFile.isDirectory()) {
					for (File soundItemFile : listedFile.listFiles())
						json.write(soundName + "/" + soundItemFile.getName().replaceFirst("\\.[^\\.]+$", ""));
				}
				json.writeEnd().writeEnd();
			}
		}
	}

	return json;
}
 
Example 7
Source File: JSONConverter.java    From jcypher with Apache License 2.0 6 votes vote down vote up
private void writeQuery(RecordedQuery query, JsonGenerator generator) {
	generator.write(GENERIC, query.isGeneric());
	if (query.getAugmentations() != null) {
		generator.writeStartArray(AUGMENTATIONS);
		Iterator<Entry<String, String>> it = query.getAugmentations().entrySet().iterator();
		while(it.hasNext()) {
			Entry<String, String> entry = it.next();
			generator.writeStartObject();
			generator.write(KEY, entry.getKey());
			generator.write(VALUE, entry.getValue());
			generator.writeEnd();
		}
		generator.writeEnd();
	}
	generator.writeStartArray(STATEMENTS);
	for(Statement s : query.getStatements()) {
		writeStatement(s, generator);
	}
	generator.writeEnd();
}
 
Example 8
Source File: SamplesSynthesisMessage.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void toJson(JsonGenerator generator) {
    generator.write("workflowId", workflowId);
    generator.writeStartArray("samplesSynthesis");
    for (SampleSynthesis sample : samples) {
        generator.writeStartObject()
                    .write("id", sample.getId())
                    .writeStartObject("lastTaskEvent")
                        .write("type", sample.getLastTaskEvent().getTaskType().name())
                        .write("status", sample.getLastTaskEvent().getTaskStatus().name());
        if (sample.getLastTaskEvent().getFailureReason() != null) {
            generator.write("failureReason", sample.getLastTaskEvent().getFailureReason());
        }
        generator.writeEnd()
                .writeEnd();
    }
    generator.writeEnd();
}
 
Example 9
Source File: VirtualInstanceCollectionMessageBodyWriter.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void writeTo(VirtualInstanceCollection virtualInstanceCollection, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws UnsupportedEncodingException {
    String charSet = "UTF-8";
    JsonGenerator jg = Json.createGenerator(new OutputStreamWriter(entityStream, charSet));
    jg.writeStartArray();

    Matrix4d gM = new Matrix4d();
    gM.setIdentity();

    PartLink virtualRootPartLink = getVirtualRootPartLink(virtualInstanceCollection);
    List<PartLink> path = new ArrayList<>();
    path.add(virtualRootPartLink);
    InstanceBodyWriterTools.generateInstanceStreamWithGlobalMatrix(productService, path, gM, virtualInstanceCollection, new ArrayList<>(), jg);
    jg.writeEnd();
    jg.flush();
}
 
Example 10
Source File: BusyCoresSeriesMessage.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void toJson(JsonGenerator generator) {
    generator.writeStartObject("busyCoresSeries");
    generator.write("availableCores", busyCoresSeries.getAvailableCores());
    generator.writeStartArray("values");
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm:ss");
    for (BusyCoresSeries.Value value : busyCoresSeries.getValues()) {
        generator.writeStartObject()
                .write("date", value.getDate().toString(fmt))
                .write("busyCores", value.getBusyCores())
                .writeEnd();
    }
    generator.writeEnd()
            .writeEnd();
}
 
Example 11
Source File: EmployeeJSONGenerator.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	OutputStream fos = new FileOutputStream("emp_stream.txt");
	JsonGenerator jsonGenerator = Json.createGenerator(fos);
	/**
	 * We can get JsonGenerator from Factory class also
	 * JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
	 * jsonGenerator = factory.createGenerator(fos);
	 */
	
	Employee emp = EmployeeJSONWriter.createEmployee();
	jsonGenerator.writeStartObject(); // {
	jsonGenerator.write("id", emp.getId()); // "id":123
	jsonGenerator.write("name", emp.getName());
	jsonGenerator.write("role", emp.getRole());
	jsonGenerator.write("permanent", emp.isPermanent());
	
	jsonGenerator.writeStartObject("address") //start of address object
		.write("street", emp.getAddress().getStreet())
		.write("city",emp.getAddress().getCity())
		.write("zipcode",emp.getAddress().getZipcode())
		.writeEnd(); //end of address object
	
	jsonGenerator.writeStartArray("phoneNumbers"); //start of phone num array
	for(long num : emp.getPhoneNumbers()){
		jsonGenerator.write(num);
	}
	jsonGenerator.writeEnd(); // end of phone num array
	jsonGenerator.writeEnd(); // }
	
	jsonGenerator.close();
	
}
 
Example 12
Source File: InstanceBodyWriterTools.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
private static void writeAttributes(List<InstanceAttributeDTO> attributes, JsonGenerator jg) {
    jg.writeStartArray("attributes");
    for (InstanceAttributeDTO a : attributes) {
        jg.writeStartObject();
        jg.write("name", a.getName());
        jg.write("type", a.getType().toString());
        jg.write("value", a.getValue());
        jg.writeEnd();
    }
    jg.writeEnd();
}
 
Example 13
Source File: InstanceBodyWriterTools.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
private static void writeMatrix(Matrix4d matrix, JsonGenerator jg) {
    jg.writeStartArray("matrix");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            jg.write(matrix.getElement(i, j));
        }
    }
    jg.writeEnd();
}
 
Example 14
Source File: InstanceCollectionMessageBodyWriter.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void writeTo(InstanceCollection instanceCollection, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws UnsupportedEncodingException {
    String charSet = "UTF-8";
    JsonGenerator jg = Json.createGenerator(new OutputStreamWriter(entityStream, charSet));
    jg.writeStartArray();

    Matrix4d gM = new Matrix4d();
    gM.setIdentity();
    InstanceBodyWriterTools.generateInstanceStreamWithGlobalMatrix(productService, null, gM, instanceCollection, new ArrayList<>(), jg);
    jg.writeEnd();
    jg.flush();
}
 
Example 15
Source File: WorkflowListMessage.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void toJson(JsonGenerator generator) {
    generator.writeStartArray("workflowStatuses");
    for (OfflineWorkflowStatus status : offlineWorkflowStatuses) {
        generator.writeStartObject();
        new WorkflowStatusMessage(status).toJson(generator);
        generator.writeEnd();
    }
    generator.writeEnd();
}
 
Example 16
Source File: JSONConverter.java    From jcypher with Apache License 2.0 5 votes vote down vote up
private void writeLiteral(Object val, JsonGenerator generator) {
	boolean isParam = val instanceof iot.jcypher.domainquery.ast.Parameter;
	if (isParam) {
		generator.write(TYPE_KEY, PARAM);
		generator.write(PARAM_NAME, ((iot.jcypher.domainquery.ast.Parameter)val).getName());
		val = ((iot.jcypher.domainquery.ast.Parameter)val).getValue();
	} else
		generator.write(TYPE_KEY, LITERAL);
	if (val != null) {
		boolean isColl = Collection.class.isAssignableFrom(val.getClass());
		boolean isArray = val.getClass().isArray();
		if (isArray) {
			StringBuilder sb = new StringBuilder();
			sb.append(ARRAY_PREF);
			sb.append(val.getClass().getComponentType().getName());
			sb.append(ARRAY_POST);
			generator.write(LITERAL_TYPE, sb.toString());
		} else
			generator.write(LITERAL_TYPE, val.getClass().getName());
		if (isArray || isColl) {
			generator.writeStartArray(LITERAL_VALUE);
			if (isColl) {
				Iterator<?> it = ((Collection<?>)val).iterator();
				while(it.hasNext()) {
					generator.writeStartObject();
					writeLiteral(it.next(), generator);
					generator.writeEnd();
				}
			} else { // an array
				for (int i = 0; i < Array.getLength(val); i++) {
					generator.writeStartObject();
					writeLiteral(Array.get(val, i), generator);
					generator.writeEnd();
				}
			}
			generator.writeEnd();
		} else
			generator.write(LITERAL_VALUE, val.toString());
	}
}
 
Example 17
Source File: JSONDomainFacade.java    From jcypher with Apache License 2.0 5 votes vote down vote up
private void writeModel(DomainModel model, JsonGenerator generator) {
	List<DOType> doTypes = model.getDOTypes();
	generator.writeStartArray("types");
	for (DOType typ : doTypes) {
		writeType(typ, generator);
	}
	generator.writeEnd();
}
 
Example 18
Source File: HalCollectionWrapperJsonbSerializer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void writeEmbedded(HalCollectionWrapper wrapper, JsonGenerator generator, SerializationContext context) {
    generator.writeKey("_embedded");
    generator.writeStartObject();
    generator.writeKey(wrapper.getCollectionName());
    generator.writeStartArray();
    for (Object entity : wrapper.getCollection()) {
        context.serialize(new HalEntityWrapper(entity), generator);
    }
    generator.writeEnd();
    generator.writeEnd();
}
 
Example 19
Source File: CadfGeolocation.java    From FHIR with Apache License 2.0 4 votes vote down vote up
public static void generate(CadfGeolocation obj, JsonGenerator generator) throws IOException {

            if (obj.getAccuracy() != null) {
                generator.write("accuracy", obj.getAccuracy());
            }

            if (obj.getElevation() != null) {
                generator.write("elevation", obj.getElevation());
            }

            if (obj.getCity() != null) {
                generator.write("city", obj.getCity());
            }

            if (obj.getState() != null) {
                generator.write("state", obj.getState());
            }

            if (obj.getId() != null) {
                generator.write("id", obj.getId());
            }

            if (obj.getLatitude() != null) {
                generator.write("latitude", obj.getLatitude());
            }

            if (obj.getLongitude() != null) {
                generator.write("longitude", obj.getLongitude());
            }

            if (obj.getRegionICANN() != null) {
                generator.write("region", obj.getRegionICANN());
            }

            //Annotations
            if (obj.getAnnotations() != null) {
                generator.writeStartArray("annotations");
                for (CadfMapItem item : obj.getAnnotations()) {
                    CadfMapItem.Writer.generate(item, generator);
                }
                generator.writeEnd();
            }
        }
 
Example 20
Source File: Data.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void writeData(JsonGenerator writer, Data data, String key) {
	boolean isCollection;
	if (data.containsKey("isCollection") && Boolean.TRUE.equals(data.get("isCollection"))) {
		isCollection = true;
		if (key == null)
			writer.writeStartArray();
		else
			writer.writeStartArray(key);
	} else {
		isCollection = false;
		if (key == null)
			writer.writeStartObject();
		else
			writer.writeStartObject(key);
	}

	if (!isCollection)
		data.forEach((k, v) -> {
			if (v instanceof Data) {
				writeData(writer, (Data) v, k);
			} else if (v instanceof Number) {
				if (v instanceof Byte) {
					writer.write(k, (Byte) v);
				} else if (v instanceof Short) {
					writer.write(k, (Short) v);
				} else if (v instanceof Integer) {
					writer.write(k, (Integer) v);
				} else if (v instanceof Long) {
					writer.write(k, (Long) v);
				} else if (v instanceof Float) {
					writer.write(k, (Float) v);
				} else if (v instanceof Double) {
					writer.write(k, (Double) v);
				} else if (v instanceof BigInteger) {
					writer.write(k, (BigInteger) v);
				} else if (v instanceof BigDecimal) {
					writer.write(k, (BigDecimal) v);
				}
			} else {
				if (v instanceof Boolean) {
					writer.write(k, (Boolean) v);
				} else if (v instanceof Character) {
					writer.write(k, (Character) v);
				} else if (v instanceof String) {
					writer.write(k, (String) v);
				} else if (v instanceof JsonValue) {
					writer.write(k, (JsonValue) v);
				}
			}
		});
	else
		LongStream.range(0, data.size()).mapToObj(l -> data.get(Long.toUnsignedString(l))).forEachOrdered(v -> {
			if (v instanceof Data) {
				writeData(writer, (Data) v, null);
			} else if (v instanceof Number) {
				if (v instanceof Byte) {
					writer.write((Byte) v);
				} else if (v instanceof Short) {
					writer.write((Short) v);
				} else if (v instanceof Integer) {
					writer.write((Integer) v);
				} else if (v instanceof Long) {
					writer.write((Long) v);
				} else if (v instanceof Float) {
					writer.write((Float) v);
				} else if (v instanceof Double) {
					writer.write((Double) v);
				} else if (v instanceof BigInteger) {
					writer.write((BigInteger) v);
				} else if (v instanceof BigDecimal) {
					writer.write((BigDecimal) v);
				}
			} else {
				if (v instanceof Boolean) {
					writer.write((Boolean) v);
				} else if (v instanceof Character) {
					writer.write((Character) v);
				} else if (v instanceof String) {
					writer.write((String) v);
				} else if (v instanceof JsonValue) {
					writer.write((JsonValue) v);
				}
			}
		});

	writer.writeEnd();
}