Example usage for org.apache.thrift TSerializer toString

List of usage examples for org.apache.thrift TSerializer toString

Introduction

In this page you can find the example usage for org.apache.thrift TSerializer toString.

Prototype

public String toString(TBase base, String charset) throws TException 

Source Link

Document

Serialize the Thrift object into a Java string, using a specified character set for encoding.

Usage

From source file:com.linecorp.armeria.server.docs.FunctionInfo.java

License:Apache License

static FunctionInfo of(Method method, Map<Class<?>, ? extends TBase<?, ?>> sampleRequests,
        @Nullable String namespace, Map<String, String> docStrings) throws ClassNotFoundException {
    requireNonNull(method, "method");

    final String methodName = method.getName();

    final Class<?> serviceClass = method.getDeclaringClass().getDeclaringClass();
    final String serviceName = serviceClass.getName();
    final ClassLoader classLoader = serviceClass.getClassLoader();

    @SuppressWarnings("unchecked")
    Class<? extends TBase<?, ?>> argsClass = (Class<? extends TBase<?, ?>>) Class
            .forName(serviceName + '$' + methodName + "_args", false, classLoader);
    String sampleJsonRequest;/*from w  ww  .j av  a2 s .  com*/
    TBase<?, ?> sampleRequest = sampleRequests.get(argsClass);
    if (sampleRequest == null) {
        sampleJsonRequest = "";
    } else {
        TSerializer serializer = new TSerializer(ThriftProtocolFactories.TEXT);
        try {
            sampleJsonRequest = serializer.toString(sampleRequest, StandardCharsets.UTF_8.name());
        } catch (TException e) {
            throw new IllegalArgumentException(
                    "Failed to serialize to a memory buffer, this shouldn't ever happen.", e);
        }
    }

    @SuppressWarnings("unchecked")
    final FunctionInfo function = new FunctionInfo(namespace, methodName, argsClass,
            (Class<? extends TBase<?, ?>>) Class.forName(serviceName + '$' + methodName + "_result", false,
                    classLoader),
            (Class<? extends TException>[]) method.getExceptionTypes(), sampleJsonRequest, docStrings);
    return function;
}

From source file:com.linecorp.armeria.server.thrift.ThriftDocServicePlugin.java

License:Apache License

@Override
public Optional<String> serializeExampleRequest(String serviceName, String methodName, Object exampleRequest) {
    if (!(exampleRequest instanceof TBase)) {
        return Optional.empty();
    }//  w ww  .  j  av  a  2s . co m

    final TBase<?, ?> exampleTBase = (TBase<?, ?>) exampleRequest;
    final TSerializer serializer = new TSerializer(ThriftProtocolFactories.TEXT);
    try {
        return Optional.of(serializer.toString(exampleTBase, StandardCharsets.UTF_8.name()));
    } catch (TException e) {
        throw new Error("should never reach here", e);
    }
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGenerator.java

License:Apache License

private static FunctionInfo newFunctionInfo(Method method, Map<Class<?>, ? extends TBase<?, ?>> sampleRequests,
        @Nullable String namespace, Map<String, String> docStrings) throws ClassNotFoundException {
    requireNonNull(method, "method");

    final String methodName = method.getName();

    final Class<?> serviceClass = method.getDeclaringClass().getDeclaringClass();
    final String serviceName = serviceClass.getName();
    final ClassLoader classLoader = serviceClass.getClassLoader();

    @SuppressWarnings("unchecked")
    Class<? extends TBase<?, ?>> argsClass = (Class<? extends TBase<?, ?>>) Class
            .forName(serviceName + '$' + methodName + "_args", false, classLoader);
    String sampleJsonRequest;/*ww  w .j  a  v  a2s .c  o  m*/
    TBase<?, ?> sampleRequest = sampleRequests.get(argsClass);
    if (sampleRequest == null) {
        sampleJsonRequest = "";
    } else {
        TSerializer serializer = new TSerializer(ThriftProtocolFactories.TEXT);
        try {
            sampleJsonRequest = serializer.toString(sampleRequest, StandardCharsets.UTF_8.name());
        } catch (TException e) {
            throw new IllegalArgumentException(
                    "Failed to serialize to a memory buffer, this shouldn't ever happen.", e);
        }
    }

    Class<?> resultClass;
    try {
        resultClass = Class.forName(serviceName + '$' + methodName + "_result", false, classLoader);
    } catch (ClassNotFoundException ignored) {
        // Oneway function does not have a result type.
        resultClass = null;
    }

    @SuppressWarnings("unchecked")
    final FunctionInfo function = newFunctionInfo(namespace, methodName, argsClass,
            (Class<? extends TBase<?, ?>>) resultClass,
            (Class<? extends TException>[]) method.getExceptionTypes(), sampleJsonRequest, docStrings);
    return function;
}

From source file:ezbake.services.search.utils.SSRUtils.java

License:Apache License

public static String convertThriftToJson(TBase thriftObject) throws TException {
    TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
    return serializer.toString(thriftObject, "UTF-8");
}

From source file:org.apache.hadoop.hive.metastore.messaging.json.JSONMessageFactory.java

License:Apache License

static String createTableObjJson(Table tableObj) throws TException {
    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
    return serializer.toString(tableObj, "UTF-8");
}

From source file:org.apache.hadoop.hive.metastore.messaging.json.JSONMessageFactory.java

License:Apache License

static String createPartitionObjJson(Partition partitionObj) throws TException {
    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
    return serializer.toString(partitionObj, "UTF-8");
}

From source file:org.apache.hadoop.hive.metastore.messaging.json.JSONMessageFactory.java

License:Apache License

static String createFunctionObjJson(Function functionObj) throws TException {
    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
    return serializer.toString(functionObj, "UTF-8");
}

From source file:org.apache.hadoop.hive.metastore.messaging.json.JSONMessageFactory.java

License:Apache License

static String createIndexObjJson(Index indexObj) throws TException {
    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
    return serializer.toString(indexObj, "UTF-8");
}

From source file:org.apache.hadoop.hive.metastore.messaging.MessageBuilder.java

License:Apache License

public static String createPrimaryKeyObjJson(SQLPrimaryKey primaryKeyObj) throws TException {
    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
    return serializer.toString(primaryKeyObj, "UTF-8");
}

From source file:org.apache.hadoop.hive.metastore.messaging.MessageBuilder.java

License:Apache License

public static String createForeignKeyObjJson(SQLForeignKey foreignKeyObj) throws TException {
    TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
    return serializer.toString(foreignKeyObj, "UTF-8");
}