Java Code Examples for com.fasterxml.jackson.annotation.JsonRootName

The following are top voted examples for showing how to use com.fasterxml.jackson.annotation.JsonRootName. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: cros-core   File: BasestationController.java   View source code 6 votes vote down vote up
@Authentication({User.Role.ADMIN, User.Role.READONLY_ADMIN})
public static Result getAll() {
    ExpressionList<Basestation> exp = QueryHelper.buildQuery(Basestation.class, Basestation.FIND.where());

    List<JsonHelper.Tuple> tuples = exp.findList().stream().map(basestation -> new JsonHelper.Tuple(basestation, new ControllerHelper.Link("self",
            controllers.routes.BasestationController.get(basestation.getId()).absoluteURL(request())))).collect(Collectors.toList());

    // TODO: add links when available
    List<ControllerHelper.Link> links = new ArrayList<>();
    links.add(new ControllerHelper.Link("self", controllers.routes.BasestationController.getAll().absoluteURL(request())));
    links.add(new ControllerHelper.Link("total", controllers.routes.BasestationController.getTotal().absoluteURL(request())));

    try {
        JsonNode result = JsonHelper.createJsonNode(tuples, links, Basestation.class);
        String[] totalQuery = request().queryString().get("total");
        if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) {
            ExpressionList<Basestation> countExpression = QueryHelper.buildQuery(Basestation.class, Basestation.FIND.where(), true);
            String root = Basestation.class.getAnnotation(JsonRootName.class).value();
            ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount());
        }
        return ok(result);
    } catch(JsonProcessingException ex) {
        play.Logger.error(ex.getMessage(), ex);
        return internalServerError();
    }
}
 
Example 2
Project: cros-core   File: AssignmentController.java   View source code 6 votes vote down vote up
@Authentication({User.Role.READONLY_ADMIN, User.Role.ADMIN})
public static Result getAll() {
    ExpressionList<Assignment> exp = QueryHelper.buildQuery(Assignment.class, Assignment.FIND.where());

    List<JsonHelper.Tuple> tuples = exp.findList().stream().map(assignment -> new JsonHelper.Tuple(assignment, new ControllerHelper.Link("self",
            controllers.routes.AssignmentController.get(assignment.getId()).absoluteURL(request())))).collect(Collectors.toList());

    // TODO: add links when available
    List<ControllerHelper.Link> links = new ArrayList<>();
    links.add(new ControllerHelper.Link("self", controllers.routes.AssignmentController.getAll().absoluteURL(request())));
    links.add(new ControllerHelper.Link("total", controllers.routes.AssignmentController.getTotal().absoluteURL(request())));

    try {
        JsonNode result = JsonHelper.createJsonNode(tuples, links, Assignment.class);
        String[] totalQuery = request().queryString().get("total");
        if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) {
            ExpressionList<Assignment> countExpression = QueryHelper.buildQuery(Assignment.class, Assignment.FIND.where(), true);
            String root = Assignment.class.getAnnotation(JsonRootName.class).value();
            ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount());
        }
        return ok(result);
    } catch(JsonProcessingException ex) {
        play.Logger.error(ex.getMessage(), ex);
        return internalServerError();
    }
}
 
Example 3
Project: Stud.IP-FileSync   File: JacksonRequest.java   View source code 6 votes vote down vote up
/**
 * Parse response into data model object.
 * 
 * @param unwrap Unwrap datamodel
 * @return Response datamodel
 * @throws IOException
 */
public T parseResponse() throws IOException {
	ObjectReader reader = MAPPER.readerFor(datamodel).without(Feature.AUTO_CLOSE_SOURCE);
	if (datamodel.isAnnotationPresent(JsonRootName.class)) {
		reader = reader.with(DeserializationFeature.UNWRAP_ROOT_VALUE);
	}

	try (final InputStream is = response.getStream()) {
		final T result = reader.readValue(is);

		/*
		 * ObjectReader.readValue might not consume the entire inputstream,
		 * we skip everything after the json root element.
		 * This is needed for proper http connection reuse (keep alive).
		 */
		is.skip(Long.MAX_VALUE);

		return result;
	}
}
 
Example 4
Project: gwt-jackson   File: ObjectMapperCreator.java   View source code 6 votes vote down vote up
/**
 * Build the constructor.
 *
 * @param mappedTypeClass the type to map
 *
 * @return the constructor method
 */
private MethodSpec buildConstructor( JClassType mappedTypeClass ) {
    Optional<JsonRootName> jsonRootName
            = findFirstEncounteredAnnotationsOnAllHierarchy( configuration, mappedTypeClass, JsonRootName.class );
    String rootName;
    if ( !jsonRootName.isPresent() || Strings.isNullOrEmpty( jsonRootName.get().value() ) ) {
        rootName = mappedTypeClass.getSimpleSourceName();
    } else {
        rootName = jsonRootName.get().value();
    }

    return MethodSpec.constructorBuilder()
            .addModifiers( Modifier.PUBLIC )
            .addStatement( "super($S)", rootName )
            .build();
}
 
Example 5
Project: cros-core   File: UserController.java   View source code 5 votes vote down vote up
@Authentication({User.Role.ADMIN, User.Role.READONLY_ADMIN})
public static Result getAll() {
    ExpressionList<User> exp = QueryHelper.buildQuery(User.class, User.FIND.where());

    List<JsonHelper.Tuple> tuples = exp.findList().stream().map(user -> new JsonHelper.Tuple(user, new ControllerHelper.Link("self",
            controllers.routes.UserController.get(user.getId()).absoluteURL(request())))).collect(Collectors.toList());

    // TODO: add links when available
    List<ControllerHelper.Link> links = new ArrayList<>();
    links.add(new ControllerHelper.Link("self", controllers.routes.UserController.getAll().absoluteURL(request())));
    links.add(new ControllerHelper.Link("total", controllers.routes.UserController.getTotal().absoluteURL(request())));
    links.add(new ControllerHelper.Link("me", controllers.routes.UserController.currentUser().absoluteURL(request())));

    try {

        JsonNode result = JsonHelper.createJsonNode(tuples, links, User.class);
        String[] totalQuery = request().queryString().get("total");
        if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) {
            ExpressionList<User> countExpression = QueryHelper.buildQuery(User.class, User.FIND.where(), true);
            String root = User.class.getAnnotation(JsonRootName.class).value();
            ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount());
        }
        return ok(result);
    } catch(JsonProcessingException ex) {
        play.Logger.error(ex.getMessage(), ex);
        return internalServerError();
    }
}
 
Example 6
Project: cros-core   File: DroneController.java   View source code 5 votes vote down vote up
@Authentication({User.Role.ADMIN, User.Role.READONLY_ADMIN})
public static Result getAll() {
    ExpressionList<Drone> exp = QueryHelper.buildQuery(Drone.class, Drone.FIND.where(),false);

    List<JsonHelper.Tuple> tuples = exp.findList().stream().map(drone -> new JsonHelper.Tuple(drone, new ControllerHelper.Link("self",
            controllers.routes.DroneController.get(drone.getId()).absoluteURL(request())))).collect(Collectors.toList());

    // TODO: add links when available
    List<ControllerHelper.Link> links = new ArrayList<>();
    links.add(new ControllerHelper.Link("self", controllers.routes.DroneController.getAll().absoluteURL(request())));
    links.add(new ControllerHelper.Link("total", controllers.routes.DroneController.getTotal().absoluteURL(request())));
    links.add(new ControllerHelper.Link("types", controllers.routes.DroneController.getSuportedTypes().absoluteURL(request())));

    try {
        JsonNode result = JsonHelper.createJsonNode(tuples, links, Drone.class);
        String[] totalQuery = request().queryString().get("total");
        if (totalQuery != null && totalQuery.length == 1 && totalQuery[0].equals("true")) {
            ExpressionList<Drone> countExpression = QueryHelper.buildQuery(Drone.class, Drone.FIND.where(), true);
            String root = Drone.class.getAnnotation(JsonRootName.class).value();
            ((ObjectNode) result.get(root)).put("total",countExpression.findRowCount());
        }
        return ok(result);
    } catch(JsonProcessingException ex) {
        play.Logger.error(ex.getMessage(), ex);
        return internalServerError();
    }
}
 
Example 7
Project: cros-core   File: JsonHelper.java   View source code 5 votes vote down vote up
public static JsonNode removeRootElement(JsonNode node, Class clazz, boolean isList) throws InvalidJSONException {
    JsonRootName annotation = (JsonRootName) clazz.getAnnotation(JsonRootName.class);
    String rootElement = annotation.value();
    JsonNode rootNode = isList ? node.get(rootElement).get(DEFAULT_ROOT_ELEMENT) : node.get(rootElement);
    if(rootNode == null)
        throw new InvalidJSONException("Invalid json: no such element (" +
                (isList ? DEFAULT_ROOT_ELEMENT : annotation.value()) + ")");
    return rootNode;
}
 
Example 8
Project: cros-core   File: JsonHelper.java   View source code 5 votes vote down vote up
public static JsonNode addRootElement(JsonNode node, Class clazz) {
    JsonRootName annotation = (JsonRootName) clazz.getAnnotation(JsonRootName.class);
    String rootElement = annotation.value();
    ObjectNode nodeWithRoot = Json.newObject();
    nodeWithRoot.put(rootElement, node);
    return nodeWithRoot;
}
 
Example 9
Project: yona-server   File: JsonRootRelProvider.java   View source code 5 votes vote down vote up
@Override
public String getItemResourceRelFor(Class<?> type)
{
	Class<?> baseType = determineBaseType(type);
	JsonRootName rootName = getAnnotationByType(baseType, JsonRootName.class);
	return (rootName == null) ? defaultRelProvider.getItemResourceRelFor(baseType) : rootName.value();
}
 
Example 10
Project: QuizUpWinner   File: JacksonAnnotationIntrospector.java   View source code 5 votes vote down vote up
public PropertyName findRootName(AnnotatedClass paramAnnotatedClass)
{
  JsonRootName localJsonRootName = (JsonRootName)paramAnnotatedClass.getAnnotation(JsonRootName.class);
  if (localJsonRootName == null)
    return null;
  return new PropertyName(localJsonRootName.value());
}
 
Example 11
Project: featuros   File: JsonRootAwareRelProvider.java   View source code 4 votes vote down vote up
private Optional<String> fromJsonRoot(Class<?> type) {
    return stream(type.getAnnotationsByType(JsonRootName.class))
            .findFirst()
            .map(JsonRootName::value);
}
 
Example 12
Project: monasca-common   File: Serialization.java   View source code 4 votes vote down vote up
private static String rootNameFor(Class<?> type) {
  JsonRootName rootName = type.getAnnotation(JsonRootName.class);
  return rootName == null ? type.getSimpleName() : rootName.value();
}