Search in sources :

Example 21 with GsonBuilder

use of com.google.gson.GsonBuilder in project ninja by ninjaframework.

the class ApiControllerDocTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 22 with GsonBuilder

use of com.google.gson.GsonBuilder in project ninja by ninjaframework.

the class ApiControllerDocTesterTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 23 with GsonBuilder

use of com.google.gson.GsonBuilder in project ninja by ninjaframework.

the class ApiControllerTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 24 with GsonBuilder

use of com.google.gson.GsonBuilder in project Prism-Bukkit by prism.

the class PsteApi method createPaste.

/**
     * Create a new paste
     * 
     * @param paste
     * @throws IOException
     */
public Results createPaste(Paste paste) throws IOException {
    final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    final String jsonPayload = gson.toJson(paste);
    final URL url = new URL(apiUrl + "/paste/");
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Length", Integer.toString(jsonPayload.getBytes().length));
    connection.setRequestProperty("Content-Language", "en-US");
    connection.setRequestProperty("Authorization", "Basic " + DatatypeConverter.printBase64Binary((apiUsername + ":" + apiKey).getBytes()));
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    final DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(jsonPayload);
    wr.flush();
    wr.close();
    // Read response
    final InputStream is = connection.getInputStream();
    final BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    final String json = readAll(rd);
    final Results response = gson.fromJson(json, Results.class);
    return response;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) DataOutputStream(java.io.DataOutputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Gson(com.google.gson.Gson) URL(java.net.URL)

Example 25 with GsonBuilder

use of com.google.gson.GsonBuilder in project core by s4.

the class ObjectBuilder method main.

public static void main(String[] argv) throws Exception {
    ObjectBuilder b = new ObjectBuilder();
    String s = "{a:5, b:100}";
    Object out = b.fromJson(s, TEST.class);
    System.out.println(out.toString());
    TEST t = new TEST(1, 2);
    System.out.println(b.toJson(t));
    String[] query = { "name", "count", "freq" };
    String[] target = { "ACDW", "11" };
    io.s4.message.Request.ClientRInfo rinfo = new io.s4.message.Request.ClientRInfo();
    rinfo.setRequesterUUID(UUID.randomUUID());
    Request req = new io.s4.message.SinglePERequest(Arrays.asList(target), Arrays.asList(query), rinfo);
    System.out.println(req.toString());
    InstanceCreator<io.s4.message.Request.RInfo> infoCreator = new InstanceCreator<io.s4.message.Request.RInfo>() {

        public io.s4.message.Request.RInfo createInstance(Type type) {
            return new io.s4.message.Request.ClientRInfo();
        }
    };
    Gson gson = (new GsonBuilder()).registerTypeAdapter(io.s4.message.Request.RInfo.class, infoCreator).registerTypeAdapter(Object.class, new ObjectTypeAdapter()).create();
    System.out.println("gson: " + gson.toJson(req));
    System.out.println("gson reversed: " + gson.fromJson(gson.toJson(req), SinglePERequest.class));
    System.out.println(b.toJson(req));
    System.out.println(b.toJson(Arrays.asList(query)));
    System.out.println("----------------------------------------------");
    ArrayList<SSTest> list = new ArrayList<SSTest>();
    SSTest ss1 = new SSTest();
    ss1.str = "list-element-1";
    SSTest ss2 = new SSTest();
    ss2.str = "list-element-2";
    list.add(ss1);
    list.add(ss2);
    Map<String, Object> listmap = new HashMap<String, Object>();
    listmap.put("ll", list);
    MapTest mt = new MapTest();
    mt.map = listmap;
    Object listmapobj = listmap;
    System.out.println("list: " + gson.toJson(list));
    System.out.println("listmap: " + gson.toJson(listmap));
    System.out.println("listmapobj: " + gson.toJson(listmapobj));
    System.out.println("mapobject: " + gson.toJson(mt));
}
Also used : SinglePERequest(io.s4.message.SinglePERequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) GsonBuilder(com.google.gson.GsonBuilder) SinglePERequest(io.s4.message.SinglePERequest) Request(io.s4.message.Request) Type(java.lang.reflect.Type) InstanceCreator(com.google.gson.InstanceCreator) JsonObject(com.google.gson.JsonObject)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)222 Gson (com.google.gson.Gson)135 IOException (java.io.IOException)40 Type (java.lang.reflect.Type)24 ArrayList (java.util.ArrayList)24 Map (java.util.Map)24 JsonElement (com.google.gson.JsonElement)20 HashMap (java.util.HashMap)20 Test (org.junit.Test)18 JsonObject (com.google.gson.JsonObject)15 JsonSyntaxException (com.google.gson.JsonSyntaxException)13 Date (java.util.Date)13 List (java.util.List)13 Retrofit (retrofit2.Retrofit)13 InputStream (java.io.InputStream)11 OkHttpClient (okhttp3.OkHttpClient)11 File (java.io.File)9 InputStreamReader (java.io.InputStreamReader)9 JsonParseException (com.google.gson.JsonParseException)8 URL (java.net.URL)8