Example usage for com.fasterxml.jackson.jr.ob.comp ArrayComposer end

List of usage examples for com.fasterxml.jackson.jr.ob.comp ArrayComposer end

Introduction

In this page you can find the example usage for com.fasterxml.jackson.jr.ob.comp ArrayComposer end.

Prototype

public PARENT end() throws IOException, JsonProcessingException 

Source Link

Usage

From source file:com.asprise.imaging.core.Imaging.java

/**
 *
 * @param functionName/*from  ww w. j ava  2  s .  c o  m*/
 * @param args Must be of type: String, Integer or Boolean;
 * @return
 * @throws RuntimeException if failed to form request in JSON format.
 */
public static String callNativeFunc(String functionName, Object... args) {
    String jsonRequest = null;
    try {
        ArrayComposer<ObjectComposer<JSONComposer<String>>> argArray = JSON.std.composeString().startObject()
                .put("function", functionName).startArrayField("args");
        for (int i = 0; args != null && i < args.length; i++) {
            Object arg = args[i];
            if (arg instanceof String) {
                argArray = argArray.add((String) arg);
            } else if (arg instanceof Integer) {
                argArray = argArray.add(((Number) arg).intValue());
            } else if (arg instanceof Long) {
                argArray = argArray.add(((Number) arg).longValue());
            } else if (arg instanceof Boolean) {
                argArray = argArray.add(((Boolean) arg).booleanValue());
            } else if (arg == null) {
                argArray = argArray.addNull();
            } else {
                throw new IllegalArgumentException(
                        "Unsupported arg type: " + arg.getClass().getName() + "; object: " + arg);
            }
        }
        jsonRequest = argArray.end().end().finish();
    } catch (Throwable e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e);
        }
    }

    if (jsonRequest == null || jsonRequest.length() == 0) {
        throw new IllegalArgumentException("Invalid requst: " + jsonRequest);
    }

    // System.out.println(jsonRequest);
    String result = TwainNative.invokeFunc(jsonRequest);
    return result;
}