158 lines
3.7 KiB
Plaintext
158 lines
3.7 KiB
Plaintext
|
|
print("=== JSON Encoder Demo ===");
|
||
|
|
print("");
|
||
|
|
|
||
|
|
class JSONEncoder {
|
||
|
|
JSONEncoder(this) {
|
||
|
|
}
|
||
|
|
|
||
|
|
encode(this, value) {
|
||
|
|
t = typeof(value);
|
||
|
|
if (t == "null") {
|
||
|
|
return "null";
|
||
|
|
}
|
||
|
|
if (t == "int" || t == "float") {
|
||
|
|
return str(value);
|
||
|
|
}
|
||
|
|
if (t == "str") {
|
||
|
|
return "\"" + value + "\"";
|
||
|
|
}
|
||
|
|
if (t == "array") {
|
||
|
|
return this.encodeArray(value);
|
||
|
|
}
|
||
|
|
return "null";
|
||
|
|
}
|
||
|
|
|
||
|
|
encodeArray(this, arr) {
|
||
|
|
result = "[";
|
||
|
|
for (i = 0; i < len(arr); i++) {
|
||
|
|
if (i > 0) {
|
||
|
|
result += ",";
|
||
|
|
}
|
||
|
|
result += this.encode(arr[i]);
|
||
|
|
}
|
||
|
|
result += "]";
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
encoder = new JSONEncoder();
|
||
|
|
|
||
|
|
print("Encoding primitives:");
|
||
|
|
print(" null ->", encoder.encode(null));
|
||
|
|
print(" 42 ->", encoder.encode(42));
|
||
|
|
print(" 3.14 ->", encoder.encode(3.14));
|
||
|
|
print(" \"hello\" ->", encoder.encode("hello"));
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("Encoding arrays:");
|
||
|
|
arr = {1, 2, 3};
|
||
|
|
print(" {1, 2, 3} ->", encoder.encodeArray(arr));
|
||
|
|
|
||
|
|
mixed = {1, "two", 3};
|
||
|
|
print(" {1, \"two\", 3} ->", encoder.encodeArray(mixed));
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("=== JSON Decoder Demo ===");
|
||
|
|
print("");
|
||
|
|
|
||
|
|
class JSONDecoder {
|
||
|
|
input = "";
|
||
|
|
pos = 0;
|
||
|
|
|
||
|
|
JSONDecoder(this) {
|
||
|
|
}
|
||
|
|
|
||
|
|
decode(this, jsonStr) {
|
||
|
|
this.input = jsonStr;
|
||
|
|
this.pos = 0;
|
||
|
|
return this.parseValue();
|
||
|
|
}
|
||
|
|
|
||
|
|
parseValue(this) {
|
||
|
|
this.skipWhitespace();
|
||
|
|
if (this.pos >= this.input.length) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
c = this.input.substr(this.pos, 1);
|
||
|
|
if (c == "[") {
|
||
|
|
return this.parseArray();
|
||
|
|
}
|
||
|
|
if (c == "n") {
|
||
|
|
this.pos += 4;
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return this.parseNumber();
|
||
|
|
}
|
||
|
|
|
||
|
|
parseNumber(this) {
|
||
|
|
start = this.pos;
|
||
|
|
while (this.pos < this.input.length) {
|
||
|
|
c = this.input.substr(this.pos, 1);
|
||
|
|
isDigit = (c == "0" || c == "1" || c == "2" || c == "3" || c == "4" || c == "5" || c == "6" || c == "7" || c == "8" || c == "9" || c == "-");
|
||
|
|
if (isDigit == 0) {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
this.pos++;
|
||
|
|
}
|
||
|
|
numStr = this.input.substr(start, this.pos - start);
|
||
|
|
return int(numStr);
|
||
|
|
}
|
||
|
|
|
||
|
|
parseArray(this) {
|
||
|
|
this.pos++;
|
||
|
|
result = {};
|
||
|
|
this.skipWhitespace();
|
||
|
|
if (this.pos < this.input.length) {
|
||
|
|
c = this.input.substr(this.pos, 1);
|
||
|
|
if (c == "]") {
|
||
|
|
this.pos++;
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
while (this.pos < this.input.length) {
|
||
|
|
value = this.parseValue();
|
||
|
|
result.push(value);
|
||
|
|
this.skipWhitespace();
|
||
|
|
if (this.pos >= this.input.length) {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
c = this.input.substr(this.pos, 1);
|
||
|
|
if (c == "]") {
|
||
|
|
this.pos++;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
if (c == ",") {
|
||
|
|
this.pos++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
skipWhitespace(this) {
|
||
|
|
while (this.pos < this.input.length) {
|
||
|
|
c = this.input.substr(this.pos, 1);
|
||
|
|
if (c != " ") {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
this.pos++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
decoder = new JSONDecoder();
|
||
|
|
|
||
|
|
print("Decoding numbers:");
|
||
|
|
print(" \"123\" ->", decoder.decode("123"));
|
||
|
|
print(" \"456\" ->", decoder.decode("456"));
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("Decoding arrays:");
|
||
|
|
decoded = decoder.decode("[1,2,3]");
|
||
|
|
print(" \"[1,2,3]\" -> length:", len(decoded));
|
||
|
|
print(" element 0:", decoded[0]);
|
||
|
|
print(" element 1:", decoded[1]);
|
||
|
|
print(" element 2:", decoded[2]);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("All JSON tests completed");
|