本文整理匯總了Java中javax.json.JsonWriter.close方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonWriter.close方法的具體用法?Java JsonWriter.close怎麽用?Java JsonWriter.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.json.JsonWriter
的用法示例。
在下文中一共展示了JsonWriter.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doSaveProject
import javax.json.JsonWriter; //導入方法依賴的package包/類
private void doSaveProject() {
try {
OutputStream fileStream = new FileOutputStream(projectFile);
OutputStream bufferedStream = new BufferedOutputStream(fileStream);
StringWriter stringWriter = new StringWriter();
Map<String, Boolean> config = new HashMap<>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriter writer = Json.createWriterFactory(config).createWriter(
stringWriter);
writer.writeObject(project.toJSON().build());
writer.close();
bufferedStream.write(stringWriter.toString().getBytes());
bufferedStream.close();
} catch (Exception ex) {
Logger.getLogger(EditorWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例2: toString
import javax.json.JsonWriter; //導入方法依賴的package包/類
public static String toString(final JsonStructure json) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final JsonWriter jsonWriter = getPrettyJsonWriterFactory()
.createWriter(outputStream, Charset.forName("UTF-8"));
jsonWriter.write(json);
jsonWriter.close();
String jsonString;
try {
jsonString = new String(outputStream.toByteArray(), "UTF8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return jsonString;
}
示例3: test2
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Test
public void test2 () throws IOException
{
CookJsonProvider provider = new CookJsonProvider ();
File srcFile = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
JsonReader r = provider.createReader (new InputStreamReader (new FileInputStream (srcFile), BOM.utf8));
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
JsonWriter w = provider.createWriter (bos);
w.write (r.read ());
r.close ();
w.close ();
Assert.assertEquals (Utils.getString (srcFile).length (), new String (bos.toByteArray (), BOM.utf8).length ());
}
示例4: testObject
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Test
public void testObject ()
{
HashMap<String, Object> config = new HashMap<String, Object> ();
CookJsonProvider provider = new CookJsonProvider ();
JsonBuilderFactory f = provider.createBuilderFactory (config);
JsonObject model = f.createObjectBuilder ()
.add ("object", f.createObjectBuilder ())
.add ("array", f.createArrayBuilder ())
.add ("double", 1234.5)
.add ("number", new CookJsonInt (1234))
.build ();
StringWriter sw = new StringWriter ();
JsonWriter writer = provider.createWriter (sw);
writer.write (model);
writer.close ();
Assert.assertEquals ("{\"object\":{},\"array\":[],\"double\":1234.5,\"number\":1234}".length (), sw.toString ().length ());
}
示例5: testArray
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Test
public void testArray ()
{
HashMap<String, Object> config = new HashMap<String, Object> ();
CookJsonProvider provider = new CookJsonProvider ();
JsonBuilderFactory f = provider.createBuilderFactory (config);
JsonArray model = f.createArrayBuilder ()
.add (12345678901234L)
.add (1234.5)
.add ("quick brown fox")
.add (new BigInteger ("123456789012345678901234567890"))
.add (f.createArrayBuilder ())
.build ();
StringWriter sw = new StringWriter ();
JsonWriter writer = provider.createWriter (sw);
writer.write (model);
writer.close ();
Assert.assertEquals ("[12345678901234,1234.5,\"quick brown fox\",123456789012345678901234567890,[]]", sw.toString ());
}
示例6: writeTo
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Override
public void writeTo(final JsonWebKeySet jwks,
final Class<?> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream os) throws IOException,
WebApplicationException {
final MessageBodyWriter<JsonWebKey> writer = providers.getMessageBodyWriter(JsonWebKey.class, JsonWebKey.class, annotations, mediaType);
final JsonArrayBuilder keysArray = Json.createArrayBuilder();
for (final JsonWebKey key : jwks.getKeys()) {
final ByteArrayOutputStream keyStream = new ByteArrayOutputStream();
writer.writeTo(key, JsonWebKey.class, null, annotations, mediaType, null, keyStream);
keyStream.close();
keysArray.add(Json.createReader(new ByteArrayInputStream(keyStream.toByteArray()))
.readObject());
}
final JsonObject jwksObject = Json.createObjectBuilder()
.add("keys", keysArray)
.build();
final JsonWriter w = Json.createWriter(os);
w.write(jwksObject);
w.close();
}
示例7: writeTo
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Override
public void writeTo(final JsonWebKey jwks,
final Class<?> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream os) throws IOException,
WebApplicationException {
JsonObjectBuilder keyBuilder = Json.createObjectBuilder();
jwks.buildJsonObject(keyBuilder);
JsonWriter w = Json.createWriter(os);
w.write(keyBuilder.build());
w.close();
}
示例8: publish_shouldConvertStageResultsIntoSchemaCompliantJson
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Test
public void publish_shouldConvertStageResultsIntoSchemaCompliantJson() throws IOException {
SchemaLookup schemaLookup = new ClasspathSchemaLookup();
ObjectMapper mapper = new ObjectMapper();
JsonFactory jsonFactory = new JsonFactory(mapper);
SchemaPassThroughCache cache = new SchemaPassThroughCache(jsonFactory);
JsonSchema schema = cache.getSchema(schemaLookup.getSchemaURL("sample_data_schema.json"));
StageResultToJsonConverter convertor = new StageResultToJsonConverter();
StageResult result = new StageResult(new LocalDate(), Period.day, "DIGITAL", 15);
JsonObject jsonResult = convertor.convert(result);
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(writer);
jsonWriter.writeObject(jsonResult);
jsonWriter.close();
List<ErrorMessage> errorMessages = schema.validate(mapper.readTree(writer.toString()));
assertEquals(Collections.<ErrorMessage>emptyList(), errorMessages);
}
示例9: setBeaconTags
import javax.json.JsonWriter; //導入方法依賴的package包/類
@POST
@Path("/api/beacons/{factory_id}/tags")
@Override
public String setBeaconTags(@PathParam("factory_id") String factoryID, @FormParam("tags") String[] tags) {
Client client = ClientBuilder.newClient();
client.register(TrafficLogger.class);
client.register(new AuthHeadersRequestFilter(token));
String url = String.format("https://manager.gimbal.com/api/beacons/%s/tags", factoryID);
WebTarget target = client.target(url);
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;
JsonArrayBuilder tagsBuilder = Json.createArrayBuilder();
for(String tag : tags)
tagsBuilder.add(tag);
JsonObjectBuilder tagsObj = Json.createObjectBuilder();
tagsObj.add("tags", tagsBuilder.build());
StringWriter sw = new StringWriter();
JsonWriter json = Json.createWriter(sw);
json.writeObject(tagsObj.build());
json.close();
Entity<String> jsonEntity = Entity.json(sw.toString());
String response = rtarget.request(MediaType.APPLICATION_JSON_TYPE).post(jsonEntity, String.class);
return response;
}
示例10: writeTo
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Override
public void writeTo(final JsonStructure jsonStructure,
final Class<?> aClass, final Type type,
final Annotation[] annotations, final MediaType mediaType,
final MultivaluedMap<String, Object> stringObjectMultivaluedMap,
final OutputStream outputStream) throws IOException, WebApplicationException {
JsonWriter writer = null;
try {
writer = factory.createWriter(close ? outputStream : noClose(outputStream));
writer.write(jsonStructure);
} finally {
if (writer != null) {
writer.close();
}
}
}
示例11: prettySimpleStructure
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Test
public void prettySimpleStructure() {
final JsonWriterFactory writerFactory = Json.createWriterFactory(new HashMap<String, Object>() {
{
put(JsonGenerator.PRETTY_PRINTING, true);
}
});
StringWriter buffer = new StringWriter();
final JsonWriter writer = writerFactory.createWriter(buffer);
writer.write(Json.createObjectBuilder().add("firstName", "John").build());
writer.close();
assertEquals("{\n" + " \"firstName\":\"John\"\n" + "}", buffer.toString());
}
示例12: prettySimpleWriter
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Test
public void prettySimpleWriter() {
final JsonWriterFactory writerFactory = Json.createWriterFactory(new HashMap<String, Object>() {
{
put(JsonGenerator.PRETTY_PRINTING, true);
}
});
StringWriter buffer = new StringWriter();
final JsonReader reader = Json.createReader(new ByteArrayInputStream("{\"firstName\":\"John\"}".getBytes()));
final JsonWriter writer = writerFactory.createWriter(buffer);
writer.write(reader.read());
writer.close();
reader.close();
assertEquals("{\n" + " \"firstName\":\"John\"\n" + "}", buffer.toString());
}
示例13: pmessage
import javax.json.JsonWriter; //導入方法依賴的package包/類
@GET
@Path("jsr")
@Produces(MediaType.APPLICATION_JSON)
public String pmessage() {
try {
if (!JsrServerEndpointImpl.SEMAPHORE.tryAcquire(1, TimeUnit.MINUTES)) {
throw new IllegalStateException("acquire failed");
}
} catch (final InterruptedException e) {
Thread.interrupted();
return null;
}
// don't setup (+dependency) for just this method the jsr jaxrs provider
final StringWriter output = new StringWriter();
final JsonWriter writer = Json.createWriter(output);
writer.write(JsrServerEndpointImpl.MESSAGES.iterator().next());
writer.close();
return output.toString();
}
示例14: unwrapJsonObjects
import javax.json.JsonWriter; //導入方法依賴的package包/類
public static Object unwrapJsonObjects(Object entity) {
if (entity instanceof JsonObjectBuilder) {
JsonObjectBuilder jsonObjectBuilder = (JsonObjectBuilder) entity;
entity = jsonObjectBuilder.build();
}
if (entity instanceof JsonStructure) {
StringWriter buffer = new StringWriter();
JsonWriter writer = Json.createWriter(buffer);
writer.write((JsonStructure) entity);
writer.close();
return buffer.toString();
}
return entity;
}
示例15: writeJsonStructure
import javax.json.JsonWriter; //導入方法依賴的package包/類
public String writeJsonStructure() {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(stringWriter);
jsonWriter.writeObject(new ObjectExample3().buildJsonDocument());
jsonWriter.close();
return stringWriter.toString();
}