/** Parse a schema from the provided file. * If named, the schema is added to the names known to this parser. */ public Schema parse(File file) throws IOException { return parse(FACTORY.createJsonParser(file)); }
static JsonNode parseJson(String s) { try { return MAPPER.readTree(FACTORY.createJsonParser(new StringReader(s))); } catch (JsonParseException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
ObjectMapper mapper = new ObjectMapper(); JsonFactory factory = mapper.getJsonFactory(); // since 2.1 use mapper.getFactory() instead JsonParser jp = factory.createJsonParser("{\"k1\":\"v1\"}"); JsonNode actualObj = mapper.readTree(jp);
/** Parse a schema from the provided string. * If named, the schema is added to the names known to this parser. */ public Schema parse(String s) { try { return parse(FACTORY.createJsonParser(new StringReader(s))); } catch (IOException e) { throw new SchemaParseException(e); } }
/** Parse a schema from the provided stream. * If named, the schema is added to the names known to this parser. * The input stream stays open after the parsing. */ public Schema parse(InputStream in) throws IOException { return parse(FACTORY.createJsonParser(in).disable( JsonParser.Feature.AUTO_CLOSE_SOURCE)); }
/** * Parses a JSON string and converts it to the object model described in * {@link org.apache.avro.JsonProperties}. */ public static Object parseJson(String s) { try { return JacksonUtils.toObject(MAPPER.readTree(FACTORY.createJsonParser( new StringReader(s)))); } catch (JsonParseException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Reconfigures this JsonDecoder to use the String provided for input. * <p/> * If the String provided is null, a NullPointerException is thrown. * <p/> * Otherwise, this JsonDecoder will reset its state and then * reconfigure its input. * @param in * The String to read from. Cannot be null. * @throws IOException * @return this JsonDecoder */ public JsonDecoder configure(String in) throws IOException { if (null == in) { throw new NullPointerException("String to read from cannot be null!"); } parser.reset(); reorderBuffers.clear(); currentReorderBuffer = null; this.in = new JsonFactory().createJsonParser(in); this.in.nextToken(); return this; }
/** * Reconfigures this JsonDecoder to use the InputStream provided. * <p/> * If the InputStream provided is null, a NullPointerException is thrown. * <p/> * Otherwise, this JsonDecoder will reset its state and then * reconfigure its input. * @param in * The InputStream to read from. Cannot be null. * @throws IOException * @return this JsonDecoder */ public JsonDecoder configure(InputStream in) throws IOException { if (null == in) { throw new NullPointerException("InputStream to read from cannot be null!"); } parser.reset(); reorderBuffers.clear(); currentReorderBuffer = null; this.in = jsonFactory.createJsonParser(in); this.in.nextToken(); return this; }
public static void main(String[] args) throws Exception { JsonFactory f = new MappingJsonFactory(); JsonParser jp = f.createJsonParser(new File(args[0])); JsonToken current; current = jp.nextToken();
public Tests(int bufferSize, int depth, String input) throws IOException { this.depth = depth; byte[] in = input.getBytes("UTF-8"); JsonFactory f = new JsonFactory(); JsonParser p = f.createJsonParser( new ByteArrayInputStream(input.getBytes("UTF-8"))); ByteArrayOutputStream os = new ByteArrayOutputStream(); EncoderFactory factory = new EncoderFactory() .configureBlockSize(bufferSize); Encoder cos = factory.blockingBinaryEncoder(os, null); serialize(cos, p, os); cos.flush(); byte[] bb = os.toByteArray(); // dump(bb); this.input = DecoderFactory.get().binaryDecoder(bb, null); this.parser = f.createJsonParser(new ByteArrayInputStream(in)); }
@Test public void testToStringIsJson() throws JsonParseException, IOException { Field stringField = new Field("string", Schema.create(Type.STRING), null, null); Field enumField = new Field("enum", Schema.createEnum("my_enum", "doc", null, Arrays.asList("a", "b", "c")), null, null); Schema schema = Schema.createRecord("my_record", "doc", "mytest", false); schema.setFields(Arrays.asList(stringField, enumField)); GenericRecord r = new GenericData.Record(schema); // \u2013 is EN DASH r.put(stringField.name(), "hello\nthere\"\tyou\u2013}"); r.put(enumField.name(), new GenericData.EnumSymbol(enumField.schema(),"a")); String json = r.toString(); JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createJsonParser(json); ObjectMapper mapper = new ObjectMapper(); // will throw exception if string is not parsable json mapper.readTree(parser); }
JsonParser parser = factory.createJsonParser(json); ObjectMapper mapper = new ObjectMapper();
return MAPPER.readTree(FACTORY.createJsonParser(json)); } catch (IOException e) { throw new RuntimeException(e);
@Test public void testSpecificRecordToString() throws IOException { FooBarSpecificRecord foo = FooBarSpecificRecord.newBuilder() .setId(123) .setName("foo") .setNicknames(Arrays.asList("bar")) .setRelatedids(Arrays.asList(1, 2, 3)) .setTypeEnum(TypeEnum.c) .build(); String json = foo.toString(); JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createJsonParser(json); ObjectMapper mapper = new ObjectMapper(); // will throw exception if string is not parsable json mapper.readTree(parser); }
.locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE); JsonFactory jfactory = new JsonFactory(mapper); JsonParser jParser = jfactory.createJsonParser(response.getStream()); boolean found = false; while (jParser.nextToken() != JsonToken.END_OBJECT) {
public class JacksonProgram { public static void main(String[] args) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); JsonFactory jsonFactory = new JsonFactory(); JsonParser parser = jsonFactory .createJsonParser("{\"name\":\"John\", \"grade\":\"D\"}"); Employee employee = objectMapper.readValue(parser, Employee.class); System.out.println(employee); } }
public JacksonReader(JsonFactory factory, Reader in) { try { reader = factory.createJsonParser(in); } catch (IOException e) { throw new AmazonClientException("Failed to create JSON reader", e); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public <T> T readValue(URL src, TypeReference valueTypeRef) throws IOException, JsonParseException, JsonMappingException { return (T) _readMapAndClose(_jsonFactory.createJsonParser(src), _typeFactory.constructType(valueTypeRef)); }
@SuppressWarnings("unchecked") public <T> T readValue(String content, Class<T> valueType) throws IOException, JsonParseException, JsonMappingException { // !!! TODO // _setupClassLoaderForDeserialization(valueType); return (T) _readMapAndClose(_jsonFactory.createJsonParser(content), _typeFactory.constructType(valueType)); }
@Override public JsonParser createJsonParser(InputStream in) throws IOException { Preconditions.checkNotNull(in); return new JacksonParser(this, factory.createJsonParser(in)); }