本文整理匯總了Java中javax.json.stream.JsonGenerator.close方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonGenerator.close方法的具體用法?Java JsonGenerator.close怎麽用?Java JsonGenerator.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.json.stream.JsonGenerator
的用法示例。
在下文中一共展示了JsonGenerator.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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();
}
示例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: testFile
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
void testFile (String f1, String f2) throws IOException
{
File file1 = new File (f1.replace ('/', File.separatorChar));
File file2 = new File (f2.replace ('/', File.separatorChar));
StringWriter out1 = new StringWriter ();
BsonParser p1 = new BsonParser (new FileInputStream (file1));
p1.setRootAsArray (true);
Assert.assertEquals (true, p1.isRootAsArray ());
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 (file2));
JsonGenerator g2 = new TextJsonGenerator (out2);
Utils.convert (p2, g2);
p2.close ();
g2.close ();
Assert.assertEquals (out1.toString (), out2.toString ());
}
示例4: testReader
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
void testReader (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 InputStreamReader (new FileInputStream (file), BOM.utf8));
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 InputStreamReader (new FileInputStream (file), BOM.utf8));
JsonGenerator g2 = new TextJsonGenerator (out2);
Utils.convert (p2, g2);
p2.close ();
g2.close ();
Assert.assertEquals (out1.toString (), out2.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: marshall
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
/**
* Marshals given object to provided Writer or OutputStream.
*
* @param object object to marshall
* @param jsonGenerator generator to use
*/
public void marshall(Object object, JsonGenerator jsonGenerator) {
try {
final ContainerModel model = new ContainerModel(runtimeType != null ? runtimeType : object.getClass(), null);
serializeRoot(object, jsonGenerator, model);
} catch (JsonbException e) {
logger.severe(e.getMessage());
throw e;
} finally {
try {
jsonGenerator.close();
} catch (JsonGenerationException jge) {
logger.severe(jge.getMessage());
}
}
}
示例7: testFile
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
void testFile (String f1, String f2, boolean smallBuf) throws IOException
{
File file1 = new File (f1.replace ('/', File.separatorChar));
File file2 = new File (f2.replace ('/', File.separatorChar));
StringWriter out1 = new StringWriter ();
JsonParser p1 = getJsonParser (file1, smallBuf);
JsonGenerator 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 (file2));
JsonGenerator g2 = new TextJsonGenerator (out2);
Utils.convert (p2, g2);
p2.close ();
g2.close ();
Assert.assertEquals (out1.toString (), out2.toString ());
}
示例8: testFile
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
private void testFile (Charset charset) throws IOException
{
File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar));
CookJsonProvider provider = new CookJsonProvider ();
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
JsonParser p = TextJsonConfigHandler.getJsonParser (new FileInputStream (file));
JsonGenerator g = provider.createGeneratorFactory (new HashMap<String, Object> ()).createGenerator (bos, charset);
Utils.convert (p, g);
p.close ();
g.close ();
StringWriter sw = new StringWriter ();
p = provider.createParser (new ByteArrayInputStream (bos.toByteArray ()));
g = provider.createGenerator (sw);
Utils.convert (p, g);
p.close ();
g.close ();
Assert.assertEquals (Utils.getString (file), sw.toString ());
}
示例9: testJsonValueJson
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Test
public void testJsonValueJson () 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> ();
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);
}
示例10: testFile
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
void testFile (String f) throws IOException
{
File file = new File (f.replace ('/', File.separatorChar));
StringWriter out1 = new StringWriter ();
CookJsonParser p1 = TextJsonConfigHandler.getJsonParser (new FileInputStream (file));
p1.next ();
JsonValue v = p1.getValue ();
TextJsonGenerator g1 = new TextJsonGenerator (out1);
g1.write (v);
p1.close ();
g1.close ();
StringWriter out2 = new StringWriter ();
CookJsonParser p2 = TextJsonConfigHandler.getJsonParser (new FileInputStream (file));
JsonGenerator g2 = new TextJsonGenerator (out2);
Utils.convert (p2, g2);
p2.close ();
g2.close ();
// because JsonObject ordering is based on hash, we cannot directly
// compare the output. Instead, we compare the length.
Assert.assertEquals (out1.toString ().length (), out2.toString ().length ());
}
示例11: 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 ());
}
示例12: testBson
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Test
public void testBson () throws IOException
{
File file1 = new File ("../tests/data/binary.bson".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);
// first convert from Json to Bson using stream API
File bsonFile = testFolder.newFile ();
JsonParser p = provider.createParserFactory (bsonConfig).createParser (new FileInputStream (file1));
JsonGenerator g = provider.createGeneratorFactory (bsonConfig).createGenerator (new FileOutputStream (bsonFile));
Utils.convert (p, g);
p.close ();
g.close ();
BsonFixLength.fix (bsonFile);
FileAssert.assertBinaryEquals (file1, bsonFile);
}
示例13: testJson
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
@Test
public void testJson () 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> ();
File jsonFile = testFolder.newFile ();
JsonParser p = provider.createParserFactory (bsonConfig).createParser (new FileInputStream (file1));
JsonGenerator g = provider.createGeneratorFactory (jsonConfig).createGenerator (new FileOutputStream (jsonFile));
Utils.convert (p, g);
p.close ();
g.close ();
FileAssert.assertBinaryEquals (file2, jsonFile);
}
示例14: 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 ();
UTF8TextJsonParser 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 ());
}
示例15: main
import javax.json.stream.JsonGenerator; //導入方法依賴的package包/類
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
StringWriter writer = new StringWriter();
JsonGeneratorFactory jf = JsonUtil.getJsonGeneratorFactory(true);
JsonGenerator gen = jf.createGenerator(writer);
gen.writeStartObject()
.write("firstName", "Duke")
.write("lastName", "Java")
.write("age", 18)
.write("streetAddress", "100 Internet Dr")
.write("city", "JavaTown")
.write("state", "JA")
.write("postalCode", "12345")
.writeStartArray("phoneNumbers")
.writeStartObject()
.write("type", "mobile")
.write("number", "111-111-1111")
.writeEnd()
.writeStartObject()
.write("type", "home")
.write("number", "222-222-2222")
.writeEnd()
.writeEnd()
.writeEnd();
gen.close();
System.out.println(writer.toString());
}