Example usage for com.google.gson JsonArray JsonArray

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

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

From source file:CarouselAdsExample.java

License:Open Source License

public static void main(String[] args) {
    try {/*from  ww w. j a  va  2 s.c o m*/
        AdAccount account = new AdAccount(ACCOUNT_ID, context);
        Targeting targeting = new Targeting()
                .setFieldGeoLocations(new TargetingGeoLocation().setFieldCountries(Arrays.asList("US")))
                .setFieldAgeMin(18L).setFieldAgeMax(30L).setFieldUserOs(Arrays.asList("Android", "iOS"));

        Campaign campaign = account.createCampaign().setName("Java SDK Test Carousel Campaign")
                .setObjective(Campaign.EnumObjective.VALUE_LINK_CLICKS).setSpendCap(10000L)
                .setStatus(Campaign.EnumStatus.VALUE_PAUSED).execute();

        AdSet adset = account.createAdSet().setName("Java SDK Test Carousel AdSet")
                .setCampaignId(campaign.getFieldId()).setStatus(AdSet.EnumStatus.VALUE_PAUSED)
                .setBillingEvent(AdSet.EnumBillingEvent.VALUE_IMPRESSIONS).setDailyBudget(1000L)
                .setBidAmount(100L).setOptimizationGoal(AdSet.EnumOptimizationGoal.VALUE_IMPRESSIONS)
                .setTargeting(targeting).execute();

        AdImage image = account.createAdImage().addUploadFile("file", imageFile).execute();
        JsonArray childAttachments = new JsonArray();
        JsonObject attachment1 = new JsonObject();
        attachment1.addProperty("link", "https://www.example.com");
        attachment1.addProperty("description", "www.example.com");
        attachment1.addProperty("image_hash", image.getFieldHash());
        childAttachments.add(attachment1);
        JsonObject attachment2 = new JsonObject();
        attachment2.addProperty("link", "https://www.example.com");
        attachment2.addProperty("description", "www.example.com");
        attachment2.addProperty("image_hash", image.getFieldHash());
        childAttachments.add(attachment2);
        JsonObject attachment3 = new JsonObject();
        attachment3.addProperty("link", "https://www.example.com");
        attachment3.addProperty("description", "www.example.com");
        attachment3.addProperty("picture",
                "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");
        childAttachments.add(attachment3);

        AdCreative creative = account.createAdCreative().setTitle("Java SDK Test Carousel Creative")
                .setBody("Java SDK Test Carousel Creative")
                .setObjectStorySpec(new AdCreativeObjectStorySpec().setFieldLinkData(new AdCreativeLinkData()
                        .setFieldChildAttachments(childAttachments.toString()).setFieldLink("www.example.com"))
                        .setFieldPageId(pageId))
                .setLinkUrl("www.example.com").execute();

        Ad ad = account.createAd().setName("Java SDK Test Carousel ad")
                .setAdsetId(Long.parseLong(adset.getId())).setCreative(creative).setStatus("PAUSED")
                .setBidAmount(100L).setRedownload(true).execute();

    } catch (APIException e) {
        e.printStackTrace();
    }
}

From source file:serverSocket.java

License:Apache License

public JsonObject sortJson(JsonArray arr) {

    List<JsonObject> tmp = new ArrayList<JsonObject>();
    for (JsonElement obj : arr) {
        tmp.add(obj.getAsJsonObject());//w w w  .  j  a  v  a  2s .co  m
    }
    Collections.sort(tmp, new Comparator<JsonObject>() {
        @Override
        public int compare(JsonObject a, JsonObject b) {
            return (int) (a.get("epoch").getAsLong() - b.get("epoch").getAsLong());
        }
    });

    //classify by time slots
    long start = tmp.get(0).get("epoch").getAsLong();
    //15 mins
    long slot = 15L * 60L;

    start += slot;
    JsonArray collection = new JsonArray();
    JsonObject done = new JsonObject();
    for (JsonObject obj : tmp) {
        if (obj.get("epoch").getAsLong() > start) {
            done.add(String.valueOf(start), collection);
            start = obj.get("epoch").getAsLong();
            start += slot;
            collection = new JsonArray();
            collection.add(obj);
        } else {
            collection.add(obj);

        }

    }

    return done;
}

From source file:serverSocket.java

License:Apache License

@OnOpen
public void start(Session session) {
    this.session = session;
    connections.add(this);
    try {/*  ww  w . j a  v  a2  s .co m*/
        synchronized (this) {
            JsonObject ret = new JsonObject();
            ret.addProperty("access_key", uuid);
            this.session.getBasicRemote().sendText(ret.toString());
            connPool.put(uuid, session);
        }
    } catch (IOException e) {
        e.printStackTrace();

        try {
            this.session.close();
        } catch (IOException e1) {
            // Ignore
        }

    }

    jsonParser = new JsonParser();

    //tricky, put tweets_data in this path
    //System.out.println(this.getClass().getResource("").getPath());

    JsonArray rawlist = new JsonArray();
    try {
        BufferedReader br = new BufferedReader(
                new InputStreamReader(serverSocket.class.getResourceAsStream("/tweets_data.txt")));
        String line = br.readLine();
        rawlist = jsonParser.parse(line).getAsJsonArray();

        br.close();
        //System.out.println(list);

    } catch (IOException e) {
        System.out.println(this.getClass().getResource("").getPath());
        e.printStackTrace();
    }

    list = sortJson(rawlist);

}

From source file:RunnerRepository.java

License:Apache License

public static void generateJSon() {
    JsonObject root = new JsonObject();
    JsonObject array = new JsonObject();
    array.addProperty("Embedded", "embedded");
    array.addProperty("DEFAULT", "Embedded");
    JsonObject array2 = new JsonObject();
    array2.addProperty("NimbusLookAndFeel", "javax.swing.plaf.nimbus.NimbusLookAndFeel");
    array2.addProperty("MetalLookAndFeel", "javax.swing.plaf.metal.MetalLookAndFeel");
    array2.addProperty("MotifLookAndFeel", "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
    array2.addProperty("WindowsLookAndFeel", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    array2.addProperty("JGoodiesWindowsLookAndFeel", "com.jgoodies.looks.windows.WindowsLookAndFeel");
    array2.addProperty("Plastic3DLookAndFeel", "com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
    array2.addProperty("PlasticXPLookAndFeel", "com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
    array2.addProperty("DEFAULT", "MetalLookAndFeel");
    root.add("plugins", new JsonArray());
    root.add("editors", array);
    root.add("looks", array2);
    root.add("layout", new JsonObject());
    try {// w w  w .j  av  a  2  s .co m
        FileWriter writer = new FileWriter(TWISTERINI);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        writer.write(gson.toJson(root));
        writer.close();
    } catch (Exception e) {
        System.out.println("Could not write default JSon to twister.conf");
        e.printStackTrace();
    }
    System.out.println("twister.conf successfully created");
}

From source file:RunnerRepository.java

License:Apache License

public static void parseIni(File ini) {
    try {/*from ww w  .  j  a  v a 2  s  .  com*/
        FileInputStream in = new FileInputStream(ini);
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer b = new StringBuffer("");
        String line;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                b.append(line);
            }
            bufferedReader.close();
            inputStreamReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JsonElement jelement = new JsonParser().parse(b.toString());
        inifile = jelement.getAsJsonObject();
        editors = inifile.getAsJsonObject("editors");
        looks = inifile.getAsJsonObject("looks");
        layout = inifile.getAsJsonObject("layout");
        plugins = inifile.getAsJsonArray("plugins");
        if (layout == null) {
            inifile.add("layout", new JsonObject());
            writeJSon();
            layout = inifile.getAsJsonObject("layout");
        }
        if (plugins == null) {
            inifile.add("plugins", new JsonArray());
            writeJSon();
            plugins = inifile.getAsJsonArray("plugins");
        }
    } catch (Exception e) {
        System.out.print("Could not parse ini file: ");
        try {
            System.out.println(ini.getCanonicalPath());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        e.printStackTrace();
    }
}

From source file:RunnerRepository.java

License:Apache License

public static void removePlugin(String filename) {
    JsonArray array = new JsonArray();
    int size = getPlugins().size();
    for (int i = 0; i < size; i++) {
        if (getPlugins().get(i).getAsString().equals(filename))
            continue;
        array.add(getPlugins().get(i));/*  w  w w  . j ava2 s . c  om*/
    }
    plugins = array;
    inifile.add("plugins", array);
    writeJSon();
}

From source file:RunnerRepository.java

License:Apache License

public static void addPlugin(String pluginfilename) {
    if (getPlugins().isJsonArray()) {
        getPlugins().getAsJsonArray().add(new JsonPrimitive(pluginfilename));
    } else {/*from   w  ww .  ja va 2  s . c o  m*/
        JsonPrimitive primitive = new JsonPrimitive(pluginfilename);
        JsonArray array = new JsonArray();
        array.add(primitive);
        inifile.add("plugins", array);
    }
    writeJSon();
}

From source file:HyperLogLog.java

public String ToJson() {
    Gson g = new Gson();
    JsonObject elm = new JsonObject();
    elm.addProperty("M", this.m);
    elm.addProperty("B", this.b);
    elm.addProperty("A", this.alpha);
    JsonArray registerArr = new JsonArray();
    for (int i = 0; i < registers.length; i++) {
        registerArr.add(new JsonPrimitive(registers[i]));
    }//from   ww  w . j a v  a  2 s  . c  o  m
    elm.add("R", registerArr);
    return g.toJson(elm);
}

From source file:ListAction.java

@Override
public void execute(HttpServletRequest request, PrintWriter out, File folder) {

    try {/* w  ww .  j a v  a2 s .c o  m*/
        String type = request.getParameter("type");

        JsonArray list = new JsonArray();

        switch (type) {
        case "movie":
            File[] movies = Service.listAllMovies(folder);

            for (int i = 0; i < movies.length; i++) {
                JsonObject m = new JsonObject();
                m.addProperty("title", movies[i].getName());
                list.add(m);
            }

            break;

        case "series":
            File[] series = Service.listAllSeries(folder);

            for (int i = 0; i < series.length; i++) {
                JsonObject m = new JsonObject();
                m.addProperty("title", series[i].getName());
                list.add(m);
            }

            break;

        case "episode":
            String series_title = request.getParameter("series");

            folder = new File(folder.getAbsolutePath() + "\\Series\\" + series_title);

            List<File> episodes = Service.listAllEpisodes(folder);

            for (File f : episodes) {
                JsonObject episode = new JsonObject();

                String title = f.getName().split(" - ")[0];

                episode.addProperty("title", series_title + " - " + title);

                list.add(episode);
            }

            break;
        }

        JsonObject container = new JsonObject();

        container.add("list", list);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(container);
        out.println(json);
    } catch (Exception e) {
        throw e;
    }
}

From source file:EAIJSONConverter.java

License:Open Source License

public static SiebelPropertySet JsonObjectToPropertySet(JsonObject obj, SiebelPropertySet ps) {
    Iterator<Entry<String, JsonElement>> iterator = obj.entrySet().iterator();
    SiebelPropertySet child;//w  w  w .  j a  va 2  s  . co  m
    while (iterator.hasNext()) {
        JsonArray jsonArray = new JsonArray();
        JsonObject jsonObject = new JsonObject();
        Map.Entry mapEntry = (Map.Entry) iterator.next();
        if (mapEntry != null) {
            JsonElement jsonelement = (JsonElement) mapEntry.getValue();
            if (jsonelement.isJsonArray()) {
                jsonArray = jsonelement.getAsJsonArray();
                child = new SiebelPropertySet();
                child.setType("ListOf-" + mapEntry.getKey().toString());
                for (int i = 0; i < jsonArray.size(); i++) {
                    if (jsonArray.get(i).isJsonObject() || jsonArray.get(i).isJsonArray()) {
                        SiebelPropertySet temp = new SiebelPropertySet();
                        temp.setType("" + i);
                        if (jsonArray.get(i).isJsonObject())
                            child.addChild(JsonObjectToPropertySet(jsonArray.get(i).getAsJsonObject(), temp));
                        else {
                            JsonObject aux = new JsonObject();
                            aux.add("" + i, jsonArray.get(i));
                            child.addChild(JsonObjectToPropertySet(aux, temp));

                        }
                    } else
                        child.setProperty("" + i, jsonArray.get(i).getAsString());
                }
                ps.addChild(child);
            } else if (jsonelement.isJsonObject()) {
                jsonObject = jsonelement.getAsJsonObject();
                child = new SiebelPropertySet();
                child.setType(mapEntry.getKey().toString());
                ps.addChild(JsonObjectToPropertySet(jsonObject, child));
            } else {
                ps.setProperty(mapEntry.getKey().toString(), mapEntry.getValue().toString());
            }
        }
    }

    return ps;
}