本文整理匯總了Java中javax.json.stream.JsonGenerator.write方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonGenerator.write方法的具體用法?Java JsonGenerator.write怎麽用?Java JsonGenerator.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.json.stream.JsonGenerator
的用法示例。
在下文中一共展示了JsonGenerator.write方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: build
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
/**
* Build a Json with name and values rom the passed map
* @param namesAndValues the map of (name, value) to add to the JsonGenerator
* @return
*/
public static String build(Map<String, String> namesAndValues) {
if (namesAndValues == null) {
throw new NullPointerException("namesAndValues is null");
}
JsonGeneratorFactory jf = JsonUtil.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);
StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);
gen.writeStartObject()
.write("status", "OK") ;
for (Map.Entry<String, String> entry : namesAndValues.entrySet()) {
// trace(entry.getKey() + "/" + entry.getValue());
gen.write(entry.getKey(), entry.getValue());
}
gen.writeEnd();
gen.close();
return sw.toString();
}
示例2: statementNotAllowedBuild
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
/**
* Builds a security error message in JSON format for Statements
* @param sqlOrder
* @param errorMessage
* @param doPrettyPrinting
* @return
*/
public static String statementNotAllowedBuild(String sqlOrder, String errorMessage, boolean doPrettyPrinting) {
try {
JsonGeneratorFactory jf = JsonUtil
.getJsonGeneratorFactory(doPrettyPrinting);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonGenerator gen = jf.createGenerator(out);
gen.writeStartObject();
gen.write(Tag.PRODUCT_SECURITY, errorMessage);
gen.write("SQL order", sqlOrder);
gen.writeEnd();
gen.close();
return out.toString("UTF-8");
} catch (Exception e) {
String returnString = Tag.PRODUCT_SECURITY + " " + errorMessage
+ " " + sqlOrder;
return returnString;
}
}
示例3: testJsonValue
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Test
public void testJsonValue () throws IOException
{
File file1 = new File ("../tests/data/complex1.json".replace ('/', File.separatorChar));
// first convert from Json to Bson using stream API
File bsonFile = testFolder.newFile ();
CookJsonParser p = TextJsonConfigHandler.getJsonParser (new FileInputStream (file1));
JsonGenerator g = new BsonGenerator (new FileOutputStream (bsonFile));
Utils.convert (p, g);
p.close ();
g.close ();
// convert from Json to Bson using tree api
File bsonFile2 = testFolder.newFile ();
p = TextJsonConfigHandler.getJsonParser (new FileInputStream (file1));
p.next ();
JsonValue value = p.getValue ();
p.close ();
g = new BsonGenerator (new FileOutputStream (bsonFile2));
g.write (value);
g.close ();
// due to object ordering, we can only compare the length.
Assert.assertEquals (bsonFile.length (), bsonFile2.length ());
}
示例4: createString
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
public static String createString(Collection<ProjectLicense> projectLicenses)
{
TreeSet<ProjectLicense> projectLicenseSet = new TreeSet<>();
projectLicenseSet.addAll(projectLicenses);
StringWriter jsonString = new StringWriter();
JsonGenerator generator = Json.createGenerator(jsonString);
generator.writeStartArray();
for (ProjectLicense projectLicense : projectLicenseSet)
{
generator.writeStartObject();
generator.write("projectKey", projectLicense.getProjectKey());
generator.write("license", projectLicense.getLicense());
generator.write("status", projectLicense.getStatus());
generator.writeEnd();
}
generator.writeEnd();
generator.close();
return jsonString.toString();
}
示例5: createString
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
public static String createString(Collection<MavenLicense> mavenLicenses)
{
TreeSet<MavenLicense> mavenLicenseSet = new TreeSet<>();
mavenLicenseSet.addAll(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();
}
示例6: serialize
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Override
public void serialize(Book book, JsonGenerator generator, SerializationContext ctx) {
generator.writeStartObject();
generator.write("id", "QWE-123-RTS");
generator.write("title", book.getTitle());
generator.write("firstName", book.getAuthor().split(" ")[0]);
generator.write("lastName", book.getAuthor().split(" ")[1]);
generator.writeEnd();
}
示例7: build
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
/**
* 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;
}
示例8: serialize
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Override
public void serialize(final CoffeeEvent event, final JsonGenerator generator, final SerializationContext ctx) {
generator.write("class", event.getClass().getCanonicalName());
generator.writeStartObject("data");
ctx.serialize("data", event, generator);
generator.writeEnd();
}
示例9: testJsonValuePrettyJson
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Test
public void testJsonValuePrettyJson () throws IOException
{
File file1 = new File ("../tests/data/binary.bson".replace ('/', File.separatorChar));
File file2 = new File ("../tests/data/binary.json".replace ('/', File.separatorChar));
CookJsonProvider provider = new CookJsonProvider ();
HashMap<String, Object> bsonConfig = new HashMap<String, Object> ();
bsonConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
bsonConfig.put (CookJsonProvider.ROOT_AS_ARRAY, Boolean.TRUE);
HashMap<String, Object> jsonConfig = new HashMap<String, Object> ();
jsonConfig.put (JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
File jsonFile1 = testFolder.newFile ();
CookJsonParser p = (CookJsonParser) provider.createParserFactory (bsonConfig).createParser (new FileInputStream (file1));
p.next ();
JsonValue value = p.getValue ();
JsonGenerator g = provider.createGeneratorFactory (jsonConfig).createGenerator (new FileOutputStream (jsonFile1));
g.write (value);
p.close ();
g.close ();
File jsonFile2 = testFolder.newFile ();
JsonParser p2 = provider.createParserFactory (jsonConfig).createParser (new FileInputStream (file2));
JsonGenerator g2 = provider.createGeneratorFactory (jsonConfig).createGenerator (new FileOutputStream (jsonFile2));
Utils.convert (p2, g2);
p2.close ();
g2.close ();
FileAssert.assertBinaryEquals (jsonFile2, jsonFile1);
}
示例10: serialize
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Override
protected void serialize(Number obj, JsonGenerator generator, Marshaller marshaller) {
BigDecimal bigDecimalValue = new BigDecimal(String.valueOf(obj));
if (BigNumberUtil.isIEEE754(bigDecimalValue)) {
generator.write(bigDecimalValue);
} else {
generator.write(String.valueOf(obj));
}
}
示例11: toJson
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Override
public void toJson(JsonGenerator generator) {
generator.write("workflowId", workflowId);
generator.writeStartObject("ruleDescription");
//TODO
generator.writeEnd();
}
示例12: writeDate
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
private void writeDate(JsonGenerator jg, String key, Date date) {
if (date != null) {
String formattedDate = getFormattedDate(date);
jg.write(key, formattedDate);
} else {
jg.write(key, JsonValue.NULL);
}
}
示例13: serialize
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Override
public void serialize(Crate obj, JsonGenerator generator, SerializationContext ctx) {
generator.writeStartObject();
generator.write("crateStr", "REPLACED crate str");
ctx.serialize("crateInner", obj.crateInner, generator);
ctx.serialize("crateInnerList", obj.crateInnerList, generator);
generator.write("crateBigDec", new BigDecimal("54321"));
generator.writeEnd();
}
示例14: testJsonValueBson
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
private void testJsonValueBson (String f) throws IOException
{
File file = new File (f.replace ('/', File.separatorChar));
JsonValue value;
BsonParser p = new BsonParser (new FileInputStream (file));
p.setRootAsArray (true);
p.next ();
value = p.getValue ();
p.close ();
StringWriter out1 = new StringWriter ();
JsonGenerator g1 = new TextJsonGenerator (out1);
g1.writeStartArray ();
g1.write (value);
g1.writeEnd ();
g1.close ();
JsonProvider provider = new org.glassfish.json.JsonProviderImpl ();
StringWriter out2 = new StringWriter ();
JsonGenerator g2 = provider.createGenerator (out2);
g2.writeStartArray ();
g2.write (value);
g2.writeEnd ();
g2.close ();
Assert.assertEquals (out1.toString (), out2.toString ());
}
示例15: serializeInternal
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Override
protected void serializeInternal(byte[] obj, JsonGenerator generator, SerializationContext ctx) {
for (byte b : obj) {
generator.write(b);
}
}