Anubis  Lockward Anubis Lockward - 4 months ago 19
JSON Question

Create JSON File Serializing the class in Java

I have the following class:

public class PDFValues {
private String value;
private int xPos;
private int yPos;

public PDFValues(){

}

public PDFValues(String value, int x, int y){
this.setValue(value);
this.setX(x);
this.setY(y);
}

public void setValue(String value){
this.value = value;
}

public void setX(int x){
this.xPos = x;
}

public void setY(int y){
this.yPos = y;
}

public String getValue(){
return this.value;
}

public int getX(){
return this.xPos;
}

public int getY(){
return this.yPos;
}
}


And then I want to do this:

public class PDFtoJSONTest {

public static void main(String[] args) {
List<PDFValues> listaElementos = new ArrayList<PDFValues>();

listaElementos.add(new PDFValues("Anubis Lockward Botello",
new Integer(100), new Integer(633)));

....more code

}

}


What I want to do, is save all the PDFValues in the ArrayList, as a JSON file, but I don't know how to make it automatic, I thought of Serializing the object or something, but I can't find a solution to this and I'm not sure how I could make the PDFValues object serializable.

I want to do this to save those values in a JSON file and then use them as properties to generate a PDF file, I'm using PDFBox for the generation of the PDFs files and SimpleJSON for the .json ones.

I'm working on a project where I'd have to generate thousands of pdf files out of some processed data. I've already managed to parse the data which consists of about 500+ MBs of text and holds around five thousand account statements which need to be generated as PDFs.

The thing is, that seeings as the Text data is generated in PDFs, one needs to indicate the position of the starting character in a string to PDFValues, to generate a PDFTextInfo object and then add it to the PDF, the file would need to contain images and text and other stuff.

Note: If there's a better way to accomplish what I'm trying to do, I'm open to suggestions, but I'd also like an answer to what I'm trying to do, and know if it would work or not, and why for both cases.

I'd like to make my code as fast as possible, right now I'm able to process the whole TXT file from RAW to mostly clean data in under 2 minutes and I know it might be optimized but right now that's not the point :D

I'd like to generate the PDFs files as fast as possible, but I've been working with that project in Java for like 1 1/2 months now, and I've only learned about JSON and PDF file generation in the past week...it's been three days of working on the JSON file and I'm a bit lost.

Here's an example of the file output for the JSON file that I'm trying to accomplish, this one I generated it manually and I managed to read and process the file, now I'm trying to generate it automatically:

{
"Header": {
"FullName":
{
"Name":"Anubis Lockward Botello",
"Horizontal":180,
"Vertical":633
},
..... more elements .....
}
}

As you can see, I'm trying to divide the elements on the pdf files as if they were panels, like HEADER, BALANCES, TRANSACTIONS and stuff, and build the PDF file as if they were pieces of a puzzle, but right now I'm trying to "build" the name element on the header.

Answer

I would guess there is a better way to do build PDF files than writing and then re-reading a file, but I can provide one answer for your JSON question.

Use the Apache commons-lang3 library and look into ToStringBuilder. You can then define a toString() method on your object and use the built-in ToStringStyle.JSON_STYLE format. You'd then have to wrap your list of PDFValues in some other object that can store a PDFValue for Header, FullName, etc..

    @Override
public String toString() {

    return new ToStringBuilder(this, ToStringStyle.JSON_STYLE)
            .append("Name", value)
            .append("Horizontal", xPos)
            .append("Vertical", yPos)

            .toString();
}
Comments