Search in sources :

Example 1 with JsonDecoder

use of com.yahoo.slime.JsonDecoder in project vespa by vespa-engine.

the class JRTConfigRequestBase method error_response_adds_common_elements.

@Test
public void error_response_adds_common_elements() {
    serverReq.addErrorResponse(ErrorCode.APPLICATION_NOT_LOADED, ErrorCode.getName(ErrorCode.APPLICATION_NOT_LOADED));
    assertThat(serverReq.getRequest().returnValues().size(), is(1));
    Slime data = new JsonDecoder().decode(new Slime(), Utf8.toBytes(serverReq.getRequest().returnValues().get(0).asString()));
    Inspector response = data.get();
    assertThat(response.field(SlimeResponseData.RESPONSE_DEF_NAME).asString(), is(defName));
    assertThat(response.field(SlimeResponseData.RESPONSE_DEF_NAMESPACE).asString(), is(defNamespace));
    assertThat(response.field(SlimeResponseData.RESPONSE_DEF_MD5).asString(), is(defMd5));
    assertThat(response.field(SlimeResponseData.RESPONSE_CONFIGID).asString(), is(configId));
    assertThat(response.field(SlimeResponseData.RESPONSE_CLIENT_HOSTNAME).asString(), is(hostname));
    Trace t = Trace.fromSlime(response.field(SlimeResponseData.RESPONSE_TRACE));
    assertThat(t.toString(), is(trace.toString()));
}
Also used : JsonDecoder(com.yahoo.slime.JsonDecoder) Inspector(com.yahoo.slime.Inspector) Slime(com.yahoo.slime.Slime) Test(org.junit.Test)

Example 2 with JsonDecoder

use of com.yahoo.slime.JsonDecoder in project vespa by vespa-engine.

the class ConfigVerification method listConfigs.

private static Map<String, Stack<String>> listConfigs(List<String> urls) throws IOException {
    Map<String, String> outputs = performRequests(urls);
    Map<String, Stack<String>> recurseMappings = new LinkedHashMap<>();
    for (Map.Entry<String, String> entry : outputs.entrySet()) {
        Slime slime = new JsonDecoder().decode(new Slime(), Utf8.toBytes(entry.getValue()));
        final List<String> list = new ArrayList<>();
        slime.get().field("configs").traverse(new ArrayTraverser() {

            @Override
            public void entry(int idx, Inspector inspector) {
                list.add(inspector.asString());
            }
        });
        Stack<String> stack = new Stack<>();
        Collections.sort(list);
        stack.addAll(list);
        recurseMappings.put(entry.getKey(), stack);
    }
    return recurseMappings;
}
Also used : Slime(com.yahoo.slime.Slime) JsonDecoder(com.yahoo.slime.JsonDecoder) Inspector(com.yahoo.slime.Inspector) ArrayTraverser(com.yahoo.slime.ArrayTraverser)

Example 3 with JsonDecoder

use of com.yahoo.slime.JsonDecoder in project vespa by vespa-engine.

the class SessionPrepareHandlerTest method getData.

private Slime getData(HttpResponse response) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    response.render(baos);
    Slime data = new Slime();
    new JsonDecoder().decode(data, baos.toByteArray());
    return data;
}
Also used : JsonDecoder(com.yahoo.slime.JsonDecoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Slime(com.yahoo.slime.Slime)

Example 4 with JsonDecoder

use of com.yahoo.slime.JsonDecoder 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 JsonDecoder

use of com.yahoo.slime.JsonDecoder in project vespa by vespa-engine.

the class HttpHandlerTest method testResponse.

@Test
public void testResponse() throws IOException {
    final String message = "failed";
    HttpHandler httpHandler = new HttpTestHandler(new InvalidApplicationException(message));
    HttpResponse response = httpHandler.handle(HttpRequest.createTestRequest("foo", com.yahoo.jdisc.http.HttpRequest.Method.GET));
    assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    response.render(baos);
    Slime data = new Slime();
    new JsonDecoder().decode(data, baos.toByteArray());
    assertThat(data.get().field("error-code").asString(), is(HttpErrorResponse.errorCodes.INVALID_APPLICATION_PACKAGE.name()));
    assertThat(data.get().field("message").asString(), is(message));
}
Also used : JsonDecoder(com.yahoo.slime.JsonDecoder) HttpResponse(com.yahoo.container.jdisc.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Slime(com.yahoo.slime.Slime) Test(org.junit.Test)

Aggregations

JsonDecoder (com.yahoo.slime.JsonDecoder)6 Slime (com.yahoo.slime.Slime)6 Inspector (com.yahoo.slime.Inspector)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.Test)2 HttpResponse (com.yahoo.container.jdisc.HttpResponse)1 Value (com.yahoo.data.access.simple.Value)1 SlimeAdapter (com.yahoo.data.access.slime.SlimeAdapter)1 ArrayTraverser (com.yahoo.slime.ArrayTraverser)1 ArrayList (java.util.ArrayList)1