-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from zapov/master
DSL-JSON example integration
- Loading branch information
Showing
4 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
examples/dsl-json-integration/src/main/java/com/example/DslJsonConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
18 changes: 16 additions & 2 deletions
18
examples/dsl-json-integration/src/main/java/com/example/DslJsonExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters