package io.vertx.lang.groovy;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.codehaus.groovy.runtime.typehandling.GroovyCastException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

Extension module for integrating Vert.x common types with Groovy.
/** * Extension module for integrating Vert.x common types with Groovy. */
public class VertxExtensionModule {
Convert a JSON like map data structure to a Vert.x data object or a JsonObject.

The data object is a class annotated by DataObject that provides a public constructor with a JsonObject argument.

Params:
  • map – the JSON like map like data structure
  • clazz – the target type which can be a Java class annotated with DataObject and provides a JsonObject constructor, or JsonObject
Returns:an instance of the specified clazz
/** * Convert a JSON like map data structure to a Vert.x data object or a {@link JsonObject}. * * <p> The data object is a class annotated by {@link DataObject} that provides a public constructor * with a {@link JsonObject} argument. * * @param map the JSON like map like data structure * @param clazz the target type which can be a Java class annotated with {@link DataObject} and provides a {@link JsonObject} * constructor, or {@link JsonObject} * @return an instance of the specified {@code clazz} */
public static Object asType(Map map, Class clazz) throws Throwable { if (clazz == JsonObject.class) { return new JsonObject(map); } else if (clazz.getAnnotation(DataObject.class) != null) { try { Constructor ctor = clazz.getConstructor(JsonObject.class); JsonObject json = new JsonObject(map); return ctor.newInstance(json); } catch (NoSuchMethodException e) { throw new GroovyCastException("Cannot cast JsonObject to class '" + clazz + "'"); } catch (InvocationTargetException e) { throw e.getCause(); } catch (Exception e) { throw e; } } return null; }
Subscript operator for JsonObject, the read part.
Params:
  • json – the json object
  • key – the key
Returns:the json value for key
/** * Subscript operator for {@link JsonObject}, the read part. * * @param json the json object * @param key the key * @return the json value for {@code key} */
public static Object getAt(JsonObject json, String key) { return json.getValue(key); }
Subscript operator for JsonObject, the write part.
Params:
  • json – the json object
  • key – the key
  • value – the json value
Returns:the previous json value for key
/** * Subscript operator for {@link JsonObject}, the write part. * * @param json the json object * @param key the key * @param value the json value * @return the previous json value for {@code key} */
public static Object putAt(JsonObject json, String key, Object value) { Object prev = json.getValue(key); json.put(key, value); return prev; }
Subscript operator for JsonArray, the read part.
Params:
  • json – the json array
  • idx – the idx
Returns:the json value for idx
/** * Subscript operator for {@link JsonArray}, the read part. * * @param json the json array * @param idx the idx * @return the json value for {@code idx} */
public static Object getAt(JsonArray json, int idx) { int size = json.size(); if (idx < 0) { idx += size; if (idx < 0) { throw new ArrayIndexOutOfBoundsException("Invalid array index " + (idx - size)); } } if (idx < size) { return json.getValue(idx); } else { return null; } }
<< operator for JsonObject.
Params:
  • json – the json object
  • value – the json value
/** * {@code <<} operator for {@link JsonObject}. * * @param json the json object * @param value the json value */
public static void leftShift(JsonArray json, Object value) { json.add(value); } }