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:batch.card.generator.BatchCardGenerator.java

/**
 * @param args the command line arguments
 *///from   w ww  . java  2s  . co  m
public static void main(String[] args) throws IOException {
    String path = "D:\\Tabletop Models\\2D stuff\\Cards\\";
    String path_vehicles = "D:\\Tabletop Models\\2D stuff\\tanks\\";
    List<String> vehicle_paths = new ArrayList<>();

    try {
        Files.walk(Paths.get(path_vehicles)).forEach(filePath -> {
            if (Files.isRegularFile(filePath)) {
                vehicle_paths.add(filePath.getFileName().toString());
            }
        });
    } catch (IOException ex) {
        Logger.getLogger(BatchCardGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Vehicles found: " + vehicle_paths.size());
    // load source images
    BufferedImage image = null;
    try {
        image = ImageIO.read(new File(path, "blank_template-400x563.png"));
    } catch (IOException ex) {
        Logger.getLogger(BatchCardGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }
    // END - Loading images, onto the card building

    // create the new image, canvas size is the max. of both image sizes
    int w = 400;
    int h = 563;
    List<Font> card_fonts = new ArrayList<>(); //Add 9

    card_fonts.add(new Font("Britannic Bold", Font.BOLD, 35)); //Title
    card_fonts.add(new Font("Sans", Font.ITALIC, 16)); //Subtitle
    card_fonts.add(new Font("Sans", Font.BOLD, 18)); //Key titles
    card_fonts.add(new Font("Sans", Font.PLAIN, 18)); //Key info and stats upper
    card_fonts.add(new Font("Sans", Font.PLAIN, 13)); //Stats lower
    card_fonts.add(new Font("Sans", Font.BOLD, 14)); //Resists title
    card_fonts.add(new Font("Sans", Font.PLAIN, 14)); //Resists data
    card_fonts.add(new Font("Sans", Font.BOLD, 12)); //Weapons title
    card_fonts.add(new Font("Sans", Font.PLAIN, 12)); //Weapons data and bio

    // Read json file into string
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(
                "C:\\Users\\Aaron\\Documents\\NetBeansProjects\\batch-card-generator\\cards.json"));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(BatchCardGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }

    Gson gson = new GsonBuilder().create();

    Machina[] people = gson.fromJson(reader, Machina[].class);

    //        for (Machina each : people) {
    //            System.out.println("Object mode: " + each.bio);
    //        }
    Rectangle title_rect = new Rectangle(96, 65, 192, 36);

    for (int i = 0; i < vehicle_paths.size(); i++) {
        BufferedImage overlay = null;
        BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

        try {
            overlay = ImageIO.read(new File(path_vehicles, vehicle_paths.get(i)));
        } catch (IOException ex) {
            Logger.getLogger(BatchCardGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

        // paint both images, preserving the alpha channels
        Graphics g = combined.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.drawImage(overlay, 208, 99, null); //208, 99 for tanks
        g.setColor(Color.black);

        Graphics2D g2 = (Graphics2D) g;
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setRenderingHints(rh);

        // if the tank has data
        for (Machina each : people) {
            if (vehicle_paths.get(i).equals(each.file)) {
                System.out.println(each.name + " has a match!");
                g2.setFont(card_fonts.get(0));
                FontMetrics metrics = g.getFontMetrics(card_fonts.get(0));
                int x = title_rect.x + (title_rect.width - metrics.stringWidth(each.name)) / 2;
                int y = (title_rect.y);
                // Name
                g2.drawString(each.name, x, y);
                // Subtitle
                g2.setFont(card_fonts.get(1));
                metrics = g.getFontMetrics(card_fonts.get(1));
                int x2 = title_rect.x + (title_rect.width - metrics.stringWidth(each.type)) / 2;
                g2.drawString(each.type, x2, y + 20);
                // Key stats
                g2.setFont(card_fonts.get(2));
                int keyY = 120;
                g2.drawString("Armor class:", 30, keyY);
                g2.drawString("Hit points:", 30, keyY + 20);
                g2.drawString("Speed:", 30, keyY + 40);
                g2.drawString("Hp/ton:", 30, keyY + 60);
                g2.setFont(card_fonts.get(3));
                g2.drawString(each.armor_class, 150, keyY);
                g2.drawString(each.hit_points, 130, keyY + 20);
                g2.drawString(each.speed, 100, keyY + 40);
                g2.drawString(each.power_weight, 100, keyY + 60);
                // Stats
                int statY = 230;
                g2.drawString("STR", 30, statY);
                g2.drawString("DEX", 90, statY);
                g2.drawString("CON", 148, statY);
                g2.drawString("INT", 215, statY);
                g2.drawString("WIS", 272, statY);
                g2.drawString("CHA", 330, statY);
                // Stats lower
                g2.setFont(card_fonts.get(4));
                g2.drawString(each.strength, 30, statY + 20);
                g2.drawString(each.dexterity, 90, statY + 20);
                g2.drawString(each.constitution, 152, statY + 20);
                g2.drawString(each.intelligence, 212, statY + 20);
                g2.drawString(each.wisdom, 272, statY + 20);
                g2.drawString(each.charisma, 335, statY + 20);
                // Resists etc
                int resistY = 275;
                g2.setFont(card_fonts.get(5));
                if (!"".equals(each.dmg_resist)) {
                    g2.drawString("Damage Resistance:", 30, resistY);
                    resistY = resistY + 15;
                }

                if (!"".equals(each.dmg_immune)) {
                    g2.drawString("Damage Immunities:", 30, resistY);
                    resistY = resistY + 15;
                }
                if (!"".equals(each.condition_immune)) {
                    g2.drawString("Condition Immunities:", 30, resistY);
                    resistY = resistY + 15;
                }

                g2.drawString("Senses:", 30, resistY);
                resistY = resistY + 15;
                g2.drawString("Languages:", 30, resistY);
                resistY = resistY + 15;
                g2.drawString("Challenge:", 30, resistY);
                g2.setFont(card_fonts.get(6));
                resistY = 275;
                if (!"".equals(each.dmg_resist)) {
                    g2.drawString(each.dmg_resist, 178, resistY);
                    resistY = resistY + 15;
                }
                if (!"".equals(each.dmg_immune)) {
                    g2.drawString(each.dmg_immune, 176, resistY);
                    resistY = resistY + 15;
                }
                if (!"".equals(each.condition_immune)) {
                    g2.drawString(each.condition_immune, 188, resistY);
                    resistY = resistY + 15;
                }
                g2.drawString(each.senses, 90, resistY);
                resistY = resistY + 15;
                g2.drawString(each.languages, 118, resistY);
                resistY = resistY + 15;
                g2.drawString(each.challenge, 110, resistY);
                resistY = resistY + 30;
                // Weapons and hurting stuff
                int weaponY = resistY;
                g2.setFont(card_fonts.get(7));
                int width1 = 0;
                int width2 = 0;
                int width3 = 0;
                if (!"".equals(each.ability_1)) {
                    g2.drawString(each.ability_1, 30, weaponY);
                    width1 = g2.getFontMetrics().stringWidth(each.ability_1);
                }
                if (!"".equals(each.ability_2)) {
                    g2.drawString(each.ability_2, 30, weaponY + 30);
                    width2 = g2.getFontMetrics().stringWidth(each.ability_2);
                }
                if (!"".equals(each.ability_3)) {
                    g2.drawString(each.ability_3, 30, weaponY + 60);
                    width3 = g2.getFontMetrics().stringWidth(each.ability_3);
                }

                // Bio
                g2.setFont(card_fonts.get(8));
                if (!"".equals(each.ability_1)) {
                    drawTextWrap(each.ability_1_d, g2.getFontMetrics(card_fonts.get(7)), g2, 35 + width1,
                            weaponY, 300);
                }
                if (!"".equals(each.ability_2)) {
                    drawTextWrap(each.ability_2_d, g2.getFontMetrics(card_fonts.get(7)), g2, 35 + width2,
                            weaponY + 30, 300);
                }
                if (!"".equals(each.ability_3)) {
                    drawTextWrap(each.ability_3_d, g2.getFontMetrics(card_fonts.get(7)), g2, 35 + width3,
                            weaponY + 60, 300);
                }
                drawTextWrap(each.bio, g.getFontMetrics(card_fonts.get(8)), g2, 58, 505, 290);
            }
        }

        try {
            // Save as new image
            ImageIO.write(combined, "PNG", new File(path + "\\javaDeck\\", "01x " + vehicle_paths.get(i)));
        } catch (IOException ex) {
            Logger.getLogger(BatchCardGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Completed: " + vehicle_paths.get(i) + "- No. " + i + "/" + vehicle_paths.size());
    }
    System.out.println("Thanks!");

}

From source file:bd.ac.seu.jsondemo.Main.java

public static void writingExample() {
    Student s1 = new Student("222", "J Doe", 2.3);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String output = gson.toJson(s1);

    try {//from  w w w .  j  a  v a2  s. com
        RandomAccessFile outFile = new RandomAccessFile("student.txt", "rw");
        outFile.setLength(0);
        outFile.writeBytes(output);
        outFile.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:bd.ac.seu.jsondemo.Main.java

public static void readFromURL() {
    try {//from w ww. ja  va2  s  .c  o  m
        URL url = new URL(
                "http://my.seu.ac.bd/~kmhasan/__WebServices/spring2017aj/schedule_section_json.php?semester=45");
        InputStream inputStream = url.openStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String line = bufferedReader.readLine();

        Gson gson = new GsonBuilder().create();

        Type sectionsListType = new TypeToken<List<Section>>() {
        }.getType();
        List<Section> sectionsList = gson.fromJson(line, sectionsListType);

        sectionsList.forEach(System.out::println);
    } catch (MalformedURLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:bd.ac.seu.jsondemo.Main.java

public static void readingExample() {
    try {//from   w ww  . java2  s  . com
        RandomAccessFile inFile = new RandomAccessFile("student.txt", "r");
        String line;
        String inputString = "";

        while ((line = inFile.readLine()) != null) {
            inputString += line;
        }

        Gson gson = new GsonBuilder().create();
        Student s1 = gson.fromJson(inputString, Student.class);
        System.out.println(s1.getStudentName());
        inFile.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:bd.ac.seu.midtermq4.Main.java

public void readJson() {
    Gson gson = new GsonBuilder().create();
    try {//  w  ww.  j  av  a  2 s .  com
        URL url = new URL("http://my.seu.ac.bd/~kmhasan/sculptures.json");
        InputStream inputStream = url.openStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String jsonString = "";
        String line;
        while ((line = bufferedReader.readLine()) != null)
            jsonString += line.trim();
        System.out.println("JSON input = [" + jsonString + "]");

        Type sculpturesListType = new TypeToken<List<Sculpture>>() {
        }.getType();
        List<Sculpture> sculpturesList = gson.fromJson(jsonString, sculpturesListType);

        sculpturesList.forEach(System.out::println);

    } catch (MalformedURLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:be.bittich.quote.core.DynaUtil.java

License:Apache License

/**
 * Jsonify a map of K,V// www.jav  a2 s.  c  om
 *
 * @param map
 * @return
 */
static String jsonifyMap(Map<?, ?> map) {
    Gson gson = new GsonBuilder().create();
    String json = gson.toJson(map);
    return json;
}

From source file:be.e_contract.eid.android.endpoint.AndroidEndpointServlet.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletresponse)
        throws ServletException, IOException {
    LOG.debug("doPost");
    BeIDOperation operation = getOperation();
    if (BeIDOperation.READ_IDENTITY == operation) {
        Response response = new Response();
        response.action = "identity";
        PrintWriter printWriter = httpServletresponse.getWriter();
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();// w  ww. j  a  va 2 s.  c  o m
        printWriter.write(gson.toJson(response));
    }
}

From source file:be.iminds.iot.dianne.nn.util.DianneJSONConverter.java

License:Open Source License

public static String toJsonString(NeuralNetworkDTO dto, boolean pretty) {
    JsonObject nn = toJson(dto);//from w  w  w.  ja v  a 2 s  . c om

    GsonBuilder builder = new GsonBuilder();
    if (pretty) {
        builder.setPrettyPrinting();
    }
    Gson gson = builder.create();
    String output = gson.toJson(nn);

    return output;
}

From source file:be.norio.twunch.android.util.PrefsUtils.java

License:Open Source License

public static TwunchData getData() {
    final String string = getPrefs().getString(KEY_DATA, null);
    if (TextUtils.isEmpty(string)) {
        return new TwunchData();
    } else//from   w w  w . j  a v  a2s.co m
        return (new GsonBuilder()).create().fromJson(string, TwunchData.class);
}

From source file:be.norio.twunch.android.util.PrefsUtils.java

License:Open Source License

public static LruCache<String, String> getAvatars() {
    final LruCache<String, String> cache = new LruCache<String, String>(200);
    final String string = getPrefs().getString(KEY_AVATARS, null);
    if (!TextUtils.isEmpty(string)) {
        Map<String, String> map = new HashMap<String, String>();
        Map<String, String> avatars = (new GsonBuilder()).create().fromJson(string, map.getClass());
        Iterator it = avatars.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            cache.put((String) pairs.getKey(), (String) pairs.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }//  ww  w.ja va  2s . c  om
    }
    return cache;
}