Reputation: 3616
Here goes my code
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
public class Test {
public static void main(String[] args) throws IOException {
JsonFactory jfactory = new JsonFactory();
JsonGenerator jGenerator = jfactory.createJsonGenerator(new File("test.json"), JsonEncoding.UTF8);
for(int i=0;i<4;i++){
jGenerator.writeStartObject(); // {
jGenerator.writeStringField("name" , "test");
jGenerator.writeEndObject(); // }
jGenerator.writeRaw('\n'); // creates new line
}
jGenerator.close();
System.out.println("File Generated");
}
}
After running this when i look into the generated file , it has a space just before each new record .. Is there a way to remove this space while generating the file itself??
Sample Output
{"name":"test"}
{"name":"test"}
{"name":"test"}
{"name":"test"}
Upvotes: 2
Views: 6980
Reputation: 29693
Use next PrettyPrinter
new MinimalPrettyPrinter("");
your code will looks like
JsonFactory jfactory = new JsonFactory();
JsonGenerator jGenerator = jfactory.createJsonGenerator(new File("test.json"), JsonEncoding.UTF8);
jGenerator.setPrettyPrinter(new MinimalPrettyPrinter(""));
for(int i=0;i<4;i++)
{
jGenerator.writeStartObject();
jGenerator.writeStringField("name" , "test");
jGenerator.writeEndObject();
jGenerator.writeRaw('\n');
}
jGenerator.close();
Upvotes: 6