Exemplo n.º 1
1
 public Object parseAndClose(InputStream paramInputStream, Charset paramCharset, Type paramType)
     throws IOException {
   paramInputStream = jsonFactory.createJsonParser(paramInputStream, paramCharset);
   initializeParser(paramInputStream);
   return paramInputStream.parse(paramType, true);
 }
Exemplo n.º 2
0
  // And then one more test just for Bytes-based symbol table
  public void testByteBasedSymbolTable() throws Exception {
    // combination of short, medium1/2, long names...
    final String JSON =
        aposToQuotes(
            "{'abc':1, 'abc\\u0000':2, '\\u0000abc':3, "
                // then some medium
                + "'abc123':4,'abcd1234':5,"
                + "'abcd1234a':6,'abcd1234abcd':7,"
                + "'abcd1234abcd1':8"
                + "}");

    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(JSON.getBytes("UTF-8"));
    ByteQuadsCanonicalizer symbols = _findSymbols(p);
    assertEquals(0, symbols.size());
    _streamThrough(p);
    assertEquals(8, symbols.size());
    p.close();

    // and, for fun, try again
    p = f.createParser(JSON.getBytes("UTF-8"));
    _streamThrough(p);
    symbols = _findSymbols(p);
    assertEquals(8, symbols.size());
    p.close();

    p = f.createParser(JSON.getBytes("UTF-8"));
    _streamThrough(p);
    symbols = _findSymbols(p);
    assertEquals(8, symbols.size());
    p.close();
  }
  public void testUtf8Issue462() throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    IOContext ioc = new IOContext(new BufferRecycler(), bytes, true);
    JsonGenerator gen = new UTF8JsonGenerator(ioc, 0, null, bytes);
    String str = "Natuurlijk is alles gelukt en weer een tevreden klant\uD83D\uDE04";
    int length = 4000 - 38;

    for (int i = 1; i <= length; ++i) {
      gen.writeNumber(1);
    }
    gen.writeString(str);
    gen.flush();
    gen.close();

    // Also verify it's parsable?
    JsonFactory f = new JsonFactory();
    JsonParser p = f.createParser(bytes.toByteArray());
    for (int i = 1; i <= length; ++i) {
      assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
      assertEquals(1, p.getIntValue());
    }
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(str, p.getText());
    assertNull(p.nextToken());
    p.close();
  }
  public void testJsonFactoryLinkage() {
    // first, implicit factory, giving implicit linkage
    assertSame(MAPPER, MAPPER.getFactory().getCodec());

    // and then explicit factory, which should also be implicitly linked
    JsonFactory f = new JsonFactory();
    ObjectMapper m = new ObjectMapper(f);
    assertSame(f, m.getFactory());
    assertSame(m, f.getCodec());
  }
Exemplo n.º 5
0
 /**
  * Method that can be used to serialize any Java value as a String. Functionally equivalent to
  * calling {@link #writeValue(Writer,Object)} with {@link java.io.StringWriter} and constructing
  * String, but more efficient.
  *
  * <p>Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
  */
 public String writeValueAsString(Object value) throws JsonProcessingException {
   // alas, we have to pull the recycler directly here...
   SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
   try {
     _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value);
   } catch (JsonProcessingException e) { // to support [JACKSON-758]
     throw e;
   } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
     throw JsonMappingException.fromUnexpectedIOE(e);
   }
   return sw.getAndClear();
 }
Exemplo n.º 6
0
 /**
  * Method that can be used to serialize any Java value as a byte array. Functionally equivalent to
  * calling {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream} and
  * getting bytes, but more efficient. Encoding used will be UTF-8.
  *
  * <p>Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
  */
 public byte[] writeValueAsBytes(Object value) throws JsonProcessingException {
   ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
   try {
     _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value);
   } catch (JsonProcessingException e) { // to support [JACKSON-758]
     throw e;
   } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
     throw JsonMappingException.fromUnexpectedIOE(e);
   }
   byte[] result = bb.toByteArray();
   bb.release();
   return result;
 }
Exemplo n.º 7
0
  // [core#191]: similarly, but for "short" symbols:
  public void testShortNameCollisionsViaParser() throws Exception {
    JsonFactory f = new JsonFactory();
    String json = _shortDoc191();
    JsonParser p;

    // First: ensure that char-based is fine
    p = f.createParser(json);
    while (p.nextToken() != null) {}
    p.close();

    // and then that byte-based
    p = f.createParser(json.getBytes("UTF-8"));
    while (p.nextToken() != null) {}
    p.close();
  }
 public String toPrettyString()
 {
   if (jsonFactory != null) {
     return jsonFactory.toPrettyString(this);
   }
   return super.toString();
 }
Exemplo n.º 9
0
  private void process(File input) throws IOException {
    JsonFactory jsonF = new JsonFactory();
    JsonParser jp = jsonF.createJsonParser(input);
    TwitterEntry entry = read(jp);

    // let's write to a file, using UTF-8 encoding (only sensible one)
    StringWriter strw = new StringWriter();
    JsonGenerator jg = jsonF.createJsonGenerator(strw);
    jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier

    // Here we would modify it... for now, will just (re)indent it

    write(jg, entry);

    System.out.println("Result = [" + strw.toString() + "]");
  }
Exemplo n.º 10
0
 /**
  * Method for reading sequence of Objects from parser stream.
  *
  * @since 1.8
  */
 public <T> MappingIterator<T> readValues(URL src) throws IOException, JsonProcessingException {
   JsonParser jp = _jsonFactory.createJsonParser(src);
   if (_schema != null) {
     jp.setSchema(_schema);
   }
   DeserializationContext ctxt = _createDeserializationContext(jp, _config);
   return new MappingIterator<T>(_valueType, jp, ctxt, _findRootDeserializer(_config, _valueType));
 }
 protected final double _testRawDeser(int reps, byte[] json, ObjectReader reader)
     throws IOException {
   long start = System.nanoTime();
   final JsonFactory f = reader.getFactory();
   while (--reps >= 0) {
     JsonParser p = f.createParser(new ByteArrayInputStream(json));
     JsonToken t;
     while ((t = p.nextToken()) != null) {
       if (t == JsonToken.VALUE_STRING) {
         p.getText();
       } else if (t.isNumeric()) {
         p.getNumberValue();
       }
       ;
     }
     p.close();
   }
   hash = f.hashCode();
   return _msecsFromNanos(System.nanoTime() - start);
 }
Exemplo n.º 12
0
 private void _testSurrogates(JsonFactory f, boolean checkText) throws IOException {
   byte[] json = "{\"text\":\"\uD83D\uDE03\"}".getBytes("UTF-8");
   // first
   JsonParser jp = f.createParser(json);
   assertToken(JsonToken.START_OBJECT, jp.nextToken());
   assertToken(JsonToken.FIELD_NAME, jp.nextToken());
   if (checkText) {
     assertEquals("text", jp.getText());
   }
   assertToken(JsonToken.VALUE_STRING, jp.nextToken());
   if (checkText) {
     assertEquals("\uD83D\uDE03", jp.getText());
   }
   assertToken(JsonToken.END_OBJECT, jp.nextToken());
 }
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: java [input]");
      System.exit(1);
    }
    byte[] data = readAll(args[0]);

    JsonFactory f = new JsonFactory();
    boolean doIntern = true;

    f.configure(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES, doIntern);
    f.configure(JsonFactory.Feature.INTERN_FIELD_NAMES, doIntern);

    ObjectMapper m = new ObjectMapper();
    Object input1 = m.readValue(data, Object.class);
    JsonNode input2 = m.readTree(data);

    new ManualReadPerfUntypedStream()
        .testFromBytes(
            //            .testFromString(
            m, "JSON-as-Object", input1, Object.class, m, "JSON-as-Object2", input2, Object.class
            //               ,m, "JSON-as-Node", input2, JsonNode.class
            );
  }
 public String toString()
 {
   if (jsonFactory != null) {
     try
     {
       String str = jsonFactory.toString(this);
       return str;
     }
     catch (IOException localIOException)
     {
       throw koh.a(localIOException);
     }
   }
   return super.toString();
 }
Exemplo n.º 15
0
 @JsonCreator
 public static Measurement factory(@JsonProperty("id") int id) {
   JsonFactory jsonFactory = new JsonFactory();
   return jsonFactory.getMeasurement(id);
 }
 protected JsonParser constructParser(byte[] data) throws IOException {
   return _factory.createParser(data, 0, data.length);
 }
 protected JsonGenerator constructGenerator(OutputStream baos) throws IOException {
   return _factory.createGenerator(baos, JsonEncoding.UTF8);
 }
 protected JsonParser constructParser(InputStream in) throws IOException {
   return _factory.createParser(in);
 }
Exemplo n.º 19
0
 public boolean isEnabled(JsonParser.Feature f) {
   return _jsonFactory.isEnabled(f);
 }
Exemplo n.º 20
0
 /**
  * Method that can be used to serialize any Java value as JSON output, written to File provided.
  */
 public void writeValue(File resultFile, Object value)
     throws IOException, JsonGenerationException, JsonMappingException {
   _configAndWriteValue(_jsonFactory.createJsonGenerator(resultFile, JsonEncoding.UTF8), value);
 }
Exemplo n.º 21
0
 public JsonNode readTree(String content) throws IOException, JsonProcessingException {
   return _bindAndCloseAsTree(_jsonFactory.createJsonParser(content));
 }
Exemplo n.º 22
0
 public JsonNode readTree(Reader r) throws IOException, JsonProcessingException {
   return _bindAndCloseAsTree(_jsonFactory.createJsonParser(r));
 }
Exemplo n.º 23
0
 public JsonNode readTree(InputStream in) throws IOException, JsonProcessingException {
   return _bindAndCloseAsTree(_jsonFactory.createJsonParser(in));
 }
Exemplo n.º 24
0
 @SuppressWarnings("unchecked")
 public <T> T readValue(URL src) throws IOException, JsonProcessingException {
   return (T) _bindAndClose(_jsonFactory.createJsonParser(src));
 }
Exemplo n.º 25
0
 @SuppressWarnings("unchecked")
 public <T> T readValue(byte[] src, int offset, int length)
     throws IOException, JsonProcessingException {
   return (T) _bindAndClose(_jsonFactory.createJsonParser(src, offset, length));
 }
Exemplo n.º 26
0
 /**
  * Method that can be used to serialize any Java value as JSON output, using output stream
  * provided (using encoding {@link JsonEncoding#UTF8}).
  *
  * <p>Note: method does not close the underlying stream explicitly here; however, {@link
  * JsonFactory} this mapper uses may choose to close the stream depending on its settings (by
  * default, it will try to close it when {@link JsonGenerator} we construct is closed).
  */
 public void writeValue(OutputStream out, Object value)
     throws IOException, JsonGenerationException, JsonMappingException {
   _configAndWriteValue(_jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8), value);
 }
Exemplo n.º 27
0
 /**
  * Method that can be used to serialize any Java value as JSON output, using Writer provided.
  *
  * <p>Note: method does not close the underlying stream explicitly here; however, {@link
  * JsonFactory} this mapper uses may choose to close the stream depending on its settings (by
  * default, it will try to close it when {@link JsonGenerator} we construct is closed).
  */
 public void writeValue(Writer w, Object value)
     throws IOException, JsonGenerationException, JsonMappingException {
   _configAndWriteValue(_jsonFactory.createJsonGenerator(w), value);
 }