Skip to content
Ravi Chodavarapu edited this page Jun 9, 2016 · 4 revisions

A particular type of Value (see Values) in datamill is a JSON object, created using the foundation.stack.datamill.json.JsonObject class. This class offers a fluent API to allow you to quickly create a JSON object in Java:

    JsonObject json = new JsonObject()
        .put("name", "Ravi Chodavarapu")
        .put("email", "rchodava@gmail.com")
        .put("male", true);
            
    System.out.println(json.asString());

You can also build JSON objects easily in various parts of the framework where it may be useful. For example, when working with a HTTP request entity, you can retrieve it as a JSON object:

    rb.ifMethodAndUriMatch(Method.POST, "/users", request ->
        request.entity().asJson()
            .map(json -> json.get(outline.name(outline.members().getEmail()).asString())                 
            .flatMap(email -> 
                "rchodava@gmail.com".equals(email) ? 
                    request.respond().ok() :
                    request.respond().notAuthorized());

Note also that getting JSON object properties using the get(...) method returns a Value which can be easily cast to various types - in this case, we cast it to a string using asString().

Clone this wiki locally