Example usage for com.google.gson GsonBuilder GsonBuilder

List of usage examples for com.google.gson GsonBuilder GsonBuilder

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder GsonBuilder.

Prototype

public GsonBuilder() 

Source Link

Document

Creates a GsonBuilder instance that can be used to build Gson with various configuration settings.

Usage

From source file:client.model.Client.java

public static String toJson(List<Client> list) {
    Gson gson = new GsonBuilder().setDateFormat(Utility.DATE_FORMAT_STRING_SHORT).create();
    String gsonString = gson.toJson(list, new TypeToken<List<Client>>() {
    }.getType());//from   w  ww . j  a  va2  s  .  c  o m
    return gsonString;
}

From source file:client.model.Client.java

public static List<Client> fromJson(String json) throws JsonSyntaxException {
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new Utility.JsonDateDeserializer()).create();
    List<Client> list = gson.fromJson(json, new TypeToken<List<Client>>() {
    }.getType());//from w  w  w  .j av  a  2  s .com
    return list;
}

From source file:client.model.Service.java

public static String toJson(List<Service> list) {
    Gson gson = new GsonBuilder().setDateFormat(Utility.DATE_FORMAT_STRING_SHORT).create();
    String gsonString = gson.toJson(list, new TypeToken<List<Service>>() {
    }.getType());/*from w w  w.j ava  2 s .c o  m*/
    return gsonString;
}

From source file:client.model.Service.java

public static List<Service> fromJson(String json) throws JsonSyntaxException {
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();
    List<Service> list = gson.fromJson(json, new TypeToken<List<Service>>() {
    }.getType());/*from www. j  a  va 2s .  co  m*/
    return list;
}

From source file:client.model.Tower.java

public static String toJson(List<Tower> list) {
    Gson gson = new GsonBuilder().setDateFormat(Utility.DATE_FORMAT_STRING_SHORT).create();
    String gsonString = gson.toJson(list, new TypeToken<List<Tower>>() {
    }.getType());/*  w w  w  . j  ava 2 s. c  o m*/
    return gsonString;
}

From source file:client.model.Tower.java

public static List<Tower> fromJson(String json) throws JsonSyntaxException {
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new Utility.JsonDateDeserializer()).create();
    List<Tower> list = gson.fromJson(json, new TypeToken<List<Tower>>() {
    }.getType());// w w  w  .j a  va2 s .c om
    return list;
}

From source file:client.model.User.java

public static String toJsonUser(List<User> list) {
    Gson gson = new GsonBuilder().setDateFormat(Utility.DATE_FORMAT_STRING_SHORT).create();
    String gsonString = gson.toJson(list, new TypeToken<List<User>>() {
    }.getType());/*from w  ww.ja  va 2s  .  co m*/
    return gsonString;
}

From source file:client.model.User.java

public static List<User> fromJsonUser(String json) throws JsonSyntaxException {
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new Utility.JsonDateDeserializer()).create();
    List<User> list = gson.fromJson(json, new TypeToken<List<User>>() {
    }.getType());/*ww w.jav a  2 s  .co  m*/
    return list;
}

From source file:clientapi.load.mixin.MixinMinecraft.java

License:Apache License

@Inject(method = "init", at = @At("RETURN"))
private void init(CallbackInfo ci) {
    // Try and find the "client.json" config
    InputStream stream = this.getClass().getResourceAsStream("/client.json");

    if (stream == null)
        throw new ClientInitException("Unable to locate Client Configuration (client.json)");

    // Construct a ClientInfo object from the client json using GSON
    ClientInfo clientInfo = new GsonBuilder().setPrettyPrinting().create()
            .fromJson(new StreamReader(stream).all(), ClientInfo.class);

    if (clientInfo == null)
        throw new ClientInitException("Unable to create ClientInfo from client.json");

    // Attempt to instantiate the specified class from the ClientInfo
    Client client;//from   w  w  w  .j  a v  a2  s .c  o  m
    try {
        Class<?> clientClass = Class.forName(clientInfo.getMain());
        Constructor<?> constructor;
        if (clientClass != null && clientClass.getSuperclass().equals(Client.class)
                && (constructor = clientClass.getConstructor(ClientInfo.class)) != null) {
            client = (Client) constructor.newInstance(clientInfo);
        } else {
            throw new ClientInitException("Client class is null or the superclass is not Client type");
        }
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new ClientInitException("Unable to instantiate Client");
    } catch (ClassNotFoundException e) {
        throw new ClientInitException("Unable to find client class");
    } catch (NoSuchMethodException e) {
        throw new ClientInitException("Unable to find constructor with valid parameters");
    }

    // Init GLUtils
    GLUtils.init();

    // Init client
    client.onInit(clientInfo);

    ClientAPI.EVENT_BUS.subscribe(ClientHandler.INSTANCE);
}

From source file:cloudlens.notebook.JSInterpreter.java

License:Apache License

public InterpreterResult interpret(Callable<BlockObject> task, CL cl) {
    if (cl.out instanceof ByteArrayOutputStream) {
        ((ByteArrayOutputStream) cl.out).reset();
    }/* w w w . j a v  a2  s. c  o m*/
    if (cl.err instanceof ByteArrayOutputStream) {
        ((ByteArrayOutputStream) cl.err).reset();
    }
    final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r) {
                @Override
                public void interrupt() {
                    stop();
                }
            };
        }
    });
    cl.future = executor.submit(task);
    final Gson gson = new GsonBuilder().create();
    try {
        final BlockObject obj = cl.future.get();
        cl.outWriter.flush();
        cl.errWriter.flush();
        if (obj instanceof InterpreterResult) {
            return (InterpreterResult) obj;
        }
        if (cl.out instanceof ByteArrayOutputStream && ((ByteArrayOutputStream) cl.out).size() == 0) {
            if (null != obj && obj.isMapArray()) {
                final Map<String, Map<String, Object>> entries = obj.asMapArray();
                cl.outWriter.print("%table\n");
                int i = 0;
                for (final Map<?, ?> entry : entries.values()) {
                    cl.outWriter.print("\n");
                    if (++i > maxResult) {
                        cl.outWriter.println(
                                "%html <font color=red>Results are limited by zeppelin.cloudlens.maxResult = "
                                        + maxResult + ".</font>");
                        break;
                    }
                    for (final Map.Entry<?, ?> field : entry.entrySet()) {
                        cl.outWriter.print("%html <font color=blue>"
                                + StringEscapeUtils.escapeHtml4(field.getKey().toString()) + "</font>:"
                                + StringEscapeUtils.escapeHtml4(gson.toJson(field.getValue()).toString())
                                + "\t");
                    }
                }
            } else {
                cl.engine.bind("__Result__", obj);
                cl.engine.eval(
                        "print(JSON.stringify(__Result__, function(key, val) { if (typeof val === 'function') return val + ''; return val; }, 2))");
            }
        }
        // }
    } catch (final InterruptedException |

            ExecutionException e) {
        return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
    } finally {
        cl.outWriter.flush();
        cl.errWriter.flush();
        executor.shutdownNow();
    }
    return new InterpreterResult(Code.SUCCESS, cl.out.toString());
}