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());
}
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());
}
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);
}
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;
}
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;
}
Aggregations