Search in sources :

Example 1 with SlimeAdapter

use of com.yahoo.data.access.slime.SlimeAdapter in project vespa by vespa-engine.

the class JSONStringTestCase method getSlime3.

private Inspector getSlime3() {
    Slime slime = new Slime();
    slime.setLong(123);
    return new SlimeAdapter(slime.get());
}
Also used : SlimeAdapter(com.yahoo.data.access.slime.SlimeAdapter) Slime(com.yahoo.slime.Slime)

Example 2 with SlimeAdapter

use of com.yahoo.data.access.slime.SlimeAdapter in project vespa by vespa-engine.

the class JSONStringTestCase method getSlime5.

private Inspector getSlime5() {
    Slime slime = new Slime();
    Cursor arr = slime.setArray();
    arr.addLong(1);
    arr.addLong(2);
    arr.addLong(3);
    return new SlimeAdapter(slime.get());
}
Also used : SlimeAdapter(com.yahoo.data.access.slime.SlimeAdapter) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor)

Example 3 with SlimeAdapter

use of com.yahoo.data.access.slime.SlimeAdapter 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 4 with SlimeAdapter

use of com.yahoo.data.access.slime.SlimeAdapter in project vespa by vespa-engine.

the class JSONString method inspect.

public Inspector inspect() {
    if (value == null) {
        JsonDecoder decoder = new JsonDecoder();
        Slime slime = decoder.decode(new Slime(), Utf8.toBytes(content));
        if (slime.get().field("error_message").valid() && slime.get().field("partial_result").valid() && slime.get().field("offending_input").valid()) {
            // probably a json parse error...
            value = new Value.StringValue(content);
        } else if (slime.get().type() == com.yahoo.slime.Type.OBJECT || slime.get().type() == com.yahoo.slime.Type.ARRAY) {
            // valid json object or array
            value = new SlimeAdapter(slime.get());
        } else {
            // 'valid' json, but leaf value
            value = new Value.StringValue(content);
        }
    }
    return value;
}
Also used : JsonDecoder(com.yahoo.slime.JsonDecoder) SlimeAdapter(com.yahoo.data.access.slime.SlimeAdapter) Value(com.yahoo.data.access.simple.Value) Slime(com.yahoo.slime.Slime)

Example 5 with SlimeAdapter

use of com.yahoo.data.access.slime.SlimeAdapter in project vespa by vespa-engine.

the class DocsumDefinitionSet method lazyDecode.

/**
 * Makes data available for decoding for the given hit.
 *
 * @param summaryClass the requested summary class
 * @param data docsum data from backend
 * @param hit the Hit corresponding to this document summary
 * @return Error message or null on success.
 * @throws ConfigurationException if the summary class of this hit is missing
 */
public final String lazyDecode(String summaryClass, byte[] data, FastHit hit) {
    ByteBuffer buffer = ByteBuffer.wrap(data);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    long docsumClassId = buffer.getInt();
    if (docsumClassId != SLIME_MAGIC_ID) {
        throw new IllegalArgumentException("Only expecting SchemaLess docsums - summary class:" + summaryClass + " hit:" + hit);
    }
    DocsumDefinition docsumDefinition = lookupDocsum(summaryClass);
    Slime value = BinaryFormat.decode(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    Inspector docsum = new SlimeAdapter(value.get());
    if (docsum.type() != OBJECT) {
        return "Hit " + hit + " failed: " + docsum.asString();
    }
    hit.addSummary(docsumDefinition, docsum);
    return null;
}
Also used : SlimeAdapter(com.yahoo.data.access.slime.SlimeAdapter) Inspector(com.yahoo.data.access.Inspector) Slime(com.yahoo.slime.Slime) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SlimeAdapter (com.yahoo.data.access.slime.SlimeAdapter)8 Slime (com.yahoo.slime.Slime)8 Cursor (com.yahoo.slime.Cursor)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Inspector (com.yahoo.data.access.Inspector)1 Value (com.yahoo.data.access.simple.Value)1 FastHit (com.yahoo.prelude.fastsearch.FastHit)1 JSONString (com.yahoo.prelude.hitfield.JSONString)1 Result (com.yahoo.search.Result)1 Hit (com.yahoo.search.result.Hit)1 StructuredData (com.yahoo.search.result.StructuredData)1 JsonDecoder (com.yahoo.slime.JsonDecoder)1 ByteBuffer (java.nio.ByteBuffer)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1