Pagini recente » Cod sursa (job #2527698) | Cod sursa (job #112171) | Cod sursa (job #2722613) | Cod sursa (job #2722612) | Cod sursa (job #1361334)
import java.io.*;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.PrintWriter;
public class Main {
// enums of tokens used to parse the .in file
private static final byte CURLY_CLOSED = 1;
private static final byte COMMA = 2;
private static final byte COLON = 3;
private static final byte STRING = 4;
private static final byte NUMBER = 5;
private static char token;
private static StringBuilder result ;
public static void main(String[] args)throws IOException
{ // Used in getting the keys from the first object in the JSON
boolean keys = true;
// splitRegex splits by zero or more spaces, thus allowing to read the lines one character at a time
Pattern splitRegex = Pattern.compile("\\s*");
Pattern checkString = Pattern.compile("");
final int builderCapacity = 6000;
Scanner reader = new Scanner(new FileInputStream("convertor.in"));
PrintWriter writer = new PrintWriter("convertor.out");
reader.useDelimiter(splitRegex);
reader.skip("\\[");
token = reader.next().charAt(0);
// The resulting stream after parsing the input file; StringBuilder is used because of faster concatenation
//times than String
result = new StringBuilder(builderCapacity);
//Goes through all the tokens in the file, determined by the delimiter set
while( reader.hasNext() ){
//This loop is used to get the keys of the JSON Objects
while(keys){
if(checkToken(token) == CURLY_CLOSED){
keys = false;
// Write the keys of a JSON Object to the .out file and reinitialize the StringBuilder
result.append(System.lineSeparator());
writer.write(result.toString());
result = new StringBuilder(builderCapacity);
token = reader.next().charAt(0);
reader.close();
// Reinitialize the scanner in order to parse the values for all the JSON Objects
reader = new Scanner(new FileInputStream("convertor.in"));
reader.useDelimiter(splitRegex);
reader.skip("\\[");
}
if(checkToken(token) == STRING ){
getString(reader, splitRegex, checkString);
}
if(checkToken(token) == COLON){
//Skips the values, which will be parsed at a later time
while( (checkToken(token) != COMMA) && (checkToken(token) != CURLY_CLOSED) ) {
token = reader.next().charAt(0);
}
}
if(checkToken(token) != CURLY_CLOSED){
token = reader.next().charAt(0);
}
}
token = reader.next().charAt(0);
// Here starts the parsing of the values, once we obtained the keys
if(checkToken(token) == COLON){
token = reader.next().charAt(0);
getString(reader, splitRegex, checkString);
if(checkToken(token) == NUMBER){
while(checkToken(token) == NUMBER){
result.append(token);
token = reader.next().charAt(0);
}
result.append(',');
}
}
if(checkToken(token) == CURLY_CLOSED){
// Write the values of a JSON Object to the .out file and reinitialize the StringBuilder
result.append(System.lineSeparator());
writer.write(result.toString());
result = new StringBuilder(builderCapacity);
}
}
writer.close();
reader.close();
}
// Helper method used to get a value or a key of String type
private static void getString(Scanner reader, Pattern splitRegex, Pattern checkString){
if(checkToken(token) == STRING ){
token = reader.next().charAt(0);
reader.useDelimiter(checkString);
while(checkToken(token) != STRING){
result.append(token);
token = reader.next().charAt(0);
}
result.append(',');
reader.useDelimiter(splitRegex);
}
}
// Helper method used to determine which type of element the current token is
private static int checkToken(char token){
switch (token) {
case '}':
return CURLY_CLOSED;
case ',':
return COMMA;
case '"':
return STRING;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '-':
return NUMBER;
case ':':
return COLON;
default: return 12;
}
}
}