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

The following examples show how to use javax.json.stream.JsonGenerator#write() . 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: SecurityRulesChangeMessage.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("ruleIds");
    for (RuleId ruleId : ruleIds) {
        generator.writeStartObject();
        generator.write("attributeSet", ruleId.getAttributeSet().toString());
        generator.writeStartObject("securityIndexId");
        generator.write("contingencyId", ruleId.getSecurityIndexId().getContingencyId());
        generator.writeStartObject("securityIndexType");
        generator.write("name", ruleId.getSecurityIndexId().getSecurityIndexType().name());
        generator.write("label", ruleId.getSecurityIndexId().getSecurityIndexType().getLabel());
        generator.write("dynamic", ruleId.getSecurityIndexId().getSecurityIndexType().isDynamic());
        generator.writeEnd();
        generator.writeEnd();
        generator.writeEnd();
    }
    generator.writeEnd();
}
 
Example 2
Source File: Index.java    From FHIR with Apache License 2.0 6 votes vote down vote up
public void store(Writer writer) {
    if (version < 1) {
        throw new IllegalStateException("index version was not set");
    }
    if (entries.isEmpty()) {
        throw new IllegalStateException("index contains no entries");
    }
    Collections.sort(entries);
    JsonGenerator generator = GENERATOR_FACTORY.createGenerator(writer);
    generator.writeStartObject();
    generator.write("index-version", version);
    generator.writeStartArray("files");
    for (Entry entry : entries) {
        write(generator, entry);
    }
    generator.writeEnd();
    generator.writeEnd();
    generator.flush();
    generator.close();
}
 
Example 3
Source File: JSONWriter.java    From jcypher with Apache License 2.0 6 votes vote down vote up
private static void writeLiteral(Object val, JsonGenerator generator) {
	if (val instanceof String)
		generator.write(val.toString());
	else if (val instanceof Number) {
		if (val instanceof Long)
			generator.write((Long)val);
		else if (val instanceof Integer)
			generator.write((Integer)val);
		else if (val instanceof Double)
			generator.write((Double)val);
		else if (val instanceof Float)
			generator.write((Float)val);
	} else if (val instanceof Boolean)
		generator.write((Boolean)val);
	else if (val != null) // handle everything else as a string
		generator.write(val.toString());
}
 
Example 4
Source File: CadfReason.java    From FHIR with Apache License 2.0 6 votes vote down vote up
public static void generate(CadfReason obj, JsonGenerator generator)
        throws IOException {
    // If the CADF reason is null, then simply skip it.
    if (obj == null) {
        return;
    }
    if (obj.getReasonType() != null) {
        generator.write("reasonType", obj.getReasonType());
    }

    if (obj.getReasonCode() != null) {
        generator.write("reasonCode", obj.getReasonCode());
    }

    if (obj.getPolicyType() != null) {
        generator.write("policyType", obj.getPolicyType());
    }

    if (obj.getPolicyId() != null) {
        generator.write("policyId", obj.getPolicyId());
    }
}
 
Example 5
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 6
Source File: MavenLicense.java    From sonarqube-licensecheck with Apache License 2.0 6 votes vote down vote up
public static String createString(Collection<MavenLicense> mavenLicenses)
{
    TreeSet<MavenLicense> mavenLicenseSet = new TreeSet<>(mavenLicenses);

    StringWriter jsonString = new StringWriter();
    JsonGenerator generator = Json.createGenerator(jsonString);
    generator.writeStartArray();
    for (MavenLicense mavenLicense : mavenLicenseSet)
    {
        generator.writeStartObject();
        generator.write("regex", mavenLicense.getRegex().pattern());
        generator.write("license", mavenLicense.getLicense());
        generator.writeEnd();
    }
    generator.writeEnd();
    generator.close();

    return jsonString.toString();
}
 
Example 7
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 8
Source File: CadfMeasurement.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public static void generate(CadfMeasurement obj, JsonGenerator generator)
        throws IOException {
    if (obj == null) {
        return;
    }
    generator.writeStartObject();

    if (obj.getResult() != null) {
        Object tmpObj = obj.getResult();
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream objStream = new ObjectOutputStream(baos);) {
            objStream.writeObject(tmpObj);
            generator.write("result",
                    java.util.Base64.getEncoder().encodeToString(baos.toByteArray()));
        }
    }

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

    if (obj.getMetric() != null) {
        generator.writeStartObject("metric");
        CadfMetric.Writer.generate(obj.getMetric(), generator);
        generator.writeEnd();
    }

    if (obj.getCalculatedBy() != null) {
        generator.writeStartObject("calculatedBy");
        CadfResource.Writer.generate(obj.getCalculatedBy(), generator);
        generator.writeEnd();
    }

    if (obj.getCalculatedById() != null) {
        generator.write("calculatedById", obj.getCalculatedById());
    }

    generator.writeEnd();
}
 
Example 9
Source File: JsonReporter.java    From revapi with Apache License 2.0 5 votes vote down vote up
private static void write(JsonGenerator gen, String key, @Nullable String value) {
    if (value == null) {
        gen.writeNull(key);
    } else {
        gen.write(key, value);
    }
}
 
Example 10
Source File: JSONDomainFacade.java    From jcypher with Apache License 2.0 5 votes vote down vote up
private void writeType(DOType typ, JsonGenerator generator) {
	generator.writeStartObject();
	generator.write("name", typ.getName());
	if (typ.getKind() != null)
		generator.write("kind", typ.getKind().name());
	if (typ.getSuperType() != null) {
		generator.writeStartArray("extends");
		generator.write(typ.getSuperType().getName());
		generator.writeEnd();
	}
	List<DOType> ifs = typ.getInterfaces();
	if (!ifs.isEmpty()) {
		if (typ.getKind() == Kind.INTERFACE)
			generator.writeStartArray("extends");
		else
			generator.writeStartArray("implements");
		for (DOType intf : ifs) {
			generator.write(intf.getName());
		}
		generator.writeEnd();
	}
	List<DOField> flds = typ.getDeclaredFields();
	if (!flds.isEmpty()) {
		generator.writeStartArray("fields");
		for (DOField f : flds) {
			writeField(f, generator);
		}
		generator.writeEnd();
	}
	generator.writeEnd();
}
 
Example 11
Source File: JsonErrorReturn.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * Builds the error message
    *
    * @return the error message
    */
   public String build() {

JsonGeneratorFactory jf = JsonUtil
	.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);

StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);

gen.writeStartObject().write("status", "FAIL").write("error_type",
	errorType);

if (errorMessage != null) {
    gen.write("error_message", errorMessage);
} else {
    gen.write("error_message", JsonValue.NULL);
}

if (stackTrace != null) {
    gen.write("stack_trace", stackTrace);
    System.err.println(stackTrace);
}

gen.write("http_status", httpStatus);

gen.writeEnd();
gen.close();

String doc = sw.toString();

if (LOG_JSON_ERROR) {
    System.err.println(doc);
}

return doc;
   }
 
Example 12
Source File: ApiParameters.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public static void generate(ApiParameters obj, JsonGenerator generator)
        throws IOException {
    if (obj.getRequest() != null) {
        generator.write("request", obj.getRequest());
    }

    if (obj.getStatus() != null) {
        generator.write("request_status", obj.getStatus());
    }
}
 
Example 13
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 14
Source File: EventJsonbSerializer.java    From scalable-coffee-shop with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final CoffeeEvent event, final JsonGenerator generator, final SerializationContext ctx) {
    generator.writeStartObject();
    generator.write("class", event.getClass().getCanonicalName());
    ctx.serialize("data", event, generator);
    generator.writeEnd();
    generator.close();
}
 
Example 15
Source File: Index.java    From FHIR with Apache License 2.0 5 votes vote down vote up
private void write(JsonGenerator generator, Entry entry) {
    generator.writeStartObject();
    generator.write("filename", entry.getFileName());
    generator.write("resourceType", entry.getResourceType());
    generator.write("id", entry.getId());
    generator.write("url", entry.getUrl());
    generator.write("version", entry.getVersion());
    if (entry.getKind() != null) {
        generator.write("kind", entry.getKind());
    }
    if (entry.getType() != null) {
        generator.write("type", entry.getType());
    }
    generator.writeEnd();
}
 
Example 16
Source File: JSONWriter.java    From jcypher with Apache License 2.0 4 votes vote down vote up
private static void writeQuery(String queryKey, String cypher,
		WriterContext context, JsonGenerator generator) {
	generator.write(queryKey, cypher);
	writeQueryParams(context, generator);
}
 
Example 17
Source File: Order.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(StatusEnum obj, JsonGenerator generator, SerializationContext ctx) {
    generator.write(obj.value);
}
 
Example 18
Source File: SecurityRulesProgressMessage.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void toJson(JsonGenerator generator) {
    generator.write("workflowId", workflowId);
    generator.write("progress", progress);
}
 
Example 19
Source File: HalLinkJsonbSerializer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(HalLink value, JsonGenerator generator, SerializationContext context) {
    generator.writeStartObject();
    generator.write("href", value.getHref());
    generator.writeEnd();
}
 
Example 20
Source File: JobParameter.java    From FHIR with Apache License 2.0 4 votes vote down vote up
/**
 * @param generator
 * @param parameter     job parameter details.
 * @param withSensitive indicates sensitive information should or should not be included.
 * @throws IOException
 */
public static void generate(JsonGenerator generator, JobParameter parameter, boolean withSensitive)
        throws IOException {
    if (withSensitive) {
        if (parameter.getCosApiKey() != null) {
            generator.write("cos.api.key", parameter.getCosApiKey());
        }
    }

    if (withSensitive) {
        if (parameter.getCosBucketName() != null) {
            generator.write("cos.bucket.name", parameter.getCosBucketName());
        }
    }

    if (parameter.getCosCredentialIbm() != null) {
        generator.write("cos.credential.ibm", parameter.getCosCredentialIbm());
    }

    if (parameter.getCosOperationBucketNameOo() != null) {
        generator.write("cos.operationoutcomes.bucket.name", parameter.getCosOperationBucketNameOo());
    }

    if (withSensitive) {
        if (parameter.getCosEndpointUrl() != null) {
            generator.write("cos.endpointurl", parameter.getCosEndpointUrl());
        }
    }
    if (parameter.getCosLocation() != null) {
        generator.write("cos.location", parameter.getCosLocation());
    }

    if (withSensitive) {
        if (parameter.getCosSrvInstId() != null) {
            generator.write("cos.srvinst.id", parameter.getCosSrvInstId());
        }
    }

    if (parameter.getFhirResourceType() != null) {
        generator.write("fhir.resourcetype", parameter.getFhirResourceType());
    }

    if (parameter.getFhirSearchFromDate() != null) {
        generator.write("fhir.search.fromdate", parameter.getFhirSearchFromDate());
    }

    if (parameter.getFhirTenant() != null) {
        generator.write("fhir.tenant", parameter.getFhirTenant());
    }

    if (parameter.getFhirPatientGroupId() != null) {
        generator.write("fhir.search.patientgroupid", parameter.getFhirPatientGroupId());
    }

    if (withSensitive) {
        if (parameter.getCosBucketPathPrefix() != null) {
            generator.write("cos.bucket.pathprefix", parameter.getCosBucketPathPrefix());
        }
    }

    if (parameter.getFhirDataStoreId() != null) {
        generator.write("fhir.datastoreid", parameter.getFhirDataStoreId());
    }

    if (parameter.getFhirTypeFilters() != null) {
        generator.write("fhir.typeFilters", parameter.getFhirTypeFilters());
    }

    if (parameter.getInputs() != null) {
        generator.write("fhir.dataSourcesInfo", writeToBase64(parameter.getInputs()));
    }

    String type = BulkDataConstants.DataSourceStorageType.HTTPS.value();
    if (parameter.getStorageDetails() != null) {
        type = parameter.getStorageDetails().getType();
    }
    generator.write("import.fhir.storagetype", type);

    generator.writeEnd();
}