Search in sources :

Example 6 with JsonStringEncoder

use of com.fasterxml.jackson.core.io.JsonStringEncoder in project elasticsearch by elastic.

the class AbstractQueryTestCase method getRandomValueForFieldName.

/**
     * create a random value for either {@link AbstractQueryTestCase#BOOLEAN_FIELD_NAME}, {@link AbstractQueryTestCase#INT_FIELD_NAME},
     * {@link AbstractQueryTestCase#DOUBLE_FIELD_NAME}, {@link AbstractQueryTestCase#STRING_FIELD_NAME} or
     * {@link AbstractQueryTestCase#DATE_FIELD_NAME}, or a String value by default
     */
protected static Object getRandomValueForFieldName(String fieldName) {
    Object value;
    switch(fieldName) {
        case STRING_FIELD_NAME:
            if (rarely()) {
                // unicode in 10% cases
                JsonStringEncoder encoder = JsonStringEncoder.getInstance();
                value = new String(encoder.quoteAsString(randomUnicodeOfLength(10)));
            } else {
                value = randomAsciiOfLengthBetween(1, 10);
            }
            break;
        case INT_FIELD_NAME:
            value = randomIntBetween(0, 10);
            break;
        case DOUBLE_FIELD_NAME:
            value = 1 + randomDouble() * 9;
            break;
        case BOOLEAN_FIELD_NAME:
            value = randomBoolean();
            break;
        case DATE_FIELD_NAME:
            value = new DateTime(System.currentTimeMillis(), DateTimeZone.UTC).toString();
            break;
        default:
            value = randomAsciiOfLengthBetween(1, 10);
    }
    return value;
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) JsonStringEncoder(com.fasterxml.jackson.core.io.JsonStringEncoder)

Example 7 with JsonStringEncoder

use of com.fasterxml.jackson.core.io.JsonStringEncoder in project elasticsearch by elastic.

the class SpanTermQueryBuilderTests method doCreateTestQueryBuilder.

@Override
protected SpanTermQueryBuilder doCreateTestQueryBuilder() {
    String fieldName = null;
    Object value;
    if (randomBoolean()) {
        fieldName = STRING_FIELD_NAME;
    }
    if (frequently()) {
        value = randomAsciiOfLengthBetween(1, 10);
    } else {
        // generate unicode string in 10% of cases
        JsonStringEncoder encoder = JsonStringEncoder.getInstance();
        value = new String(encoder.quoteAsString(randomUnicodeOfLength(10)));
    }
    if (fieldName == null) {
        fieldName = randomAsciiOfLengthBetween(1, 10);
    }
    return createQueryBuilder(fieldName, value);
}
Also used : JsonStringEncoder(com.fasterxml.jackson.core.io.JsonStringEncoder)

Example 8 with JsonStringEncoder

use of com.fasterxml.jackson.core.io.JsonStringEncoder in project jackson-core by FasterXML.

the class BufferRecyclers method getJsonStringEncoder.

public static JsonStringEncoder getJsonStringEncoder() {
    SoftReference<JsonStringEncoder> ref = _encoderRef.get();
    JsonStringEncoder enc = (ref == null) ? null : ref.get();
    if (enc == null) {
        enc = new JsonStringEncoder();
        _encoderRef.set(new SoftReference<JsonStringEncoder>(enc));
    }
    return enc;
}
Also used : JsonStringEncoder(com.fasterxml.jackson.core.io.JsonStringEncoder)

Example 9 with JsonStringEncoder

use of com.fasterxml.jackson.core.io.JsonStringEncoder in project vespa by vespa-engine.

the class JsonReaderTestCase method testRaw.

@Test
public final void testRaw() throws IOException {
    String stuff = new String(new JsonStringEncoder().quoteAsString(new Base64().encodeToString(Utf8.toBytes("smoketest"))));
    InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:testraw::whee\"," + " \"fields\": { \"actualraw\": \"" + stuff + "\"" + " }}"));
    JsonReader r = new JsonReader(types, rawDoc, parserFactory);
    DocumentParseInfo parseInfo = r.parseDocument().get();
    DocumentType docType = r.readDocumentType(parseInfo.documentId);
    DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId));
    new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put);
    Document doc = put.getDocument();
    FieldValue f = doc.getFieldValue(doc.getField("actualraw"));
    assertSame(Raw.class, f.getClass());
    Raw s = (Raw) f;
    ByteBuffer b = s.getByteBuffer();
    assertEquals("smoketest", Utf8.toString(b));
}
Also used : Base64(org.apache.commons.codec.binary.Base64) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) Raw(com.yahoo.document.datatypes.Raw) Document(com.yahoo.document.Document) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader) JsonStringEncoder(com.fasterxml.jackson.core.io.JsonStringEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) Test(org.junit.Test)

Example 10 with JsonStringEncoder

use of com.fasterxml.jackson.core.io.JsonStringEncoder in project XRTB by benmfaul.

the class TestEncoding method main.

public static void main(String[] args) {
    JsonStringEncoder encoder = JsonStringEncoder.getInstance();
    String test = "This is \\\\\" a test";
    char[] output = encoder.quoteAsString(test);
    String end = new String(output);
    System.out.println(test);
    ;
    System.out.println(end);
    ;
}
Also used : JsonStringEncoder(com.fasterxml.jackson.core.io.JsonStringEncoder)

Aggregations

JsonStringEncoder (com.fasterxml.jackson.core.io.JsonStringEncoder)10 Base64 (org.apache.commons.codec.binary.Base64)2 Test (org.junit.Test)2 Document (com.yahoo.document.Document)1 DocumentPut (com.yahoo.document.DocumentPut)1 DocumentType (com.yahoo.document.DocumentType)1 FieldValue (com.yahoo.document.datatypes.FieldValue)1 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)1 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)1 Raw (com.yahoo.document.datatypes.Raw)1 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)1 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)1 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)1 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)1 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1