Skip to content

Commit 25ba9db

Browse files
Add java-gson example
1 parent 005ff9e commit 25ba9db

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

java-gson/additional_files/Main.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import com.google.gson.Gson;
2+
3+
class Person {
4+
private String name;
5+
private int age;
6+
7+
public Person(String name, int age) {
8+
this.name = name;
9+
this.age = age;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public void setName(String name) {
17+
this.name = name;
18+
}
19+
20+
public int getAge() {
21+
return age;
22+
}
23+
24+
public void setAge(int age) {
25+
this.age = age;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Person{name='" + name + "', age=" + age + "}";
31+
}
32+
}
33+
34+
35+
public class Main {
36+
public static void main(String[] args) {
37+
Gson gson = new Gson();
38+
39+
Person person = new Person("Nenad", 30);
40+
41+
String json = gson.toJson(person);
42+
System.out.println("Serialized JSON: " + json);
43+
44+
Person personFromJson = gson.fromJson(json, Person.class);
45+
System.out.println("Deserialized Object: " + personFromJson);
46+
}
47+
}

java-gson/additional_files/compile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
/usr/local/openjdk13/bin/javac -classpath ".:gson-2.11.0.jar" Main.java
291 KB
Binary file not shown.

java-gson/additional_files/run

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
/usr/local/openjdk13/bin/java -classpath ".:gson-2.11.0.jar" Main

java-gson/run_example.sh

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
source ../.env
3+
4+
function generate_request_body() {
5+
cat << EOF
6+
{
7+
"source_code": "",
8+
"language_id": 89,
9+
"additional_files": "$(cd additional_files; zip -r - . | $JUDGE0_BASE64_CMD -w0 -)"
10+
}
11+
EOF
12+
}
13+
14+
function create_submission() {
15+
echo "[$(date)] Generating request body..." 1>&2
16+
generate_request_body > request_body.json
17+
echo "[$(date)] Creating submission..." 1>&2
18+
19+
# Use these headers for RapidAPI
20+
# -H "X-RapidAPI-Key: $RAPIDAPI_KEY" \
21+
# -H "X-RapidAPI-Host: judge0-ce.p.rapidapi.com" \
22+
curl --progress-bar \
23+
--no-silent \
24+
-X POST \
25+
-H "Content-Type: application/json" \
26+
-H "Authorization: Bearer $SULU_KEY" \
27+
--data @request_body.json \
28+
--output request_response.json \
29+
"$JUDGE0_CE_BASE_URL/submissions?base64_encoded=true&wait=false"
30+
cat request_response.json
31+
}
32+
33+
function get_submission() {
34+
# Use these headers for RapidAPI
35+
# -H "X-RapidAPI-Key: $RAPIDAPI_KEY" \
36+
# -H "X-RapidAPI-Host: judge0-ce.p.rapidapi.com" \
37+
curl -H "Accept: application/json" \
38+
-H "Authorization: Bearer $SULU_KEY" \
39+
"$JUDGE0_CE_BASE_URL/submissions/$1?base64_encoded=true&fields=$2"
40+
}
41+
42+
token="$(create_submission | jq -r ".token")"
43+
if [[ "$token" == "null" ]]; then
44+
cat request_response.json | jq
45+
exit
46+
fi
47+
48+
echo "[$(date)] Token: $token"
49+
50+
for i in {1..10}; do
51+
sleep $(( i / 2 ))
52+
53+
status_id="$(get_submission "$token" "status" | jq -r ".status.id")"
54+
echo "[$(date)] Status ID: $status_id"
55+
56+
if [[ "$status_id" != "1" && "$status_id" != "2" ]]; then
57+
break
58+
fi
59+
done
60+
61+
submission_json="$(get_submission "$token" "status,stdout,stderr,compile_output,message")"
62+
63+
echo "[$(date)] Received submission:"
64+
echo "$submission_json" | jq
65+
66+
echo "[$(date)] Base64 decoded stdout:"
67+
echo "$submission_json" | jq -r ".stdout" | $JUDGE0_BASE64_CMD -d -

0 commit comments

Comments
 (0)