Search in sources :

Example 6 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class JsonRendererTestCase method testJsonObjects.

@Test
public void testJsonObjects() throws InterruptedException, ExecutionException, IOException, JSONException {
    String expected = "{\n" + "    \"root\": {\n" + "        \"children\": [\n" + "            {\n" + "                \"fields\": {\n" + "                    \"inspectable\": {\n" + "                        \"a\": \"b\"\n" + "                    },\n" + "                    \"jackson\": {\n" + "                        \"Nineteen-eighty-four\": 1984\n" + "                    },\n" + "                    \"json producer\": {\n" + "                        \"long in structured\": 7809531904\n" + "                    },\n" + "                    \"org.json array\": [\n" + "                        true,\n" + "                        true,\n" + "                        false\n" + "                    ],\n" + "                    \"org.json object\": {\n" + "                        \"forty-two\": 42\n" + "                    }\n" + "                },\n" + "                \"id\": \"json objects\",\n" + "                \"relevance\": 1.0\n" + "            }\n" + "        ],\n" + "        \"fields\": {\n" + "            \"totalCount\": 0\n" + "        },\n" + "        \"id\": \"toplevel\",\n" + "        \"relevance\": 1.0\n" + "    }\n" + "}\n";
    Result r = newEmptyResult();
    Hit h = new Hit("json objects");
    JSONObject o = new JSONObject();
    JSONArray a = new JSONArray();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode j = mapper.createObjectNode();
    JSONString s = new JSONString("{\"a\": \"b\"}");
    Slime slime = new Slime();
    Cursor c = slime.setObject();
    c.setLong("long in structured", 7809531904L);
    SlimeAdapter slimeInit = new SlimeAdapter(slime.get());
    StructuredData struct = new StructuredData(slimeInit);
    ((ObjectNode) j).put("Nineteen-eighty-four", 1984);
    o.put("forty-two", 42);
    a.put(true);
    a.put(true);
    a.put(false);
    h.setField("inspectable", s);
    h.setField("jackson", j);
    h.setField("json producer", struct);
    h.setField("org.json array", a);
    h.setField("org.json object", o);
    r.hits().add(h);
    String summary = render(r);
    assertEqualJson(expected, summary);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) StructuredData(com.yahoo.search.result.StructuredData) JSONArray(org.json.JSONArray) JsonNode(com.fasterxml.jackson.databind.JsonNode) JSONString(com.yahoo.prelude.hitfield.JSONString) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor) Result(com.yahoo.search.Result) FastHit(com.yahoo.prelude.fastsearch.FastHit) Hit(com.yahoo.search.result.Hit) JSONObject(org.json.JSONObject) SlimeAdapter(com.yahoo.data.access.slime.SlimeAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 7 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class FieldsTestCase method testCompressionJson.

@Test
public final void testCompressionJson() {
    String value = "{0:000000000000000000000000000000000000000000000000000000000000000}";
    byte[] raw = Utf8.toBytesStd(value);
    byte[] output = new byte[raw.length * 2];
    Deflater compresser = new Deflater();
    compresser.setInput(raw);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();
    scratchSpace.putInt((compressedDataLength + 4) | (1 << 31));
    scratchSpace.putInt(raw.length);
    scratchSpace.put(output, 0, compressedDataLength);
    scratchSpace.flip();
    assertTrue(new JSONField(fieldName).isCompressed(scratchSpace));
    new JSONField(fieldName).decode(scratchSpace, contains);
    assertEquals(value, ((JSONString) contains.getField(fieldName)).getContent());
}
Also used : Deflater(java.util.zip.Deflater) XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 8 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class FieldsTestCase method testJSON.

@Test
public final void testJSON() {
    String value = "{1: 2}";
    int s = scratchSpace.position();
    scratchSpace.putInt(value.length());
    scratchSpace.put(Utf8.toBytes(value));
    int l = scratchSpace.position();
    scratchSpace.flip();
    assertEquals(l, new JSONField(fieldName).getLength(scratchSpace));
    scratchSpace.position(s);
    new JSONField(fieldName).decode(scratchSpace, contains);
    assertEquals(value, ((JSONString) contains.getField(fieldName)).getContent());
}
Also used : XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 9 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class SlimeSummaryTestCase method testDecoding.

@Test
public void testDecoding() {
    Tensor tensor1 = Tensor.from("tensor(x{},y{}):{{x:foo,y:bar}:0.1}");
    Tensor tensor2 = Tensor.from("tensor(x[],y[1]):{{x:0,y:0}:-0.3}");
    String summary_cf = "file:src/test/java/com/yahoo/prelude/fastsearch/summary.cfg";
    DocsumDefinitionSet set = createDocsumDefinitionSet(summary_cf);
    byte[] docsum = makeDocsum(tensor1, tensor2);
    FastHit hit = new FastHit();
    assertNull(set.lazyDecode("default", docsum, hit));
    assertThat(hit.getField("integer_field"), equalTo(4));
    assertThat(hit.getField("short_field"), equalTo((short) 2));
    assertThat(hit.getField("byte_field"), equalTo((byte) 1));
    assertThat(hit.getField("float_field"), equalTo(4.5f));
    assertThat(hit.getField("double_field"), equalTo(8.75));
    assertThat(hit.getField("int64_field"), equalTo(8L));
    assertThat(hit.getField("string_field"), equalTo("string_value"));
    assertThat(hit.getField("data_field"), instanceOf(RawData.class));
    assertThat(hit.getField("data_field").toString(), equalTo("data_value"));
    assertThat(hit.getField("longstring_field"), equalTo((Object) "longstring_value"));
    assertThat(hit.getField("longdata_field"), instanceOf(RawData.class));
    assertThat(hit.getField("longdata_field").toString(), equalTo("longdata_value"));
    assertThat(hit.getField("xmlstring_field"), instanceOf(XMLString.class));
    assertThat(hit.getField("xmlstring_field").toString(), equalTo("<tag>xmlstring_value</tag>"));
    if (hit.getField("jsonstring_field") instanceof JSONString) {
        JSONString jstr = (JSONString) hit.getField("jsonstring_field");
        assertThat(jstr.getContent(), equalTo("{\"foo\":1,\"bar\":2}"));
        assertThat(jstr.getParsedJSON(), notNullValue());
        com.yahoo.data.access.Inspectable obj = jstr;
        com.yahoo.data.access.Inspector value = obj.inspect();
        assertThat(value.field("foo").asLong(), equalTo(1L));
        assertThat(value.field("bar").asLong(), equalTo(2L));
    } else {
        StructuredData sdata = (StructuredData) hit.getField("jsonstring_field");
        assertThat(sdata.toJson(), equalTo("{\"foo\":1,\"bar\":2}"));
        com.yahoo.data.access.Inspectable obj = sdata;
        com.yahoo.data.access.Inspector value = obj.inspect();
        assertThat(value.field("foo").asLong(), equalTo(1L));
        assertThat(value.field("bar").asLong(), equalTo(2L));
    }
    assertEquals(tensor1, hit.getField("tensor_field1"));
    assertEquals(tensor2, hit.getField("tensor_field2"));
}
Also used : RawData(com.yahoo.prelude.hitfield.RawData) Tensor(com.yahoo.tensor.Tensor) StructuredData(com.yahoo.search.result.StructuredData) XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 10 with JSONString

use of com.yahoo.prelude.hitfield.JSONString in project vespa by vespa-engine.

the class JSONField method decode.

@Override
public Object decode(ByteBuffer b) {
    long dataLen = 0;
    long len = ((long) b.getInt()) & 0xffffffffL;
    boolean compressed;
    JSONString field;
    // if MSB is set this is a compressed field.  set the compressed
    // flag accordingly and decompress the data
    compressed = ((len & 0x80000000) != 0);
    if (compressed) {
        len &= 0x7fffffff;
        dataLen = b.getInt();
        len -= 4;
    }
    byte[] tmp = new byte[(int) len];
    b.get(tmp);
    if (compressed) {
        SlowInflate inf = new SlowInflate();
        tmp = inf.unpack(tmp, (int) dataLen);
    }
    field = new JSONString(Utf8.toString(tmp));
    return field;
}
Also used : SlowInflate(com.yahoo.io.SlowInflate) JSONString(com.yahoo.prelude.hitfield.JSONString)

Aggregations

JSONString (com.yahoo.prelude.hitfield.JSONString)19 Test (org.junit.Test)15 Inspector (com.yahoo.data.access.Inspector)8 Value (com.yahoo.data.access.simple.Value)7 XMLString (com.yahoo.prelude.hitfield.XMLString)4 Result (com.yahoo.search.Result)4 FastHit (com.yahoo.prelude.fastsearch.FastHit)3 Hit (com.yahoo.search.result.Hit)3 StructuredData (com.yahoo.search.result.StructuredData)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Chain (com.yahoo.component.chain.Chain)1 SlimeAdapter (com.yahoo.data.access.slime.SlimeAdapter)1 SlowInflate (com.yahoo.io.SlowInflate)1 RawData (com.yahoo.prelude.hitfield.RawData)1 Query (com.yahoo.search.Query)1 FeatureData (com.yahoo.search.result.FeatureData)1 Relevance (com.yahoo.search.result.Relevance)1 Execution (com.yahoo.search.searchchain.Execution)1