本文整理匯總了Java中javax.json.stream.JsonGenerator類的典型用法代碼示例。如果您正苦於以下問題:Java JsonGenerator類的具體用法?Java JsonGenerator怎麽用?Java JsonGenerator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JsonGenerator類屬於javax.json.stream包,在下文中一共展示了JsonGenerator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onCargoInspected
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
public void onCargoInspected(@Observes @CargoInspected Cargo cargo) {
Writer writer = new StringWriter();
try (JsonGenerator generator = Json.createGenerator(writer)) {
generator
.writeStartObject()
.write("trackingId", cargo.getTrackingId().getIdString())
.write("origin", cargo.getOrigin().getName())
.write("destination", cargo.getRouteSpecification().getDestination().getName())
.write("lastKnownLocation", cargo.getDelivery().getLastKnownLocation().getName())
.write("transportStatus", cargo.getDelivery().getTransportStatus().toString())
.writeEnd();
}
String jsonValue = writer.toString();
for (Session session : sessions) {
try {
session.getBasicRemote().sendText(jsonValue);
} catch (IOException ex) {
logger.log(Level.WARNING, "Unable to publish WebSocket message", ex);
}
}
}
示例2: build
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
/**
* Returns a name and a value after the OK
* @return just OK
*/
public static String build(String name, String value) {
if (name == null) {
throw new NullPointerException("name is null");
}
if (value == null) {
throw new NullPointerException("value is null");
}
JsonGeneratorFactory jf = JsonUtil.getJsonGeneratorFactory(JsonUtil.DEFAULT_PRETTY_PRINTING);
StringWriter sw = new StringWriter();
JsonGenerator gen = jf.createGenerator(sw);
gen.writeStartObject()
.write("status", "OK")
.write(name, value)
.writeEnd();
gen.close();
return sw.toString();
}
示例3: 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;
}
}
示例4: jsonStudentList
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
private static String jsonStudentList(List<Student> students) {
StringWriter swriter = new StringWriter();
try (JsonGenerator gen = Json.createGenerator(swriter)) {
gen.writeStartObject();
gen.write("type", "list");
gen.writeStartArray("body");
for (Student student : students) {
gen.writeStartObject();
gen.write("name", student.getName());
gen.write("status", student.getStatus().toString());
gen.writeEnd();
}
gen.writeEnd();
gen.writeEnd();
}
return swriter.toString();
}
示例5: testFile
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
void testFile (String f, int buffType) throws IOException
{
File file = new File (f.replace ('/', File.separatorChar));
StringWriter out1 = new StringWriter ();
TextJsonParser p1 = getJsonParser (file, buffType);
TextJsonGenerator g1 = new TextJsonGenerator (out1);
Utils.convert (p1, g1);
p1.close ();
g1.close ();
StringWriter out2 = new StringWriter ();
JsonProvider provider = new org.glassfish.json.JsonProviderImpl ();
JsonParser p2 = provider.createParser (new FileInputStream (file));
JsonGenerator g2 = new TextJsonGenerator (out2);
Utils.convert (p2, g2);
p2.close ();
g2.close ();
Assert.assertEquals (out2.toString (), out1.toString ());
}
示例6: formatBody
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
private String formatBody(String contentType, String body) {
if (body != null && contentType != null) {
if (contentType.startsWith("application/json")) {
try {
JsonStructure json = Json.createReader(new StringReader(body)).read();
StringWriter w = new StringWriter();
HashMap<String, Object> p = new HashMap<>();
p.put(JsonGenerator.PRETTY_PRINTING, true);
Json.createWriterFactory(p).createWriter(w).write(json);
return w.toString();
} catch (JsonException ex) {
return body;
}
} else {
return body;
}
} else {
return null;
}
}
示例7: testInputStream
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
void testInputStream (String fileName, HashMap<String, Object> config) throws IOException
{
File file = new File (fileName.replace ('/', File.separatorChar));
JsonProvider provider1 = new CookJsonProvider ();
JsonParserFactory f1 = provider1.createParserFactory (config);
StringWriter out1 = new StringWriter ();
JsonParser p1 = f1.createParser (new FileInputStream (file));
JsonGenerator g1 = new TextJsonGenerator (out1);
Utils.convert (p1, g1);
p1.close ();
g1.close ();
StringWriter out2 = new StringWriter ();
JsonProvider provider2 = new org.glassfish.json.JsonProviderImpl ();
JsonParserFactory f2 = provider2.createParserFactory (config);
JsonParser p2 = f2.createParser (new FileInputStream (file));
JsonGenerator g2 = new TextJsonGenerator (out2);
Utils.convert (p2, g2);
p2.close ();
g2.close ();
Assert.assertEquals (out1.toString (), out2.toString ());
}
示例8: testJsonValueJsonHex
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@Test
public void testJsonValueJsonHex () throws IOException
{
File file1 = new File ("../tests/data/binary.bson".replace ('/', File.separatorChar));
File file2 = new File ("../tests/data/binary2.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 (CookJsonProvider.BINARY_FORMAT, CookJsonProvider.BINARY_FORMAT_HEX);
File jsonFile = 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 (jsonFile));
g.write (value);
p.close ();
g.close ();
FileAssert.assertBinaryEquals (file2, jsonFile);
}
示例9: write
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@Override
public JsonGenerator write (JsonValue value)
{
// assert Debug.debug ("WRITE: JsonValue");
if (m_state != GeneratorState.IN_ARRAY)
{
if (m_state == GeneratorState.INITIAL)
{
if (!(value instanceof JsonArray) &&
!(value instanceof JsonObject))
throw new JsonGenerationException (ErrorMessage.invalidContext);
}
else
throw new JsonGenerationException (ErrorMessage.notInArrayContext);
}
return writeValue (value);
}
示例10: toJson
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@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();
}
示例11: write
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@Override
public JsonGenerator write (BigInteger value)
{
// assert Debug.debug ("WRITE: VALUE_NUMBER: (BigInteger)" + value);
if (m_state != GeneratorState.IN_ARRAY)
throw new JsonGenerationException (ErrorMessage.notInArrayContext);
try
{
writeComma ();
w (value.toString ());
}
catch (IOException ex)
{
throw new JsonGenerationException (ex.getMessage (), ex);
}
return this;
}
示例12: toJson
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@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();
}
示例13: whiteboard
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@RequestMapping(value = "whiteboard", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + "; " + CHARSET)
public String whiteboard() {
try (StringWriter stringWriter = new StringWriter(); JsonGenerator jsonGenerator = Json.createGenerator(stringWriter)) {
jsonGenerator.writeStartArray();
service.listItems().stream()
.map(item -> Json.createObjectBuilder()
.add("id", item.getId())
.add("title", item.getTitle())
.add("creationDate", item.getCreationDate())
.build())
.forEach(jsonGenerator::write);
jsonGenerator.writeEnd();
jsonGenerator.close();
return stringWriter.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "[]";
}
示例14: writeNull
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@Override
public JsonGenerator writeNull ()
{
// assert Debug.debug ("WRITE: VALUE_NULL");
if (m_state != GeneratorState.IN_ARRAY)
throw new JsonGenerationException (ErrorMessage.notInArrayContext);
try
{
writeComma ();
w ("null");
}
catch (IOException ex)
{
throw new JsonGenerationException (ex.getMessage (), ex);
}
return this;
}
示例15: writeEnd
import javax.json.stream.JsonGenerator; //導入依賴的package包/類
@Override
public JsonGenerator writeEnd ()
{
if (m_state == GeneratorState.END)
throw new JsonGenerationException (ErrorMessage.invalidContext);
boolean isArray = popState ();
// assert Debug.debug ("WRITE: " + (isArray ? "END_ARRAY" : "END_OBJECT"));
m_first = false;
char ch = isArray ? ']' : '}';
try
{
w (ch);
}
catch (IOException ex)
{
throw new JsonGenerationException (ex.getMessage (), ex);
}
return this;
}