Recursive parser from Binary to JSON outputYet another JSON parser and serializer for Qt, but with additional featuresA JavaScript VM that interprets code written in JSONJSON C++14 library API/implementationGenerate URLs from a complex JSON object using recursionC# Regex to extract bug ID from Bugzilla JSON responseExcel VBA to parse JSON out of Google Maps APIJavacript code to flatten JSON via recursion with conditional logicOptimize function for assigning existing 3D array values to values of keys in array of objectsLooping JSON in WebAPI Controller and Add new PropertyRecursively generating the look-and-say sequence
What is the difference between "shut" and "close"?
Replacing Windows 7 security updates with anti-virus?
What has been your most complicated TikZ drawing?
It's a yearly task, alright
Time dilation for a moving electronic clock
Life insurance that covers only simultaneous/dual deaths
Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?
Are there situations where a child is permitted to refer to their parent by their first name?
Can infringement of a trademark be pursued for using a company's name in a sentence?
Giving Plot options defined outside of the Plot expression
Is a lawful good "antagonist" effective?
How is the Swiss post e-voting system supposed to work, and how was it wrong?
Unreachable code, but reachable with exception
What is the dot in “1.2.4."
Question about partial fractions with irreducible quadratic factors
Is it true that real estate prices mainly go up?
Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements
Is this animal really missing?
Gravity alteration as extermination tool viable?
Does the Bracer of Flying Daggers benefit from the Dueling Fighting style?
Running a subshell from the middle of the current command
Why don't MCU characters ever seem to have language issues?
What happens with multiple copies of Humility and Glorious Anthem on the battlefield?
Should QA ask requirements to developers?
Recursive parser from Binary to JSON output
Yet another JSON parser and serializer for Qt, but with additional featuresA JavaScript VM that interprets code written in JSONJSON C++14 library API/implementationGenerate URLs from a complex JSON object using recursionC# Regex to extract bug ID from Bugzilla JSON responseExcel VBA to parse JSON out of Google Maps APIJavacript code to flatten JSON via recursion with conditional logicOptimize function for assigning existing 3D array values to values of keys in array of objectsLooping JSON in WebAPI Controller and Add new PropertyRecursively generating the look-and-say sequence
$begingroup$
Background
I got this interview test and got declined due to not meeting their expectations, but never got a reason on what was bad and how they solved it. I want to improve and learn something out of it.
Question
How can this be better written and what is badly written?
Assignment description
To optimize the amount of data sent between the web browser to the web server
a "clever" Javascript developer came up with the idea to translate JSON objects
into binary format in the application and send them to the server. Faced with
the fact that the Javascript is released in its final version to the customer
it is now your task to develop the parser on the back end system.
A JSON object is a hierarchy of key-value pairs where a value in its turn can
contain new key-value pairs. It consists of four basic types: numbers, strings,
arrays and dictionaries. An array is a list of values and a dictionay is a list
of key-value pairs. The key can only be of the type number or string while a
value can be of any type.
A JSON object always starts with a value.
An example JSON object can look like:
'firstName': 'John',
'lastName': 'Smith',
'age': 25,
'address':
'streetAddress': '21 2nd Street',
'city': 'New York',
'state': 'NY',
'postalCode': '10021'
,
'phoneNumber': [
'type': 'home', 'number': '212 555-1234' ,
'type': 'fax', 'number': '646 555-4567'
]
A number is printed in decimal without any decoration.
Example: 25
A string is printed in ASCII with single quotes in the start and end of the
string.
Example: 'test'
A key-value pair is printed as key followed by colon (:), a space ( ) and the
value.
Example: 'a': 67
A dictionary starts and ends with curly brackets ( and ) and then has a
comma (,) separated list of key-value pairs.
Example: 'name': 'Joe', 'age': 31
An array starts and ends with square brackets ([ and ]) and then has a
comma (,) separated list of values.
Example: [ 'hello', 56, 'world' ]
The binary representation of the JSON object contains a one byte identifier
that describes the type of the data to follow and is then immediately followed
by the data.
The identifiers and their types are as follows:
Identifier Type Description
0x01 Number 4 bytes signed integer in big endian byte order.
0x02 String N ASCII characters terminated by 0x00.
0x05 List Amount of items as a number followed by N values
0x06 Dictionary Amount of items as a number followed by N
key-value pairs
The program's task is to parse a binary file and prints it as human readable
text. It should read the data from standard input and writes it the result to
standard output.
Look at the files 'input_x' and their respective 'result_x' for examples of
input and output. More background can be found on e.g. www.json.org
Input_4 binary
firstsecondvalue for seconddeepinteger as keymixit is possible to mix integers and strings#Eg
My solution
public class Main
private static String INPUT_FILENAME = "input_4";
private static String OUTPUT_FILENAME = "result_4";
private static String RESOURCE_INPUT_PATH = "src/main/resources/input/";
private static String RESOURCE_OUTPUT_PATH = "src/main/resources/output/";
public static void main(String[] args)
File resourcesDirectory = new File(String.format("%s%s", RESOURCE_INPUT_PATH, INPUT_FILENAME));
File file = new File(resourcesDirectory.getAbsolutePath());
try
byte[] byteArray = Files.readAllBytes(file.toPath());
RecursiveParser recursiveParser = new RecursiveParser();
try
String result = recursiveParser.parse(byteArray, 0, false).toString();
String prettyPrinted = prettyPrint(result);
BufferedWriter writer = new BufferedWriter(
new FileWriter(
new File(
String.format("%s%s%s", RESOURCE_OUTPUT_PATH, OUTPUT_FILENAME, ".json")
)
)
);
writer.write(prettyPrinted);
writer.close();
catch (JSONException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
private static String prettyPrint(String data) throws JSONException
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
return (new JSONObject(data)).toString(4);
else if (json instanceof JSONArray)
return (new JSONArray(data)).toString(4);
else
return data; // nothing to pretty print
class RecursiveParser
private static int TERMINATE = 0x00;
private static int NUMBER = 0x01; // Number 4 bytes signed integer in big endian byte order.
private static int STRING = 0x02; // String N ASCII characters terminated by 0x00.
private static int LIST = 0x05; // List Amount of items as a number followed by N values
private static int DICTIONARY = 0x06; // Dictionary Amount of items as a number followed by N key-value pairs
Object parse(byte[] byteArray, int index, boolean hasSub) throws JSONException
for(; index < byteArray.length; index++)
if(byteArray[index] == NUMBER)
return getNumber(byteArray, index);
if(byteArray[index] == STRING)
return getString(byteArray, index);
if(byteArray[index] == LIST)
return getList(byteArray, index, hasSub);
if(byteArray[index] == DICTIONARY)
return getDictionary(byteArray, index, hasSub);
return null; // should never get here
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because dictionary size
int dictionarySize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(dictionarySize).array().length;
JSONWriter jsonWriter = new JSONStringer()
.object();
for(int i = 0; i < dictionarySize; i++)
jsonWriter
.endObject();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONObject(jsonWriter.toString())) : new JSONObject(jsonWriter.toString());
private Object getList(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because list size
int listSize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(listSize).array().length;
JSONWriter jsonWriter = new JSONStringer().array();
for(int i = 0; i < listSize; i++)
index++;
// check if sub-array or sub-dictionary
hasSub = hasSub
jsonWriter.endArray();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONArray(jsonWriter.toString())) : new JSONArray(jsonWriter.toString());
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
Result_4 output
"5": 25,
"deep":
"1": "integer as key",
"2": "4": 19088743,
"mix": "it is possible to mix integers and strings"
,
"first": 16777216,
"second": "value for second"
java recursion interview-questions json
New contributor
$endgroup$
add a comment |
$begingroup$
Background
I got this interview test and got declined due to not meeting their expectations, but never got a reason on what was bad and how they solved it. I want to improve and learn something out of it.
Question
How can this be better written and what is badly written?
Assignment description
To optimize the amount of data sent between the web browser to the web server
a "clever" Javascript developer came up with the idea to translate JSON objects
into binary format in the application and send them to the server. Faced with
the fact that the Javascript is released in its final version to the customer
it is now your task to develop the parser on the back end system.
A JSON object is a hierarchy of key-value pairs where a value in its turn can
contain new key-value pairs. It consists of four basic types: numbers, strings,
arrays and dictionaries. An array is a list of values and a dictionay is a list
of key-value pairs. The key can only be of the type number or string while a
value can be of any type.
A JSON object always starts with a value.
An example JSON object can look like:
'firstName': 'John',
'lastName': 'Smith',
'age': 25,
'address':
'streetAddress': '21 2nd Street',
'city': 'New York',
'state': 'NY',
'postalCode': '10021'
,
'phoneNumber': [
'type': 'home', 'number': '212 555-1234' ,
'type': 'fax', 'number': '646 555-4567'
]
A number is printed in decimal without any decoration.
Example: 25
A string is printed in ASCII with single quotes in the start and end of the
string.
Example: 'test'
A key-value pair is printed as key followed by colon (:), a space ( ) and the
value.
Example: 'a': 67
A dictionary starts and ends with curly brackets ( and ) and then has a
comma (,) separated list of key-value pairs.
Example: 'name': 'Joe', 'age': 31
An array starts and ends with square brackets ([ and ]) and then has a
comma (,) separated list of values.
Example: [ 'hello', 56, 'world' ]
The binary representation of the JSON object contains a one byte identifier
that describes the type of the data to follow and is then immediately followed
by the data.
The identifiers and their types are as follows:
Identifier Type Description
0x01 Number 4 bytes signed integer in big endian byte order.
0x02 String N ASCII characters terminated by 0x00.
0x05 List Amount of items as a number followed by N values
0x06 Dictionary Amount of items as a number followed by N
key-value pairs
The program's task is to parse a binary file and prints it as human readable
text. It should read the data from standard input and writes it the result to
standard output.
Look at the files 'input_x' and their respective 'result_x' for examples of
input and output. More background can be found on e.g. www.json.org
Input_4 binary
firstsecondvalue for seconddeepinteger as keymixit is possible to mix integers and strings#Eg
My solution
public class Main
private static String INPUT_FILENAME = "input_4";
private static String OUTPUT_FILENAME = "result_4";
private static String RESOURCE_INPUT_PATH = "src/main/resources/input/";
private static String RESOURCE_OUTPUT_PATH = "src/main/resources/output/";
public static void main(String[] args)
File resourcesDirectory = new File(String.format("%s%s", RESOURCE_INPUT_PATH, INPUT_FILENAME));
File file = new File(resourcesDirectory.getAbsolutePath());
try
byte[] byteArray = Files.readAllBytes(file.toPath());
RecursiveParser recursiveParser = new RecursiveParser();
try
String result = recursiveParser.parse(byteArray, 0, false).toString();
String prettyPrinted = prettyPrint(result);
BufferedWriter writer = new BufferedWriter(
new FileWriter(
new File(
String.format("%s%s%s", RESOURCE_OUTPUT_PATH, OUTPUT_FILENAME, ".json")
)
)
);
writer.write(prettyPrinted);
writer.close();
catch (JSONException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
private static String prettyPrint(String data) throws JSONException
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
return (new JSONObject(data)).toString(4);
else if (json instanceof JSONArray)
return (new JSONArray(data)).toString(4);
else
return data; // nothing to pretty print
class RecursiveParser
private static int TERMINATE = 0x00;
private static int NUMBER = 0x01; // Number 4 bytes signed integer in big endian byte order.
private static int STRING = 0x02; // String N ASCII characters terminated by 0x00.
private static int LIST = 0x05; // List Amount of items as a number followed by N values
private static int DICTIONARY = 0x06; // Dictionary Amount of items as a number followed by N key-value pairs
Object parse(byte[] byteArray, int index, boolean hasSub) throws JSONException
for(; index < byteArray.length; index++)
if(byteArray[index] == NUMBER)
return getNumber(byteArray, index);
if(byteArray[index] == STRING)
return getString(byteArray, index);
if(byteArray[index] == LIST)
return getList(byteArray, index, hasSub);
if(byteArray[index] == DICTIONARY)
return getDictionary(byteArray, index, hasSub);
return null; // should never get here
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because dictionary size
int dictionarySize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(dictionarySize).array().length;
JSONWriter jsonWriter = new JSONStringer()
.object();
for(int i = 0; i < dictionarySize; i++)
jsonWriter
.endObject();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONObject(jsonWriter.toString())) : new JSONObject(jsonWriter.toString());
private Object getList(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because list size
int listSize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(listSize).array().length;
JSONWriter jsonWriter = new JSONStringer().array();
for(int i = 0; i < listSize; i++)
index++;
// check if sub-array or sub-dictionary
hasSub = hasSub
jsonWriter.endArray();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONArray(jsonWriter.toString())) : new JSONArray(jsonWriter.toString());
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
Result_4 output
"5": 25,
"deep":
"1": "integer as key",
"2": "4": 19088743,
"mix": "it is possible to mix integers and strings"
,
"first": 16777216,
"second": "value for second"
java recursion interview-questions json
New contributor
$endgroup$
$begingroup$
Would you mind adding additional examples? I don't quite follow the transformation fromInput_4
toResult_4
. Where is, for example,"5": 25
coming from?
$endgroup$
– ggorlen
44 mins ago
add a comment |
$begingroup$
Background
I got this interview test and got declined due to not meeting their expectations, but never got a reason on what was bad and how they solved it. I want to improve and learn something out of it.
Question
How can this be better written and what is badly written?
Assignment description
To optimize the amount of data sent between the web browser to the web server
a "clever" Javascript developer came up with the idea to translate JSON objects
into binary format in the application and send them to the server. Faced with
the fact that the Javascript is released in its final version to the customer
it is now your task to develop the parser on the back end system.
A JSON object is a hierarchy of key-value pairs where a value in its turn can
contain new key-value pairs. It consists of four basic types: numbers, strings,
arrays and dictionaries. An array is a list of values and a dictionay is a list
of key-value pairs. The key can only be of the type number or string while a
value can be of any type.
A JSON object always starts with a value.
An example JSON object can look like:
'firstName': 'John',
'lastName': 'Smith',
'age': 25,
'address':
'streetAddress': '21 2nd Street',
'city': 'New York',
'state': 'NY',
'postalCode': '10021'
,
'phoneNumber': [
'type': 'home', 'number': '212 555-1234' ,
'type': 'fax', 'number': '646 555-4567'
]
A number is printed in decimal without any decoration.
Example: 25
A string is printed in ASCII with single quotes in the start and end of the
string.
Example: 'test'
A key-value pair is printed as key followed by colon (:), a space ( ) and the
value.
Example: 'a': 67
A dictionary starts and ends with curly brackets ( and ) and then has a
comma (,) separated list of key-value pairs.
Example: 'name': 'Joe', 'age': 31
An array starts and ends with square brackets ([ and ]) and then has a
comma (,) separated list of values.
Example: [ 'hello', 56, 'world' ]
The binary representation of the JSON object contains a one byte identifier
that describes the type of the data to follow and is then immediately followed
by the data.
The identifiers and their types are as follows:
Identifier Type Description
0x01 Number 4 bytes signed integer in big endian byte order.
0x02 String N ASCII characters terminated by 0x00.
0x05 List Amount of items as a number followed by N values
0x06 Dictionary Amount of items as a number followed by N
key-value pairs
The program's task is to parse a binary file and prints it as human readable
text. It should read the data from standard input and writes it the result to
standard output.
Look at the files 'input_x' and their respective 'result_x' for examples of
input and output. More background can be found on e.g. www.json.org
Input_4 binary
firstsecondvalue for seconddeepinteger as keymixit is possible to mix integers and strings#Eg
My solution
public class Main
private static String INPUT_FILENAME = "input_4";
private static String OUTPUT_FILENAME = "result_4";
private static String RESOURCE_INPUT_PATH = "src/main/resources/input/";
private static String RESOURCE_OUTPUT_PATH = "src/main/resources/output/";
public static void main(String[] args)
File resourcesDirectory = new File(String.format("%s%s", RESOURCE_INPUT_PATH, INPUT_FILENAME));
File file = new File(resourcesDirectory.getAbsolutePath());
try
byte[] byteArray = Files.readAllBytes(file.toPath());
RecursiveParser recursiveParser = new RecursiveParser();
try
String result = recursiveParser.parse(byteArray, 0, false).toString();
String prettyPrinted = prettyPrint(result);
BufferedWriter writer = new BufferedWriter(
new FileWriter(
new File(
String.format("%s%s%s", RESOURCE_OUTPUT_PATH, OUTPUT_FILENAME, ".json")
)
)
);
writer.write(prettyPrinted);
writer.close();
catch (JSONException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
private static String prettyPrint(String data) throws JSONException
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
return (new JSONObject(data)).toString(4);
else if (json instanceof JSONArray)
return (new JSONArray(data)).toString(4);
else
return data; // nothing to pretty print
class RecursiveParser
private static int TERMINATE = 0x00;
private static int NUMBER = 0x01; // Number 4 bytes signed integer in big endian byte order.
private static int STRING = 0x02; // String N ASCII characters terminated by 0x00.
private static int LIST = 0x05; // List Amount of items as a number followed by N values
private static int DICTIONARY = 0x06; // Dictionary Amount of items as a number followed by N key-value pairs
Object parse(byte[] byteArray, int index, boolean hasSub) throws JSONException
for(; index < byteArray.length; index++)
if(byteArray[index] == NUMBER)
return getNumber(byteArray, index);
if(byteArray[index] == STRING)
return getString(byteArray, index);
if(byteArray[index] == LIST)
return getList(byteArray, index, hasSub);
if(byteArray[index] == DICTIONARY)
return getDictionary(byteArray, index, hasSub);
return null; // should never get here
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because dictionary size
int dictionarySize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(dictionarySize).array().length;
JSONWriter jsonWriter = new JSONStringer()
.object();
for(int i = 0; i < dictionarySize; i++)
jsonWriter
.endObject();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONObject(jsonWriter.toString())) : new JSONObject(jsonWriter.toString());
private Object getList(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because list size
int listSize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(listSize).array().length;
JSONWriter jsonWriter = new JSONStringer().array();
for(int i = 0; i < listSize; i++)
index++;
// check if sub-array or sub-dictionary
hasSub = hasSub
jsonWriter.endArray();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONArray(jsonWriter.toString())) : new JSONArray(jsonWriter.toString());
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
Result_4 output
"5": 25,
"deep":
"1": "integer as key",
"2": "4": 19088743,
"mix": "it is possible to mix integers and strings"
,
"first": 16777216,
"second": "value for second"
java recursion interview-questions json
New contributor
$endgroup$
Background
I got this interview test and got declined due to not meeting their expectations, but never got a reason on what was bad and how they solved it. I want to improve and learn something out of it.
Question
How can this be better written and what is badly written?
Assignment description
To optimize the amount of data sent between the web browser to the web server
a "clever" Javascript developer came up with the idea to translate JSON objects
into binary format in the application and send them to the server. Faced with
the fact that the Javascript is released in its final version to the customer
it is now your task to develop the parser on the back end system.
A JSON object is a hierarchy of key-value pairs where a value in its turn can
contain new key-value pairs. It consists of four basic types: numbers, strings,
arrays and dictionaries. An array is a list of values and a dictionay is a list
of key-value pairs. The key can only be of the type number or string while a
value can be of any type.
A JSON object always starts with a value.
An example JSON object can look like:
'firstName': 'John',
'lastName': 'Smith',
'age': 25,
'address':
'streetAddress': '21 2nd Street',
'city': 'New York',
'state': 'NY',
'postalCode': '10021'
,
'phoneNumber': [
'type': 'home', 'number': '212 555-1234' ,
'type': 'fax', 'number': '646 555-4567'
]
A number is printed in decimal without any decoration.
Example: 25
A string is printed in ASCII with single quotes in the start and end of the
string.
Example: 'test'
A key-value pair is printed as key followed by colon (:), a space ( ) and the
value.
Example: 'a': 67
A dictionary starts and ends with curly brackets ( and ) and then has a
comma (,) separated list of key-value pairs.
Example: 'name': 'Joe', 'age': 31
An array starts and ends with square brackets ([ and ]) and then has a
comma (,) separated list of values.
Example: [ 'hello', 56, 'world' ]
The binary representation of the JSON object contains a one byte identifier
that describes the type of the data to follow and is then immediately followed
by the data.
The identifiers and their types are as follows:
Identifier Type Description
0x01 Number 4 bytes signed integer in big endian byte order.
0x02 String N ASCII characters terminated by 0x00.
0x05 List Amount of items as a number followed by N values
0x06 Dictionary Amount of items as a number followed by N
key-value pairs
The program's task is to parse a binary file and prints it as human readable
text. It should read the data from standard input and writes it the result to
standard output.
Look at the files 'input_x' and their respective 'result_x' for examples of
input and output. More background can be found on e.g. www.json.org
Input_4 binary
firstsecondvalue for seconddeepinteger as keymixit is possible to mix integers and strings#Eg
My solution
public class Main
private static String INPUT_FILENAME = "input_4";
private static String OUTPUT_FILENAME = "result_4";
private static String RESOURCE_INPUT_PATH = "src/main/resources/input/";
private static String RESOURCE_OUTPUT_PATH = "src/main/resources/output/";
public static void main(String[] args)
File resourcesDirectory = new File(String.format("%s%s", RESOURCE_INPUT_PATH, INPUT_FILENAME));
File file = new File(resourcesDirectory.getAbsolutePath());
try
byte[] byteArray = Files.readAllBytes(file.toPath());
RecursiveParser recursiveParser = new RecursiveParser();
try
String result = recursiveParser.parse(byteArray, 0, false).toString();
String prettyPrinted = prettyPrint(result);
BufferedWriter writer = new BufferedWriter(
new FileWriter(
new File(
String.format("%s%s%s", RESOURCE_OUTPUT_PATH, OUTPUT_FILENAME, ".json")
)
)
);
writer.write(prettyPrinted);
writer.close();
catch (JSONException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
private static String prettyPrint(String data) throws JSONException
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
return (new JSONObject(data)).toString(4);
else if (json instanceof JSONArray)
return (new JSONArray(data)).toString(4);
else
return data; // nothing to pretty print
class RecursiveParser
private static int TERMINATE = 0x00;
private static int NUMBER = 0x01; // Number 4 bytes signed integer in big endian byte order.
private static int STRING = 0x02; // String N ASCII characters terminated by 0x00.
private static int LIST = 0x05; // List Amount of items as a number followed by N values
private static int DICTIONARY = 0x06; // Dictionary Amount of items as a number followed by N key-value pairs
Object parse(byte[] byteArray, int index, boolean hasSub) throws JSONException
for(; index < byteArray.length; index++)
if(byteArray[index] == NUMBER)
return getNumber(byteArray, index);
if(byteArray[index] == STRING)
return getString(byteArray, index);
if(byteArray[index] == LIST)
return getList(byteArray, index, hasSub);
if(byteArray[index] == DICTIONARY)
return getDictionary(byteArray, index, hasSub);
return null; // should never get here
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because dictionary size
int dictionarySize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(dictionarySize).array().length;
JSONWriter jsonWriter = new JSONStringer()
.object();
for(int i = 0; i < dictionarySize; i++)
jsonWriter
.endObject();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONObject(jsonWriter.toString())) : new JSONObject(jsonWriter.toString());
private Object getList(byte[] byteArray, int index, boolean hasSub) throws JSONException
index++; // move to size after type because list size
int listSize = (int)parse(byteArray, index, hasSub);
index += ByteBuffer.allocate(4).putInt(listSize).array().length;
JSONWriter jsonWriter = new JSONStringer().array();
for(int i = 0; i < listSize; i++)
index++;
// check if sub-array or sub-dictionary
hasSub = hasSub
jsonWriter.endArray();
return hasSub && index != (byteArray.length - 1) ? new AbstractMap.SimpleEntry<>(index, new JSONArray(jsonWriter.toString())) : new JSONArray(jsonWriter.toString());
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
Result_4 output
"5": 25,
"deep":
"1": "integer as key",
"2": "4": 19088743,
"mix": "it is possible to mix integers and strings"
,
"first": 16777216,
"second": "value for second"
java recursion interview-questions json
java recursion interview-questions json
New contributor
New contributor
edited 6 hours ago
422_unprocessable_entity
2,19831752
2,19831752
New contributor
asked 7 hours ago
RovdjuretRovdjuret
1285
1285
New contributor
New contributor
$begingroup$
Would you mind adding additional examples? I don't quite follow the transformation fromInput_4
toResult_4
. Where is, for example,"5": 25
coming from?
$endgroup$
– ggorlen
44 mins ago
add a comment |
$begingroup$
Would you mind adding additional examples? I don't quite follow the transformation fromInput_4
toResult_4
. Where is, for example,"5": 25
coming from?
$endgroup$
– ggorlen
44 mins ago
$begingroup$
Would you mind adding additional examples? I don't quite follow the transformation from
Input_4
to Result_4
. Where is, for example, "5": 25
coming from?$endgroup$
– ggorlen
44 mins ago
$begingroup$
Would you mind adding additional examples? I don't quite follow the transformation from
Input_4
to Result_4
. Where is, for example, "5": 25
coming from?$endgroup$
– ggorlen
44 mins ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
There are two points that showed up immediately when looking at your implementation:
- You missed one of the goals of the task: It should read the data from standard input and writes it the result to standard output.
- You are reading in the complete binary data into memory and process it from there.
The latter is a no go, if we're talking about large structures of JSON-data, e.g. containing Base64-encoded binary data of a DVD-image (don't ask ;-) It might be unlikely in a real world example but it might let them come to the conclusion that you're not familiar with stream based processing and had another one how showed that ability which might have led to their decision.
Some remarks about the actual code:
Your reading-number-implementation:
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
The specification said that a number is a signed integer in big endian order which is exactly how a Java int
is defined, so instead of creating temporary arrays and a BigInteger
you could simply have used an int
and used bit-shifting:
private int getNumber(byte[] byteArray, int index) = (byteArray[index + 2] & 0xff) < 16;
ret
If you had implemented a stream based processing and used a DataInputStream
the implementation would have been
private int getNumber(DataInputStream source)
return source.readInt();
Your reading-text-implementation:
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
Not much that can be changed here but for good measure, I'd change (char)byteArray[i]
to (char) (byteArray[i] & 0xff)
. For ASCII-characters that's irrelevant but still ;-)
In getDictionary
:
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException else if (value instanceof AbstractMap.SimpleEntry)
valueLength = (int) ((AbstractMap.SimpleEntry) value).getKey() - index;
jsonWriter.value(((AbstractMap.SimpleEntry) value).getValue());
This code block is duplicated in getList
, you should put that into its own method and call it from the two methods. Same is true for the logic with the return
-statement. This should be put into its own method so you only need to fix it once in case you find a bug in it.
General stuff:
You had to cope with the fact that your get
-methods had to return a value and should change the value of the index. That's not possible, so you decided to change the index value in the calling method in dependence of the type of the returned parsed value. This is "not optimal" to say the least.
New contributor
$endgroup$
$begingroup$
Thank you very much for your review, I see now that this wasn't one of my greatest moments such as missing a obvious requirement. I learned a lot from your review and will bring it into my future assignments. Really appreciate it, cheers!
$endgroup$
– Rovdjuret
6 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Rovdjuret is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215361%2frecursive-parser-from-binary-to-json-output%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
There are two points that showed up immediately when looking at your implementation:
- You missed one of the goals of the task: It should read the data from standard input and writes it the result to standard output.
- You are reading in the complete binary data into memory and process it from there.
The latter is a no go, if we're talking about large structures of JSON-data, e.g. containing Base64-encoded binary data of a DVD-image (don't ask ;-) It might be unlikely in a real world example but it might let them come to the conclusion that you're not familiar with stream based processing and had another one how showed that ability which might have led to their decision.
Some remarks about the actual code:
Your reading-number-implementation:
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
The specification said that a number is a signed integer in big endian order which is exactly how a Java int
is defined, so instead of creating temporary arrays and a BigInteger
you could simply have used an int
and used bit-shifting:
private int getNumber(byte[] byteArray, int index) = (byteArray[index + 2] & 0xff) < 16;
ret
If you had implemented a stream based processing and used a DataInputStream
the implementation would have been
private int getNumber(DataInputStream source)
return source.readInt();
Your reading-text-implementation:
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
Not much that can be changed here but for good measure, I'd change (char)byteArray[i]
to (char) (byteArray[i] & 0xff)
. For ASCII-characters that's irrelevant but still ;-)
In getDictionary
:
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException else if (value instanceof AbstractMap.SimpleEntry)
valueLength = (int) ((AbstractMap.SimpleEntry) value).getKey() - index;
jsonWriter.value(((AbstractMap.SimpleEntry) value).getValue());
This code block is duplicated in getList
, you should put that into its own method and call it from the two methods. Same is true for the logic with the return
-statement. This should be put into its own method so you only need to fix it once in case you find a bug in it.
General stuff:
You had to cope with the fact that your get
-methods had to return a value and should change the value of the index. That's not possible, so you decided to change the index value in the calling method in dependence of the type of the returned parsed value. This is "not optimal" to say the least.
New contributor
$endgroup$
$begingroup$
Thank you very much for your review, I see now that this wasn't one of my greatest moments such as missing a obvious requirement. I learned a lot from your review and will bring it into my future assignments. Really appreciate it, cheers!
$endgroup$
– Rovdjuret
6 hours ago
add a comment |
$begingroup$
There are two points that showed up immediately when looking at your implementation:
- You missed one of the goals of the task: It should read the data from standard input and writes it the result to standard output.
- You are reading in the complete binary data into memory and process it from there.
The latter is a no go, if we're talking about large structures of JSON-data, e.g. containing Base64-encoded binary data of a DVD-image (don't ask ;-) It might be unlikely in a real world example but it might let them come to the conclusion that you're not familiar with stream based processing and had another one how showed that ability which might have led to their decision.
Some remarks about the actual code:
Your reading-number-implementation:
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
The specification said that a number is a signed integer in big endian order which is exactly how a Java int
is defined, so instead of creating temporary arrays and a BigInteger
you could simply have used an int
and used bit-shifting:
private int getNumber(byte[] byteArray, int index) = (byteArray[index + 2] & 0xff) < 16;
ret
If you had implemented a stream based processing and used a DataInputStream
the implementation would have been
private int getNumber(DataInputStream source)
return source.readInt();
Your reading-text-implementation:
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
Not much that can be changed here but for good measure, I'd change (char)byteArray[i]
to (char) (byteArray[i] & 0xff)
. For ASCII-characters that's irrelevant but still ;-)
In getDictionary
:
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException else if (value instanceof AbstractMap.SimpleEntry)
valueLength = (int) ((AbstractMap.SimpleEntry) value).getKey() - index;
jsonWriter.value(((AbstractMap.SimpleEntry) value).getValue());
This code block is duplicated in getList
, you should put that into its own method and call it from the two methods. Same is true for the logic with the return
-statement. This should be put into its own method so you only need to fix it once in case you find a bug in it.
General stuff:
You had to cope with the fact that your get
-methods had to return a value and should change the value of the index. That's not possible, so you decided to change the index value in the calling method in dependence of the type of the returned parsed value. This is "not optimal" to say the least.
New contributor
$endgroup$
$begingroup$
Thank you very much for your review, I see now that this wasn't one of my greatest moments such as missing a obvious requirement. I learned a lot from your review and will bring it into my future assignments. Really appreciate it, cheers!
$endgroup$
– Rovdjuret
6 hours ago
add a comment |
$begingroup$
There are two points that showed up immediately when looking at your implementation:
- You missed one of the goals of the task: It should read the data from standard input and writes it the result to standard output.
- You are reading in the complete binary data into memory and process it from there.
The latter is a no go, if we're talking about large structures of JSON-data, e.g. containing Base64-encoded binary data of a DVD-image (don't ask ;-) It might be unlikely in a real world example but it might let them come to the conclusion that you're not familiar with stream based processing and had another one how showed that ability which might have led to their decision.
Some remarks about the actual code:
Your reading-number-implementation:
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
The specification said that a number is a signed integer in big endian order which is exactly how a Java int
is defined, so instead of creating temporary arrays and a BigInteger
you could simply have used an int
and used bit-shifting:
private int getNumber(byte[] byteArray, int index) = (byteArray[index + 2] & 0xff) < 16;
ret
If you had implemented a stream based processing and used a DataInputStream
the implementation would have been
private int getNumber(DataInputStream source)
return source.readInt();
Your reading-text-implementation:
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
Not much that can be changed here but for good measure, I'd change (char)byteArray[i]
to (char) (byteArray[i] & 0xff)
. For ASCII-characters that's irrelevant but still ;-)
In getDictionary
:
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException else if (value instanceof AbstractMap.SimpleEntry)
valueLength = (int) ((AbstractMap.SimpleEntry) value).getKey() - index;
jsonWriter.value(((AbstractMap.SimpleEntry) value).getValue());
This code block is duplicated in getList
, you should put that into its own method and call it from the two methods. Same is true for the logic with the return
-statement. This should be put into its own method so you only need to fix it once in case you find a bug in it.
General stuff:
You had to cope with the fact that your get
-methods had to return a value and should change the value of the index. That's not possible, so you decided to change the index value in the calling method in dependence of the type of the returned parsed value. This is "not optimal" to say the least.
New contributor
$endgroup$
There are two points that showed up immediately when looking at your implementation:
- You missed one of the goals of the task: It should read the data from standard input and writes it the result to standard output.
- You are reading in the complete binary data into memory and process it from there.
The latter is a no go, if we're talking about large structures of JSON-data, e.g. containing Base64-encoded binary data of a DVD-image (don't ask ;-) It might be unlikely in a real world example but it might let them come to the conclusion that you're not familiar with stream based processing and had another one how showed that ability which might have led to their decision.
Some remarks about the actual code:
Your reading-number-implementation:
private int getNumber(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
int offset = start + 4;
byte[] numberByteArray = Arrays.copyOfRange(byteArray, start, offset);
return new BigInteger(numberByteArray).intValue();
The specification said that a number is a signed integer in big endian order which is exactly how a Java int
is defined, so instead of creating temporary arrays and a BigInteger
you could simply have used an int
and used bit-shifting:
private int getNumber(byte[] byteArray, int index) = (byteArray[index + 2] & 0xff) < 16;
ret
If you had implemented a stream based processing and used a DataInputStream
the implementation would have been
private int getNumber(DataInputStream source)
return source.readInt();
Your reading-text-implementation:
private String getString(byte[] byteArray, int index)
int start = index + 1; // move to next value after type
StringBuilder value = new StringBuilder();
for(int i = start; i < byteArray.length; i++)
if(byteArray[i] == TERMINATE)
break;
value.append((char)byteArray[i]);
return value.toString();
Not much that can be changed here but for good measure, I'd change (char)byteArray[i]
to (char) (byteArray[i] & 0xff)
. For ASCII-characters that's irrelevant but still ;-)
In getDictionary
:
private Object getDictionary(byte[] byteArray, int index, boolean hasSub) throws JSONException else if (value instanceof AbstractMap.SimpleEntry)
valueLength = (int) ((AbstractMap.SimpleEntry) value).getKey() - index;
jsonWriter.value(((AbstractMap.SimpleEntry) value).getValue());
This code block is duplicated in getList
, you should put that into its own method and call it from the two methods. Same is true for the logic with the return
-statement. This should be put into its own method so you only need to fix it once in case you find a bug in it.
General stuff:
You had to cope with the fact that your get
-methods had to return a value and should change the value of the index. That's not possible, so you decided to change the index value in the calling method in dependence of the type of the returned parsed value. This is "not optimal" to say the least.
New contributor
New contributor
answered 6 hours ago
LotharLothar
1561
1561
New contributor
New contributor
$begingroup$
Thank you very much for your review, I see now that this wasn't one of my greatest moments such as missing a obvious requirement. I learned a lot from your review and will bring it into my future assignments. Really appreciate it, cheers!
$endgroup$
– Rovdjuret
6 hours ago
add a comment |
$begingroup$
Thank you very much for your review, I see now that this wasn't one of my greatest moments such as missing a obvious requirement. I learned a lot from your review and will bring it into my future assignments. Really appreciate it, cheers!
$endgroup$
– Rovdjuret
6 hours ago
$begingroup$
Thank you very much for your review, I see now that this wasn't one of my greatest moments such as missing a obvious requirement. I learned a lot from your review and will bring it into my future assignments. Really appreciate it, cheers!
$endgroup$
– Rovdjuret
6 hours ago
$begingroup$
Thank you very much for your review, I see now that this wasn't one of my greatest moments such as missing a obvious requirement. I learned a lot from your review and will bring it into my future assignments. Really appreciate it, cheers!
$endgroup$
– Rovdjuret
6 hours ago
add a comment |
Rovdjuret is a new contributor. Be nice, and check out our Code of Conduct.
Rovdjuret is a new contributor. Be nice, and check out our Code of Conduct.
Rovdjuret is a new contributor. Be nice, and check out our Code of Conduct.
Rovdjuret is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215361%2frecursive-parser-from-binary-to-json-output%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Would you mind adding additional examples? I don't quite follow the transformation from
Input_4
toResult_4
. Where is, for example,"5": 25
coming from?$endgroup$
– ggorlen
44 mins ago