當前位置: 首頁>>代碼示例>>Java>>正文


Java JsonWriter.close方法代碼示例

本文整理匯總了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);
  }
}
 
開發者ID:alex-ac,項目名稱:LevelEditor,代碼行數:18,代碼來源:EditorWindow.java

示例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;
	}
 
開發者ID:biggis-project,項目名稱:path-optimizer,代碼行數:20,代碼來源:JsonUtils.java

示例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 ());
}
 
開發者ID:coconut2015,項目名稱:cookjson,代碼行數:18,代碼來源:CookJsonProviderTest.java

示例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 ());
}
 
開發者ID:coconut2015,項目名稱:cookjson,代碼行數:22,代碼來源:BuilderTest.java

示例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 ());
}
 
開發者ID:coconut2015,項目名稱:cookjson,代碼行數:23,代碼來源:BuilderTest.java

示例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();
}
 
開發者ID:trajano,項目名稱:openid-connect,代碼行數:27,代碼來源:JsonWebKeySetProvider.java

示例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();
}
 
開發者ID:trajano,項目名稱:openid-connect,代碼行數:18,代碼來源:JsonWebKeyProvider.java

示例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);
}
 
開發者ID:alphagov,項目名稱:pp-db-collector-template,代碼行數:21,代碼來源:StageResultToJsonConverterTest.java

示例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;
}
 
開發者ID:starksm64,項目名稱:RaspberryPiBeaconParser,代碼行數:26,代碼來源:GimbalManagerProxy.java

示例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();
        }
    }
}
 
開發者ID:apache,項目名稱:johnzon,代碼行數:17,代碼來源:JsrMessageBodyWriter.java

示例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());
}
 
開發者ID:apache,項目名稱:johnzon,代碼行數:17,代碼來源:JsonGeneratorImplTest.java

示例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());
}
 
開發者ID:apache,項目名稱:johnzon,代碼行數:20,代碼來源:JsonGeneratorImplTest.java

示例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();
}
 
開發者ID:apache,項目名稱:johnzon,代碼行數:21,代碼來源:ServerReport.java

示例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;
}
 
開發者ID:fabric8-launcher,項目名稱:launcher-backend,代碼行數:15,代碼來源:JsonOperations.java

示例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();
    }
 
開發者ID:readlearncode,項目名稱:JSON-Processing-with-Java-EE,代碼行數:10,代碼來源:ObjectExample6.java


注:本文中的javax.json.JsonWriter.close方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。