Skip to content

Commit

Permalink
Merge pull request #171 from zapov/master
Browse files Browse the repository at this point in the history
DSL-JSON example integration
  • Loading branch information
nmihajlovski committed Aug 12, 2018
2 parents 9c77c01 + 72cb2ac commit ac161a6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/dsl-json-integration/pom.xml
Expand Up @@ -17,6 +17,11 @@
<artifactId>rapidoid-quick</artifactId>
<version>${rapidoid.version}</version>
</dependency>
<dependency>
<groupId>com.dslplatform</groupId>
<artifactId>dsl-json-java8</artifactId>
<version>1.7.4</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
@@ -0,0 +1,34 @@
package com.example;

import com.dslplatform.json.DslJson;
import com.dslplatform.json.runtime.Settings;
import org.rapidoid.RapidoidThing;
import org.rapidoid.http.Req;
import org.rapidoid.http.customize.BeanParameterFactory;
import org.rapidoid.http.customize.HttpRequestBodyParser;
import org.rapidoid.http.customize.HttpResponseRenderer;

import java.io.OutputStream;
import java.util.Map;

public class DslJsonConverter extends RapidoidThing implements HttpResponseRenderer, HttpRequestBodyParser, BeanParameterFactory {

private final DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime().includeServiceLoader());

@Override
public void render(Req req, Object value, OutputStream out) throws Exception {
dslJson.serialize(value, out);
}

@SuppressWarnings("unchecked")
@Override
public Map<String, ?> parseRequestBody(Req req, byte[] body) throws Exception {
return null;
}

@Override
public Object getParamValue(Req req, Class<?> paramType, String paramName, Map<String, Object> properties) throws Exception {
byte[] body = req.body();
return dslJson.deserialize(paramType, body, body.length);
}
}
@@ -1,17 +1,31 @@
package com.example;

import com.dslplatform.json.CompiledJson;
import org.rapidoid.setup.On;
import org.rapidoid.u.U;

public class DslJsonExample {

public static void main(String[] args) {
// a blank dsl-json integration example
DslJsonConverter dslJsonConverter = new DslJsonConverter();
On.custom().jsonResponseRenderer(dslJsonConverter);
On.custom().jsonRequestBodyParser(dslJsonConverter);
On.custom().beanParameterFactory(dslJsonConverter);

On.get("/hello").json(() -> U.map("msg", "Hello, world!"));
On.post("/reflection").json((HelloReflection h) -> { h.x = h.x * 2; return h; });
On.post("/compiled").json((HelloCompiled h) -> { h.x = h.x * 2; return h; });
}

// TODO integrate the dsl-json library
public static class HelloReflection {
public int x;
public String s;
}

@CompiledJson
public static class HelloCompiled {
public int x;
public String s;
}
}

Expand Up @@ -6,6 +6,7 @@
import org.rapidoid.test.RapidoidIntegrationTest;
import org.rapidoid.u.U;

import java.nio.charset.Charset;
import java.util.Map;

/**
Expand All @@ -23,4 +24,10 @@ public void testHelloWorld() {
eq(resp, U.map("msg", "Hello, world!"));
}

@Test
public void testPost() {
Map<String, Object> resp = Self.post("/compiled").body("{\"x\":3,\"s\":\"a\"}".getBytes(Charset.forName("UTF-8"))).toMap();

eq(resp, U.map("x", 6, "s", "a"));
}
}

0 comments on commit ac161a6

Please sign in to comment.