Search in sources :

Example 1 with JsonBuilder

use of groovy.json.JsonBuilder in project AJSC by att.

the class VandelayServiceTest method createJson.

public String createJson() {
    HashMap<String, String> jsonHash = new HashMap<>();
    jsonHash.put("contextClass", "ajsc.Context");
    jsonHash.put("contextName", "demo");
    jsonHash.put("contextVersion", "v1");
    return new JsonBuilder(jsonHash).toString();
}
Also used : JsonBuilder(groovy.json.JsonBuilder) HashMap(java.util.HashMap)

Example 2 with JsonBuilder

use of groovy.json.JsonBuilder in project rest-assured by rest-assured.

the class EncoderRegistry method encodeJSON.

/**
 * <p>Accepts a Collection or a JavaBean object which is converted to JSON.
 * A Map or Collection will be converted to a {@link JsonBuilder}..  A
 * String or GString will be interpreted as valid JSON and passed directly
 * as the request body (with charset conversion if necessary.)</p>
 * <p/>
 * <p>If a Closure is passed as the model, it will be executed as if it were
 * a JSON object definition passed to a {@link JsonBuilder}.  In order
 * for the closure to be interpreted correctly, there must be a 'root'
 * element immediately inside the closure.  For example:</p>
 * <p/>
 * <pre>builder.post( JSON ) {
 *   body = {
 *     root {
 *       first {
 *         one = 1
 *         two = '2'
 *       }
 *       second = 'some string'
 *     }
 *   }
 * }</pre>
 * <p> will return the following JSON string:<pre>
 * {"root":{"first":{"one":1,"two":"2"},"second":"some string"}}</pre></p>
 *
 * @param model data to be converted to JSON, as specified above.
 * @return an {@link HttpEntity} encapsulating this request data
 * @throws UnsupportedEncodingException
 */
@SuppressWarnings("unchecked")
public HttpEntity encodeJSON(Object contentType, Object model) throws UnsupportedEncodingException {
    String contentTypeAsString = contentTypeToString(contentType);
    Object json;
    if (model instanceof Map || model instanceof Collection) {
        json = new JsonBuilder(model);
    } else if (model instanceof Closure) {
        Closure closure = (Closure) model;
        closure.setDelegate(new JsonBuilder());
        json = closure.call();
    } else if (model instanceof String || model instanceof GString || model instanceof byte[]) {
        // assume valid JSON already.
        json = model;
    } else if (model instanceof File) {
        json = toString((File) model, contentTypeAsString);
    } else {
        throw new UnsupportedOperationException("Internal error: Can't encode " + model + " to JSON.");
    }
    return createEntity(contentTypeAsString, json);
}
Also used : JsonBuilder(groovy.json.JsonBuilder) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) FileReader.readToString(io.restassured.internal.support.FileReader.readToString) GString(groovy.lang.GString) GString(groovy.lang.GString)

Example 3 with JsonBuilder

use of groovy.json.JsonBuilder in project gradle by gradle.

the class JsonProjectDependencyRenderer method render.

/**
 * Generates the project dependency report structure
 *
 * @param project the project for which the report must be generated
 * @return the generated JSON, as a String
 */
public String render(Project project) {
    JsonBuilder json = new JsonBuilder();
    renderProject(project, json);
    return json.toString();
}
Also used : JsonBuilder(groovy.json.JsonBuilder)

Example 4 with JsonBuilder

use of groovy.json.JsonBuilder in project hale by halestudio.

the class ValueListTypeTest method testValueListValueListJsonGroovy.

/**
 * Test if a nested list is the same when converted to JSON and back again.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testValueListValueListJsonGroovy() throws Exception {
    ValueList values1 = new ValueList();
    values1.add(Value.of(1));
    values1.add(Value.of(2));
    ValueList values2 = new ValueList();
    values2.add(Value.of("a"));
    values2.add(Value.of("b"));
    values2.add(Value.of("c"));
    ValueList values = new ValueList();
    values.add(new ComplexValue(values1));
    values.add(new ComplexValue(values2));
    // converter
    ValueListType vlt = new ValueListType();
    // convert to Json
    Object json = vlt.toJson(values);
    System.out.println(new JsonBuilder(json).toString());
    // convert back
    ValueList conv = vlt.fromJson(json, null);
    assertEquals("List size does not match", 2, conv.size());
    assertEquals(values, conv);
}
Also used : JsonBuilder(groovy.json.JsonBuilder) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Test(org.junit.Test)

Example 5 with JsonBuilder

use of groovy.json.JsonBuilder in project hale by halestudio.

the class LookupTableTypeTest method testComplexLookupJsonGroovy.

/**
 * Test if a lookup table containing only complex values is the same when
 * converted to JSON and back again.
 */
@Test
public void testComplexLookupJsonGroovy() {
    Map<Value, Value> values2 = createComplexLookup();
    LookupTable org2 = new LookupTableImpl(values2);
    // converter
    LookupTableType ltt = new LookupTableType();
    // convert to Json
    Object json = ltt.toJson(org2);
    System.out.println(new JsonBuilder(json).toString());
    // convert back
    LookupTable conv = ltt.fromJson(json, null);
    checkTable(conv, values2);
}
Also used : JsonBuilder(groovy.json.JsonBuilder) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Aggregations

JsonBuilder (groovy.json.JsonBuilder)6 Test (org.junit.Test)2 Value (eu.esdihumboldt.hale.common.core.io.Value)1 ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)1 LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)1 LookupTableImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)1 Closure (groovy.lang.Closure)1 GString (groovy.lang.GString)1 FileReader.readToString (io.restassured.internal.support.FileReader.readToString)1 HashMap (java.util.HashMap)1 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)1