Search in sources :

Example 1 with JsonUrl

use of com.thoughtworks.go.util.json.JsonUrl in project gocd by gocd.

the class DirectoryEntry method toJson.

@Override
public Map<String, Object> toJson() {
    Map<String, Object> fileOrFolder = new LinkedHashMap<>();
    fileOrFolder.put("name", fileName);
    fileOrFolder.put("url", new JsonUrl(url));
    fileOrFolder.put("type", type);
    return fileOrFolder;
}
Also used : JsonUrl(com.thoughtworks.go.util.json.JsonUrl) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with JsonUrl

use of com.thoughtworks.go.util.json.JsonUrl in project gocd by gocd.

the class JsonViewTest method testShouldRenderUrlsWithContext.

@Test
public void testShouldRenderUrlsWithContext() {
    JsonUrl url = new JsonUrl("/foo/bar/baz");
    when(requestContext.getFullRequestPath()).thenReturn("http://something/context");
    String json = render(url, requestContext);
    assertThat(json, is("\"http://something/context/foo/bar/baz\""));
}
Also used : JsonUrl(com.thoughtworks.go.util.json.JsonUrl) Test(org.junit.jupiter.api.Test)

Example 3 with JsonUrl

use of com.thoughtworks.go.util.json.JsonUrl in project gocd by gocd.

the class JsonRenderer method gsonBuilder.

private static Gson gsonBuilder(final GoRequestContext requestContext) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonUrl.class, (JsonSerializer<JsonUrl>) (src, typeOfSrc, context) -> {
        if (requestContext == null) {
            return new JsonPrimitive(src.getUrl());
        } else {
            return new JsonPrimitive(requestContext.getFullRequestPath() + src.getUrl());
        }
    });
    builder.registerTypeHierarchyAdapter(MessageSourceResolvable.class, (JsonSerializer<MessageSourceResolvable>) (src, typeOfSrc, context) -> {
        if (requestContext == null) {
            return new JsonPrimitive(src.getDefaultMessage());
        } else {
            return new JsonPrimitive(requestContext.getMessage(src));
        }
    });
    builder.serializeNulls();
    return builder.create();
}
Also used : Gson(com.google.gson.Gson) StringWriter(java.io.StringWriter) Writer(java.io.Writer) JsonFakeMap(com.thoughtworks.go.util.json.JsonFakeMap) JsonUrl(com.thoughtworks.go.util.json.JsonUrl) JsonSerializer(com.google.gson.JsonSerializer) JsonPrimitive(com.google.gson.JsonPrimitive) JsonAware(com.thoughtworks.go.util.json.JsonAware) MessageSourceResolvable(org.springframework.context.MessageSourceResolvable) GsonBuilder(com.google.gson.GsonBuilder) MessageSourceResolvable(org.springframework.context.MessageSourceResolvable) GsonBuilder(com.google.gson.GsonBuilder) JsonPrimitive(com.google.gson.JsonPrimitive) JsonUrl(com.thoughtworks.go.util.json.JsonUrl)

Aggregations

JsonUrl (com.thoughtworks.go.util.json.JsonUrl)3 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSerializer (com.google.gson.JsonSerializer)1 JsonAware (com.thoughtworks.go.util.json.JsonAware)1 JsonFakeMap (com.thoughtworks.go.util.json.JsonFakeMap)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.jupiter.api.Test)1 MessageSourceResolvable (org.springframework.context.MessageSourceResolvable)1