本文整理匯總了Java中javax.json.JsonWriter.write方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonWriter.write方法的具體用法?Java JsonWriter.write怎麽用?Java JsonWriter.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.json.JsonWriter
的用法示例。
在下文中一共展示了JsonWriter.write方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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;
}
示例2: 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 ());
}
示例3: 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 ());
}
示例4: 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 ());
}
示例5: serializeAsStream
import javax.json.JsonWriter; //導入方法依賴的package包/類
/**
* Convert a POJO into Serialized JSON form.
*
* @param o the POJO to serialize
* @param s the OutputStream to write to..
* @throws IOException when there are problems creating the JSON.
*/
public static void serializeAsStream(Object o, OutputStream s) throws IOException {
try {
JsonStructure builtJsonObject = findFieldsToSerialize(o).mainObject;
Map<String, Object> config = new HashMap<String, Object>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory writerFactory = Json.createWriterFactory(config);
JsonWriter streamWriter = writerFactory.createWriter(s);
streamWriter.write(builtJsonObject);
} catch (IllegalStateException ise) {
// the reflective attempt to build the object failed.
throw new IOException("Unable to build JSON for Object", ise);
} catch (JsonException e) {
throw new IOException("Unable to build JSON for Object", e);
}
}
示例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: 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();
}
}
}
示例9: 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());
}
示例10: 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());
}
示例11: 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();
}
示例12: writeStructure
import javax.json.JsonWriter; //導入方法依賴的package包/類
private File writeStructure(final File targetDir, final String directory,
final String fileName, JsonStructure structure) throws IOException {
Validate.notNull(fileName);
final File dir = new File(targetDir, directory);
dir.mkdirs();
final File file = new File(dir, fileName);
final FileWriter fileWriter = new FileWriter(file);
try {
final JsonWriter jsonWriter = provider.createWriterFactory(
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING,
Boolean.TRUE)).createWriter(fileWriter);
jsonWriter.write(structure);
} finally {
try {
fileWriter.close();
} catch (IOException ignored) {
}
}
return file;
}
示例13: 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;
}
示例14: toString
import javax.json.JsonWriter; //導入方法依賴的package包/類
@Override
public String toString(JsonStructure value) {
StringWriter w = new StringWriter();
JsonWriter jw = wf.createWriter(w);
jw.write(value);
return w.toString();
}
示例15: prettyPrint
import javax.json.JsonWriter; //導入方法依賴的package包/類
/**
* Returns a pretty-printed JSON string for the given example instance of the given RAML type.
*
* @param type
* RAML type declaration
* @param example
* example instance
* @return pretty-printed JSON string
*/
public String prettyPrint(TypeDeclaration type, ExampleSpec example) {
JsonValue json = toJsonValue(type, example);
Map<String, Boolean> config = new HashMap<>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory jwf = Json.createWriterFactory(config);
StringWriter sw = new StringWriter();
JsonWriter writer = jwf.createWriter(sw);
if (json instanceof JsonStructure) {
writer.write((JsonStructure) json);
return sw.toString();
}
else {
return json.toString();
}
}