本文整理汇总了Java中javax.json.JsonReaderFactory.createReader方法的典型用法代码示例。如果您正苦于以下问题:Java JsonReaderFactory.createReader方法的具体用法?Java JsonReaderFactory.createReader怎么用?Java JsonReaderFactory.createReader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.json.JsonReaderFactory
的用法示例。
在下文中一共展示了JsonReaderFactory.createReader方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toDictionary
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
public static Map<String, Object> toDictionary(String source) throws JsonParseException {
if (source == null) {
return null;
}
JsonObject jsonObject = null;
try {
JsonReaderFactory factory = Json.createReaderFactory(null);
JsonReader jsonReader = factory.createReader(new StringReader(source));
jsonObject = jsonReader.readObject();
jsonReader.close();
} catch (Exception e) {
throw new JsonParseException(e);
}
Map<String, Object> dictionary = new HashMap<String, Object>();
fill_dictionary(dictionary, jsonObject);
return dictionary;
}
示例2: parseSpeaker
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
private final List<Speaker> parseSpeaker(URL speakerResource) {
try {
final JsonReaderFactory factory = Json.createReaderFactory(null);
final JsonReader reader = factory.createReader(speakerResource.openStream());
final JsonArray items = reader.readArray();
// parse session objects
final List<Speaker> speaker = new LinkedList<>();
for (final JsonValue item : items) {
final Speaker s = new Speaker((JsonObject) item);
s.setId(String.valueOf(this.speakerId.incrementAndGet()));
speaker.add(s);
}
return speaker;
} catch (Exception e) {
throw new RuntimeException("Failed to parse speaker.json");
}
}
示例3: testBson
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
@Test
public void testBson () throws IOException
{
BasicConfigurator.configure ();
String f = "../tests/data/data1.bson";
File file = new File (f.replace ('/', File.separatorChar));
JsonPathProvider provider = new JsonPathProvider ();
Configuration pathConfig = Configuration.defaultConfiguration ().jsonProvider (provider);
JsonPath path = JsonPath.compile ("$..A");
JsonProvider p = new CookJsonProvider ();
HashMap<String, Object> readConfig = new HashMap<String, Object> ();
readConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
readConfig.put (CookJsonProvider.ROOT_AS_ARRAY, Boolean.TRUE);
JsonReaderFactory rf = p.createReaderFactory (readConfig);
JsonReader reader = rf.createReader (new FileInputStream (file));
JsonStructure obj = reader.read ();
reader.close ();
JsonValue value = path.read (obj, pathConfig);
Assert.assertEquals ("[1,3,5,7]", provider.toJson (value));
}
示例4: testGrowingString
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
@Test
public void testGrowingString() throws Throwable {
JsonReaderFactory factory = Json.createReaderFactory(null);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10000; i++) {
sb.append('x');
String growingString = sb.toString();
String str = "[4, \"\", \"" + growingString + "\", \"\", \"" + growingString + "\", \"\", 400]";
try {
JsonReader reader = factory.createReader(new StringReader(str));
JsonArray array = reader.readArray();
assertEquals(4, array.getInt(0));
assertEquals("", array.getString(1));
assertEquals(growingString, array.getString(2));
assertEquals("", array.getString(3));
assertEquals(growingString, array.getString(4));
assertEquals("", array.getString(5));
assertEquals(400, array.getInt(6));
reader.close();
} catch (Throwable t) {
throw new Throwable("Failed for growingString with length: " + i, t);
}
}
}
示例5: mergeProperties
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
public void mergeProperties(final String properties) {
if (StringUtils.isEmpty(properties)) {
return;
}
if (StringUtils.isEmpty(getProperties())) {
setProperties(properties);
return;
}
final JsonReaderFactory fact = Json.createReaderFactory(null);
final JsonReader r1 = fact.createReader(new StringReader(getProperties()));
final JsonObjectBuilder jbf = Json.createObjectBuilder(r1.readObject());
final JsonReader r2 = fact.createReader(new StringReader(getProperties()));
final JsonObject obj2 = r2.readObject();
for (Entry<String, JsonValue> jv : obj2.entrySet()) {
jbf.add(jv.getKey(), jv.getValue());
}
setProperties(jbf.build().toString());
}
示例6: parseJson
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
@Before
public void parseJson() {
JsonReaderFactory factory = Json.createReaderFactory(null);
InputStream in = LibrariesTest.class.getResourceAsStream("libraries.json");
JsonReader reader = factory.createReader(in);
apis = reader.readArray().toArray(new JsonObject[0]);
}
示例7: testJson
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
@Test
public void testJson () throws IOException
{
BasicConfigurator.configure ();
String f = "../tests/data/data3.json";
File file = new File (f.replace ('/', File.separatorChar));
JsonPathProvider provider = new JsonPathProvider ();
Configuration pathConfig = Configuration.defaultConfiguration ().jsonProvider (provider);
JsonPath path = JsonPath.compile ("$..A");
JsonProvider p = new CookJsonProvider ();
HashMap<String, Object> readConfig = new HashMap<String, Object> ();
JsonReaderFactory rf = p.createReaderFactory (readConfig);
JsonReader reader = rf.createReader (new FileInputStream (file));
JsonStructure obj = reader.read ();
reader.close ();
JsonValue value = path.read (obj, pathConfig);
Assert.assertEquals ("[1,3,5,7]", provider.toJson (value));
}
示例8: testGrowingStringWithDifferentBufferSizes
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
@Test
public void testGrowingStringWithDifferentBufferSizes() throws Throwable {
for (int size = 20; size < 500; size++) {
final int k = size;
Map<String, Object> config = new HashMap<String, Object>() {
{
put("org.apache.johnzon.default-char-buffer", k);
}
};
JsonReaderFactory factory = Json.createReaderFactory(config);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
sb.append('x');
String name = sb.toString();
String str = "[4, \"\", \"" + name + "\", \"\", \"" + name + "\", \"\", 400]";
try {
JsonReader reader = factory.createReader(new StringReader(str));
JsonArray array = reader.readArray();
assertEquals(4, array.getInt(0));
assertEquals("", array.getString(1));
assertEquals(name, array.getString(2));
assertEquals("", array.getString(3));
assertEquals(name, array.getString(4));
assertEquals("", array.getString(5));
assertEquals(400, array.getInt(6));
reader.close();
} catch (Throwable t) {
throw new Throwable("Failed for buffer size=" + size + " growingString with length: " + i, t);
}
}
}
}
示例9: penginePost
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
/**
* Low level famulus to abstract out some of the HTTP handling common to all requests
*
* @param url The actual url to httpRequest
* @param contentType The value string of the Content-Type header
* @param body the body of the POST request
* @return the returned JSON object
*
* @throws CouldNotCreateException
* @throws IOException
*/
private JsonObject penginePost(
URL url,
String contentType,
String body
) throws IOException {
StringBuffer response;
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// above should get us an HttpsURLConnection if it's https://...
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "JavaPengine");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-type", contentType);
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
try {
wr.writeBytes(body);
wr.flush();
} finally {
wr.close();
}
int responseCode = con.getResponseCode();
if(responseCode < 200 || responseCode > 299) {
throw new IOException("bad response code (if 500, query was invalid? query threw Prolog exception?) " + Integer.toString(responseCode) + " " + url.toString() + " " + body);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuffer();
try {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} finally {
in.close();
}
JsonReaderFactory jrf = Json.createReaderFactory(null);
JsonReader jr = jrf.createReader(new StringReader(response.toString()));
JsonObject respObject = jr.readObject();
return respObject;
} catch (IOException e) {
state.destroy();
throw e;
}
}
示例10: generate
import javax.json.JsonReaderFactory; //导入方法依赖的package包/类
private void generate(final JsonReaderFactory readerFactory, final File source, final Writer writer, final String javaName) throws MojoExecutionException {
JsonReader reader = null;
try {
reader = readerFactory.createReader(new FileReader(source));
final JsonStructure structure = reader.read();
if (JsonArray.class.isInstance(structure) || !JsonObject.class.isInstance(structure)) { // quite redundant for now but to avoid surprises in future
throw new MojoExecutionException("This plugin doesn't support array generation, generate the model (generic) and handle arrays in your code");
}
final JsonObject object = JsonObject.class.cast(structure);
final Collection<String> imports = new TreeSet<String>();
// while we browse the example tree just store imports as well, avoids a 2 passes processing duplicating imports logic
final StringWriter memBuffer = new StringWriter();
generateFieldsAndMethods(memBuffer, object, " ", imports);
if (header != null) {
writer.write(header);
writer.write('\n');
}
writer.write("package " + packageBase + ";\n\n");
if (!imports.isEmpty()) {
for (final String imp : imports) {
writer.write("import " + imp + ";\n");
}
writer.write('\n');
}
writer.write("public class " + javaName + " {\n");
writer.write(memBuffer.toString());
writer.write("}\n");
} catch (final IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
if (reader != null) {
reader.close();
}
}
}